File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.38: download - view: text, annotated - select for diffs
Mon Apr 7 23:02:06 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- logic was incorrect here, so sometimes endredirection was called when startredirection wasn't

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.38 2003/04/07 23:02:06 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 ($Apache::lonhomework::type ne 'exam') {
   54: 	if ( $numtries < $hinttries ) {
   55: 	    $skiptoend='1';
   56: 	} else {
   57: 	    $result='<table bgcolor="#dddddd"><tr><td>';
   58: 	}
   59:     } else {
   60: 	&Apache::lonxml::startredirection;
   61:     }
   62:     if ($skiptoend) {
   63:       &Apache::lonxml::get_all_text("/hintgroup",$parser);
   64:     }
   65:   } elsif ($target eq 'tex') {
   66:     $result .= '\keephidden{'; 
   67:   }
   68:   @Apache::hint::which=();
   69:   return $result;
   70: }
   71: 
   72: sub end_hintgroup {
   73:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   74:   my $result;
   75: 
   76:   if ($target eq 'web') {
   77:     my $id=$Apache::inputtags::part;
   78:     my $numtries=$Apache::lonhomework::history{"resource.$id.tries"};
   79:     if ( $numtries eq '') { $numtries = 0; }
   80:     my $hinttries=&Apache::lonnet::EXT("resource.$id.hinttries");
   81:     if ( $hinttries eq '') { $hinttries = 1; }
   82:     &Apache::lonxml::debug("found :$id:$numtries:$hinttries:");
   83:     if ($Apache::lonhomework::type ne 'exam') {
   84: 	if ( $numtries >= $hinttries ) {
   85: 	    $result='</td></tr></table>';
   86: 	}
   87:     } else {
   88: 	&Apache::lonxml::endredirection;
   89:     }
   90:   } elsif ($target eq 'edit') {
   91:     $result.=&Apache::edit::end_table();
   92:   } elsif ($target eq 'tex') {
   93:     $result .= '}'; 
   94:   }
   95:   @Apache::hint::which=();
   96:   return $result;
   97: }
   98: 
   99: sub start_numericalhint {
  100:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  101:   #do everything in end, so intervening <responseparams> work
  102:   &Apache::response::start_hintresponse($parstack,$safeeval);
  103:   my $result;
  104:   if ($target eq 'edit') {
  105:     $result.=&Apache::edit::tag_start($target,$token);
  106:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  107:     $result.=&Apache::edit::text_arg('Answer:','answer',$token);
  108:     if ($token->[1] eq 'numericalhint') {
  109:       $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
  110:                          &Apache::loncommon::help_open_topic('Physical_Units');
  111:       $result.=&Apache::edit::text_arg('Format:','format',$token,4).
  112:               &Apache::loncommon::help_open_topic('Numerical_Response_Format');
  113:     } elsif ($token->[1] eq 'stringhint') {
  114:       $result.=&Apache::edit::select_arg('Type:','type',['cs','ci','mc'],$token);
  115:     } elsif ($token->[1] eq 'formulahint') {
  116:       $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
  117:     }
  118:     $result.=&Apache::edit::end_row();
  119:     $result.=&Apache::edit::start_spanning_row();
  120:   } elsif ($target eq 'modified') {
  121:     my $constructtag;
  122:     if ($token->[1] eq 'numericalhint') {
  123:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  124: 						'name','answer','unit','format');
  125:     } elsif ($token->[1] eq 'stringhint') {
  126:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  127: 						'name','answer','type');
  128:     } elsif ($token->[1] eq 'formulahint') {
  129:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  130: 						'name','answer','samples');
  131:     }
  132:     if ($constructtag) {
  133:       $result  = &Apache::edit::rebuild_tag($token);
  134:       $result .= &Apache::edit::handle_insert();
  135:     }
  136:   } elsif ($target eq 'web') {
  137:       &Apache::response::reset_params();
  138:   }
  139:   return $result;
  140: }
  141: 
  142: sub end_numericalhint {
  143:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  144:   my $result;
  145:   if ($target eq 'web') {
  146:     if (!$Apache::lonxml::default_homework_loaded) {
  147: 	&Apache::lonxml::default_homework_load($safeeval);
  148:     }
  149:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  150:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  151:     &Apache::response::setup_params('numericalhint');
  152:     my $partid=$Apache::inputtags::part;
  153:     my $id=$Apache::inputtags::response['-1'];
  154:     #id submissions occured under
  155:     my $submitid=$Apache::inputtags::response['-2'];
  156:     my $response = $Apache::lonhomework::history{
  157: 			    "resource.$partid.$submitid.submission"};
  158:     &Apache::lonxml::debug("hintgroup is using $response<br />\n");
  159:     #build safe space expression
  160:     my $expression="&caparesponse_check_list('".$response."','".
  161:       $$parstack[$#$parstack];
  162:     #need to get all possible parms
  163:     foreach my $key (keys(%Apache::inputtags::params)) {
  164:       $expression.= ';my $'. #'
  165: 	$key.'="'.$Apache::inputtags::params{$key}.'"';
  166:     }
  167:     $expression.="');";
  168:     $result = &Apache::run::run($expression,$safeeval);
  169:     &Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
  170:     my ($awards) = split /:/ , $result;
  171:     my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  172:     if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
  173:     $result='';
  174:   } elsif ($target eq 'meta') {
  175:     $result=&Apache::response::meta_package_write($token->[1]);
  176:   } elsif ($target eq 'edit') {
  177:     $result.='</td></tr>'.&Apache::edit::end_table;
  178:   }
  179:   &Apache::response::end_hintresponse();
  180:   return $result;
  181: }
  182: 
  183: sub start_stringhint {
  184:     return &start_numericalhint(@_);
  185: }
  186: 
  187: sub end_stringhint {
  188:     return &end_numericalhint(@_);
  189: }
  190: 
  191: sub start_formulahint {
  192:     return &start_numericalhint(@_);
  193: }
  194: 
  195: sub end_formulahint {
  196:     return end_numericalhint(@_);
  197: }
  198: 
  199: # a part shows if it is on, if no specific parts are on, then default shows
  200: sub start_hintpart {
  201:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  202: 
  203:   my $show ='0';
  204:   my $result = '';
  205:   if ($target eq 'web') {
  206:     my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  207:     &Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  208:     if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  209:       $show=1;
  210:     } else {
  211:       my $which;
  212:       foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  213:     }
  214:     if (!$show) {
  215:       &Apache::lonxml::get_all_text("/hintpart",$parser);
  216:     }
  217:   } elsif ($target eq 'grade') {
  218:     &Apache::lonxml::get_all_text("/hintpart",$parser);
  219:   } elsif ($target eq 'edit') {
  220:     $result.= &Apache::edit::tag_start($target,$token);
  221:     $result.= &Apache::edit::text_arg('On:','on',$token);
  222:     $result.= &Apache::edit::end_row();
  223:     $result.= &Apache::edit::start_spanning_row();
  224:   } elsif ($target eq 'modified') {
  225:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  226: 						 'on');
  227:     if ($constructtag) {
  228:       $result = &Apache::edit::rebuild_tag($token);
  229:       $result.=&Apache::edit::handle_insert();
  230:     }
  231:   }
  232:   return $result;
  233: }
  234: 
  235: sub end_hintpart {
  236:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  237:     my $result;
  238:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  239:     return $result;
  240: }
  241: 
  242: sub start_optionhint {
  243:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  244:     my $result;
  245:     &Apache::response::start_hintresponse($parstack,$safeeval);
  246:     if ($target eq 'edit') {
  247: 	$result.=&Apache::edit::tag_start($target,$token);
  248: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  249: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  250: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  251:     } elsif ($target eq 'modified') {
  252: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  253: 						     $safeeval,'name',
  254: 						     'answer','concept');
  255: 	if ($constructtag) {
  256: 	    $result  = &Apache::edit::rebuild_tag($token);
  257: 	    $result .= &Apache::edit::handle_insert();
  258: 	}
  259:     } elsif ($target eq 'meta') {
  260: 	$result=&Apache::response::meta_package_write('numericalhint');
  261:     }
  262:     return $result;
  263: }
  264: 
  265: sub end_optionhint {
  266:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  267:     my $result;
  268:     if ($target eq 'web') {
  269: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  270: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  271: 	my $partid=$Apache::inputtags::part;
  272: 	#id submissions occured under
  273: 	my $submitid=$Apache::inputtags::response['-2'];
  274: 	my $part_id="$partid.$submitid";
  275: 	my %answer;
  276: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  277: 	if ($answer) {
  278: 	    eval('%answer ='.$answer);
  279: 	    &Apache::lonhomework::showhash(%answer);
  280: 	    my $response = $Apache::lonhomework::history{
  281: 				            "resource.$part_id.submission"};
  282: 	    my %response=&Apache::lonnet::str2hash($response);
  283: 	    &Apache::lonhomework::showhash(%response);
  284: 	    foreach my $foil (keys(%answer)) {
  285: 		$foilmatch=1;
  286: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  287: 	    }
  288: 	}
  289: 	my %concept;
  290: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  291: 	if ( $constr ) { eval('%concept ='.$constr);	}
  292: 	my $response = $Apache::lonhomework::history{
  293: 					"resource.$part_id.submissiongrading"};
  294: 	my %response=&Apache::lonnet::str2hash($response);
  295: 	foreach my $concept (keys(%concept)) {
  296: 	    my $compare;
  297: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  298: 	    $conceptmatch=1;
  299: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  300: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  301: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  302: 		    if ( exists($response{$foil}) && 
  303: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  304: 		}
  305: 	    } else {
  306: 		$conceptmatch=0;
  307: 	    }
  308: 	    if ($conceptmatch eq '0') { last; }
  309: 	}
  310: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  311: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  312: 	    push(@Apache::hint::which,$name);
  313: 	}
  314:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  315:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  316:     &Apache::response::end_hintresponse();
  317:     return $result;
  318: }
  319: 
  320: sub start_radiobuttonhint {
  321:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  322:     my $result;
  323:     &Apache::response::start_hintresponse($parstack,$safeeval);
  324:     if ($target eq 'edit') {
  325: 	$result.=&Apache::edit::tag_start($target,$token);
  326: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  327: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  328:     } elsif ($target eq 'modified') {
  329: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  330: 						     $safeeval,'name',
  331: 						     'answer');
  332: 	if ($constructtag) {
  333: 	    $result  = &Apache::edit::rebuild_tag($token);
  334: 	    $result .= &Apache::edit::handle_insert();
  335: 	}
  336:     } elsif ($target eq 'meta') {
  337: 	$result=&Apache::response::meta_package_write('numericalhint');
  338:     }
  339:     return $result;
  340: }
  341: 
  342: sub end_radiobuttonhint {
  343:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  344:     my $result;
  345:     if ($target eq 'web') {
  346: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  347: 	my @answer;
  348: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  349: 	eval('@answer ='.$answer);
  350: 	my $partid=$Apache::inputtags::part;
  351: 	#id submissions occured under
  352: 	my $submitid=$Apache::inputtags::response['-2'];
  353: 	my $part_id="$partid.$submitid";
  354: 	my $response = $Apache::lonhomework::history{
  355: 			    "resource.$part_id.submission"};
  356: 	($response)=&Apache::lonnet::str2hash($response);
  357: 	if ($answer[0] eq 'foil' && $response eq $answer[1]) {
  358: 	    push (@Apache::hint::which,$name);
  359: 	} elsif ($answer[0] eq 'concept') {
  360: 	    if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer[1]})) {
  361: 		my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer[1]} };
  362: 		if (grep(/^\Q$response\E$/,@names)) {
  363: 		    push(@Apache::hint::which,$name);
  364: 		}
  365: 	    }
  366: 	}
  367:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  368:     &Apache::response::end_hintresponse();
  369:     return $result;
  370: }
  371: 1;
  372: __END__

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