File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.35: download - view: text, annotated - select for diffs
Thu Feb 13 23:48:27 2003 UTC (21 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixes most of BUG#1194, still need to work on editing

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.35 2003/02/13 23:48:27 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA me&aree software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::hinttags; 
   30: 
   31: use strict;
   32: use Apache::lonnet;
   33: use capa;
   34: 
   35: BEGIN {
   36:   &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint'));
   37: }
   38: 
   39: 
   40: @Apache::hint::which=();
   41: sub start_hintgroup {
   42:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   43:   my $skiptoend='0';
   44:   my $result;
   45: 
   46:   if ($target eq 'web') {
   47:     my $id=$Apache::inputtags::part;
   48:     my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
   49:     if ( $numtries eq '') { $numtries = 0; }
   50:     my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
   51:     if ( $hinttries eq '') { $hinttries = 1; }
   52:     &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   53:     if ( $numtries < $hinttries ) {
   54:       $skiptoend='1';
   55:     } else {
   56:       if ($target eq 'web') {$result='<table bgcolor="#dddddd"><tr><td>';}
   57:     }
   58:     if ($skiptoend) {
   59:       &Apache::lonxml::get_all_text("/hintgroup",$parser);
   60:     }
   61:   } elsif ($target eq 'tex') {
   62:     $result .= '\keephidden{'; 
   63:   }
   64:   @Apache::hint::which=();
   65:   return $result;
   66: }
   67: 
   68: sub end_hintgroup {
   69:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   70:   my $result;
   71: 
   72:   if ($target eq 'web') {
   73:     my $id=$Apache::inputtags::part;
   74:     my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
   75:     if ( $numtries eq '') { $numtries = 0; }
   76:     my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
   77:     if ( $hinttries eq '') { $hinttries = 1; }
   78:     &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   79:     if ( $numtries >= $hinttries ) {
   80:       $result='</td></tr></table>';
   81:     }
   82:   } elsif ($target eq 'edit') {
   83:     $result.=&Apache::edit::end_table();
   84:   } elsif ($target eq 'tex') {
   85:     $result .= '}'; 
   86:   }
   87:   @Apache::hint::which=();
   88:   return $result;
   89: }
   90: 
   91: sub start_numericalhint {
   92:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   93:   #do everything in end, so intervening <responseparams> work
   94:   &Apache::response::start_hintresponse($parstack,$safeeval);
   95:   my $result;
   96:   if ($target eq 'edit') {
   97:     $result.=&Apache::edit::tag_start($target,$token);
   98:     $result.=&Apache::edit::text_arg('Name:','name',$token);
   99:     $result.=&Apache::edit::text_arg('Answer:','answer',$token);
  100:     if ($token->[1] eq 'numericalhint') {
  101:       $result.=&Apache::edit::text_arg('Unit:','unit',$token,5);
  102:       $result.=&Apache::edit::text_arg('Format:','format',$token,4);
  103:     } elsif ($token->[1] eq 'stringhint') {
  104:       $result.=&Apache::edit::select_arg('Type:','type',['cs','ci','mc'],$token);
  105:     } elsif ($token->[1] eq 'formulahint') {
  106:       $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
  107:     }
  108:     $result.=&Apache::edit::end_row();
  109:     $result.=&Apache::edit::start_spanning_row();
  110:   } elsif ($target eq 'modified') {
  111:     my $constructtag;
  112:     if ($token->[1] eq 'numericalhint') {
  113:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  114: 						'name','answer','unit','format');
  115:     } elsif ($token->[1] eq 'stringhint') {
  116:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  117: 						'name','answer','type');
  118:     } elsif ($token->[1] eq 'formulahint') {
  119:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  120: 						'name','answer','samples');
  121:     }
  122:     if ($constructtag) {
  123:       $result  = &Apache::edit::rebuild_tag($token);
  124:       $result .= &Apache::edit::handle_insert();
  125:     }
  126:   } elsif ($target eq 'web') {
  127:       &Apache::response::reset_params();
  128:   }
  129:   return $result;
  130: }
  131: 
  132: sub end_numericalhint {
  133:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  134:   my $result;
  135:   if ($target eq 'web') {
  136:     if (!$Apache::lonxml::default_homework_loaded) {
  137: 	&Apache::lonxml::default_homework_load($safeeval);
  138:     }
  139:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  140:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  141:     &Apache::response::setup_params('numericalhint');
  142:     my $partid=$Apache::inputtags::part;
  143:     my $id=$Apache::inputtags::response['-1'];
  144:     #id submissions occured under
  145:     my $submitid=$Apache::inputtags::response['-2'];
  146:     my $response = $Apache::lonhomework::history{
  147: 			    "resource.$partid.$submitid.submission"};
  148:     &Apache::lonxml::debug("hintgroup is using $response<br />\n");
  149:     #build safe space expression
  150:     my $expression="&caparesponse_check_list('".$response."','".
  151:       $$parstack[$#$parstack];
  152:     #need to get all possible parms
  153:     foreach my $key (keys(%Apache::inputtags::params)) {
  154:       $expression.= ';my $'. #'
  155: 	$key.'="'.$Apache::inputtags::params{$key}.'"';
  156:     }
  157:     $expression.="');";
  158:     $result = &Apache::run::run($expression,$safeeval);
  159:     &Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
  160:     my ($awards) = split /:/ , $result;
  161:     my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  162:     if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
  163:     $result='';
  164:   } elsif ($target eq 'meta') {
  165:     $result=&Apache::response::meta_package_write($token->[1]);
  166:   } elsif ($target eq 'edit') {
  167:     $result.='</td></tr>'.&Apache::edit::end_table;
  168:   }
  169:   &Apache::response::end_hintresponse();
  170:   return $result;
  171: }
  172: 
  173: sub start_stringhint {
  174:     return &start_numericalhint(@_);
  175: }
  176: 
  177: sub end_stringhint {
  178:     return &end_numericalhint(@_);
  179: }
  180: 
  181: sub start_formulahint {
  182:     return &start_numericalhint(@_);
  183: }
  184: 
  185: sub end_formulahint {
  186:     return end_numericalhint(@_);
  187: }
  188: 
  189: # a part shows if it is on, if no specific parts are on, then default shows
  190: sub start_hintpart {
  191:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  192: 
  193:   my $show ='0';
  194:   my $result = '';
  195:   if ($target eq 'web') {
  196:     my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  197:     &Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  198:     if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  199:       $show=1;
  200:     } else {
  201:       my $which;
  202:       foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  203:     }
  204:     if (!$show) {
  205:       &Apache::lonxml::get_all_text("/hintpart",$parser);
  206:     }
  207:   } elsif ($target eq 'grade') {
  208:     &Apache::lonxml::get_all_text("/hintpart",$parser);
  209:   } elsif ($target eq 'edit') {
  210:     $result.= &Apache::edit::tag_start($target,$token);
  211:     $result.= &Apache::edit::text_arg('On:','on',$token);
  212:     $result.= &Apache::edit::end_row();
  213:     $result.= &Apache::edit::start_spanning_row();
  214:   } elsif ($target eq 'modified') {
  215:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  216: 						 'on');
  217:     if ($constructtag) {
  218:       $result = &Apache::edit::rebuild_tag($token);
  219:       $result.=&Apache::edit::handle_insert();
  220:     }
  221:   }
  222:   return $result;
  223: }
  224: 
  225: sub end_hintpart {
  226:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  227:     my $result;
  228:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  229:     return $result;
  230: }
  231: 
  232: sub start_optionhint {
  233:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  234:     my $result;
  235:     &Apache::response::start_hintresponse($parstack,$safeeval);
  236:     if ($target eq 'edit') {
  237: 	$result.=&Apache::edit::tag_start($target,$token);
  238: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  239: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  240: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  241:     } elsif ($target eq 'modified') {
  242: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  243: 						     $safeeval,'name',
  244: 						     'answer','concept');
  245: 	if ($constructtag) {
  246: 	    $result  = &Apache::edit::rebuild_tag($token);
  247: 	    $result .= &Apache::edit::handle_insert();
  248: 	}
  249:     } elsif ($target eq 'meta') {
  250: 	$result=&Apache::response::meta_package_write('numericalhint');
  251:     }
  252:     return $result;
  253: }
  254: 
  255: sub end_optionhint {
  256:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  257:     my $result;
  258:     if ($target eq 'web') {
  259: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  260: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  261: 	my $partid=$Apache::inputtags::part;
  262: 	#id submissions occured under
  263: 	my $submitid=$Apache::inputtags::response['-2'];
  264: 	my $part_id="$partid.$submitid";
  265: 	my %answer;
  266: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  267: 	if ($answer) {
  268: 	    eval('%answer ='.$answer);
  269: 	    &Apache::lonhomework::showhash(%answer);
  270: 	    my $response = $Apache::lonhomework::history{
  271: 				            "resource.$part_id.submission"};
  272: 	    my %response=&Apache::lonnet::str2hash($response);
  273: 	    &Apache::lonhomework::showhash(%response);
  274: 	    foreach my $foil (keys(%answer)) {
  275: 		$foilmatch=1;
  276: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  277: 	    }
  278: 	}
  279: 	my %concept;
  280: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  281: 	if ( $constr ) { eval('%concept ='.$constr);	}
  282: 	my $response = $Apache::lonhomework::history{
  283: 					"resource.$part_id.submissiongrading"};
  284: 	my %response=&Apache::lonnet::str2hash($response);
  285: 	foreach my $concept (keys(%concept)) {
  286: 	    my $compare;
  287: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  288: 	    $conceptmatch=1;
  289: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  290: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  291: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  292: 		    if ( exists($response{$foil}) && 
  293: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  294: 		}
  295: 	    } else {
  296: 		$conceptmatch=0;
  297: 	    }
  298: 	    if ($conceptmatch eq '0') { last; }
  299: 	}
  300: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  301: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  302: 	    push(@Apache::hint::which,$name);
  303: 	}
  304:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  305:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  306:     &Apache::response::end_hintresponse();
  307:     return $result;
  308: }
  309: 
  310: sub start_radiobuttonhint {
  311:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  312:     my $result;
  313:     &Apache::response::start_hintresponse($parstack,$safeeval);
  314:     if ($target eq 'edit') {
  315: 	$result.=&Apache::edit::tag_start($target,$token);
  316: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  317: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  318:     } elsif ($target eq 'modified') {
  319: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  320: 						     $safeeval,'name',
  321: 						     'answer');
  322: 	if ($constructtag) {
  323: 	    $result  = &Apache::edit::rebuild_tag($token);
  324: 	    $result .= &Apache::edit::handle_insert();
  325: 	}
  326:     } elsif ($target eq 'meta') {
  327: 	$result=&Apache::response::meta_package_write('numericalhint');
  328:     }
  329:     return $result;
  330: }
  331: 
  332: sub end_radiobuttonhint {
  333:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  334:     my $result;
  335:     if ($target eq 'web') {
  336: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  337: 	my @answer;
  338: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  339: 	eval('@answer ='.$answer);
  340: 	my $partid=$Apache::inputtags::part;
  341: 	#id submissions occured under
  342: 	my $submitid=$Apache::inputtags::response['-2'];
  343: 	my $part_id="$partid.$submitid";
  344: 	my $response = $Apache::lonhomework::history{
  345: 			    "resource.$part_id.submission"};
  346: 	($response)=&Apache::lonnet::str2hash($response);
  347: 	if ($answer[0] eq 'foil' && $response eq $answer[1]) {
  348: 	    push (@Apache::hint::which,$name);
  349: 	} elsif ($answer[0] eq 'concept') {
  350: 	    if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer[1]})) {
  351: 		my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer[1]} };
  352: 		if (grep(/^\Q$response\E$/,@names)) {
  353: 		    push(@Apache::hint::which,$name);
  354: 		}
  355: 	    }
  356: 	}
  357:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  358:     &Apache::response::end_hintresponse();
  359:     return $result;
  360: }
  361: 1;
  362: __END__

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