File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.32: download - view: text, annotated - select for diffs
Wed Nov 13 23:37:56 2002 UTC (21 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added more checks to make sure default homework is loaded in cases where people would care.
- Fixes BUG#957

    1: # The LearningOnline Network with CAPA 
    2: # implements the tags that control the hints
    3: #
    4: # $Id: hint.pm,v 1.32 2002/11/13 23:37:56 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 is free 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[$#$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:   }
  127:   return $result;
  128: }
  129: 
  130: sub end_numericalhint {
  131:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  132:   my $result;
  133:   if ($target eq 'web') {
  134:     if (!$Apache::lonxml::default_homework_loaded) {
  135: 	&Apache::lonxml::default_homework_load($safeeval);
  136:     }
  137:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  138:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  139:     &Apache::response::setup_params('numericalhint');
  140:     my $partid=$Apache::inputtags::part;
  141:     my $id=$Apache::inputtags::response['-1'];
  142:     #id submissions occured under
  143:     my $submitid=$Apache::inputtags::response['-2'];
  144:     my $response = $Apache::lonhomework::history{
  145: 			    "resource.$partid.$submitid.submission"};
  146:     &Apache::lonxml::debug("hintgroup is using $response<br />\n");
  147:     #build safe space expression
  148:     my $expression="&caparesponse_check_list('".$response."','".
  149:       $$parstack[$#$parstack];
  150:     #need to get all possible parms
  151:     foreach my $key (keys(%Apache::inputtags::params)) {
  152:       $expression.= ';my $'. #'
  153: 	$key.'="'.$Apache::inputtags::params{$key}.'"';
  154:     }
  155:     $expression.="');";
  156:     $result = &Apache::run::run($expression,$safeeval);
  157:     &Apache::lonxml::debug("$expression:result:$result:$Apache::lonxml::curdepth");
  158:     my ($awards) = split /:/ , $result;
  159:     my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  160:     if ($ad eq 'EXACT_ANS' || $ad eq 'APPROX_ANS') { push (@Apache::hint::which,$name); }
  161:     $result='';
  162:   } elsif ($target eq 'meta') {
  163:     $result=&Apache::response::meta_package_write('numericalhint');
  164:   } elsif ($target eq 'edit') {
  165:     $result.='</td></tr>'.&Apache::edit::end_table;
  166:   }
  167:   &Apache::response::end_hintresponse();
  168:   return $result;
  169: }
  170: 
  171: sub start_stringhint {
  172:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  173:   my $result;
  174:   if ($target eq 'meta') {
  175:     $result=&Apache::response::meta_package_write('stringhint');
  176:   } else {
  177:     $result.=&start_numericalhint(@_);
  178:   }
  179:   return $result;
  180: }
  181: 
  182: sub end_stringhint {
  183:   return end_numericalhint(@_);
  184: }
  185: 
  186: sub start_formulahint {
  187:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  188:   my $result;
  189:   if ($target eq 'meta') {
  190:     $result=&Apache::response::meta_package_write('formulahint');
  191:   } else {
  192:     $result.=&start_numericalhint(@_);
  193:   }
  194:   return $result;
  195: }
  196: 
  197: sub end_formulahint {
  198:   return end_numericalhint(@_);
  199: }
  200: 
  201: # a part shows if it is on, if no specific parts are on, then default shows
  202: sub start_hintpart {
  203:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  204: 
  205:   my $show ='0';
  206:   my $result = '';
  207:   if ($target eq 'web') {
  208:     my $on= &Apache::lonxml::get_param('on',$parstack,$safeeval);
  209:     &Apache::lonxml::debug("hintpart sees $on and ,$#Apache::hint::which");
  210:     if ( $on eq 'default' && $#Apache::hint::which == '-1') {
  211:       $show=1;
  212:     } else {
  213:       my $which;
  214:       foreach $which (@Apache::hint::which) { if ($which eq $on) { $show = 1; last } }
  215:     }
  216:     if (!$show) {
  217:       &Apache::lonxml::get_all_text("/hintpart",$$parser[$#$parser]);
  218:     }
  219:   } elsif ($target eq 'grade') {
  220:     &Apache::lonxml::get_all_text("/hintpart",$$parser[$#$parser]);
  221:   } elsif ($target eq 'edit') {
  222:     $result.= &Apache::edit::tag_start($target,$token);
  223:     $result.= &Apache::edit::text_arg('On:','on',$token);
  224:     $result.= &Apache::edit::end_row();
  225:     $result.= &Apache::edit::start_spanning_row();
  226:   } elsif ($target eq 'modified') {
  227:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  228: 						 'on');
  229:     if ($constructtag) {
  230:       $result = &Apache::edit::rebuild_tag($token);
  231:       $result.=&Apache::edit::handle_insert();
  232:     }
  233:   }
  234:   return $result;
  235: }
  236: 
  237: sub end_hintpart {
  238:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  239:     my $result;
  240:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  241:     return $result;
  242: }
  243: 
  244: sub start_optionhint {
  245:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  246:     my $result;
  247:     &Apache::response::start_hintresponse($parstack,$safeeval);
  248:     if ($target eq 'edit') {
  249: 	$result.=&Apache::edit::tag_start($target,$token);
  250: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  251: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token,40);
  252: 	$result.=&Apache::edit::text_arg('Concept:','concept',$token,50);
  253:     } elsif ($target eq 'modified') {
  254: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  255: 						     $safeeval,'name',
  256: 						     'answer','concept');
  257: 	if ($constructtag) {
  258: 	    $result  = &Apache::edit::rebuild_tag($token);
  259: 	    $result .= &Apache::edit::handle_insert();
  260: 	}
  261:     } elsif ($target eq 'meta') {
  262: 	$result=&Apache::response::meta_package_write('numericalhint');
  263:     }
  264:     return $result;
  265: }
  266: 
  267: sub end_optionhint {
  268:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  269:     my $result;
  270:     if ($target eq 'web') {
  271: 	my ($foilmatch,$conceptmatch)=(-1,-1);
  272: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  273: 	my $partid=$Apache::inputtags::part;
  274: 	#id submissions occured under
  275: 	my $submitid=$Apache::inputtags::response['-2'];
  276: 	my $part_id="$partid.$submitid";
  277: 	my %answer;
  278: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  279: 	if ($answer) {
  280: 	    eval('%answer ='.$answer);
  281: 	    &Apache::lonhomework::showhash(%answer);
  282: 	    my $response = $Apache::lonhomework::history{
  283: 				            "resource.$part_id.submission"};
  284: 	    my %response=&Apache::lonnet::str2hash($response);
  285: 	    &Apache::lonhomework::showhash(%response);
  286: 	    foreach my $foil (keys(%answer)) {
  287: 		$foilmatch=1;
  288: 		if ($answer{$foil} ne $response{$foil}) {$foilmatch=0;last;}
  289: 	    }
  290: 	}
  291: 	my %concept;
  292: 	my $constr=&Apache::lonxml::get_param('concept',$parstack,$safeeval);
  293: 	if ( $constr ) { eval('%concept ='.$constr);	}
  294: 	my $response = $Apache::lonhomework::history{
  295: 					"resource.$part_id.submissiongrading"};
  296: 	my %response=&Apache::lonnet::str2hash($response);
  297: 	foreach my $concept (keys(%concept)) {
  298: 	    my $compare;
  299: 	    if ($concept{$concept} eq 'correct') {$compare=1}else{$compare=0}
  300: 	    $conceptmatch=1;
  301: 	    if (ref($Apache::hint::option{"$part_id.concepts"})) {
  302: 		foreach my $foil (@{ $Apache::hint::option{"$part_id.concept.$concept"} }) {
  303: 		    &Apache::lonxml::debug("compare -$foil- -$response{$foil}-$compare-");
  304: 		    if ( exists($response{$foil}) && 
  305: 			 $response{$foil} ne $compare) {$conceptmatch=0;last;}
  306: 		}
  307: 	    } else {
  308: 		$conceptmatch=0;
  309: 	    }
  310: 	    if ($conceptmatch eq '0') { last; }
  311: 	}
  312: 	if ( ($conceptmatch eq '-1' || $conceptmatch eq '1') &&
  313: 	     ($foilmatch    eq '-1' || $foilmatch    eq '1') ) {
  314: 	    push(@Apache::hint::which,$name);
  315: 	}
  316:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  317:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  318:     &Apache::response::end_hintresponse();
  319:     return $result;
  320: }
  321: 
  322: sub start_radiobuttonhint {
  323:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  324:     my $result;
  325:     &Apache::response::start_hintresponse($parstack,$safeeval);
  326:     if ($target eq 'edit') {
  327: 	$result.=&Apache::edit::tag_start($target,$token);
  328: 	$result.=&Apache::edit::text_arg('Name:','name',$token);
  329: 	$result.=&Apache::edit::text_arg('Answer:','answer',$token);
  330:     } elsif ($target eq 'modified') {
  331: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  332: 						     $safeeval,'name',
  333: 						     'answer');
  334: 	if ($constructtag) {
  335: 	    $result  = &Apache::edit::rebuild_tag($token);
  336: 	    $result .= &Apache::edit::handle_insert();
  337: 	}
  338:     } elsif ($target eq 'meta') {
  339: 	$result=&Apache::response::meta_package_write('numericalhint');
  340:     }
  341:     return $result;
  342: }
  343: 
  344: sub end_radiobuttonhint {
  345:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  346:     my $result;
  347:     if ($target eq 'web') {
  348: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  349: 	my @answer;
  350: 	my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
  351: 	eval('@answer ='.$answer);
  352: 	my $partid=$Apache::inputtags::part;
  353: 	#id submissions occured under
  354: 	my $submitid=$Apache::inputtags::response['-2'];
  355: 	my $part_id="$partid.$submitid";
  356: 	my $response = $Apache::lonhomework::history{
  357: 			    "resource.$part_id.submission"};
  358: 	($response)=&Apache::lonnet::str2hash($response);
  359: 	if ($answer[0] eq 'foil' && $response eq $answer[1]) {
  360: 	    push (@Apache::hint::which,$name);
  361: 	} elsif ($answer[0] eq 'concept') {
  362: 	    if (ref($Apache::hint::radiobutton{"$part_id.concept.".$answer[1]})) {
  363: 		my @names=@{ $Apache::hint::radiobutton{"$part_id.concept.".$answer[1]} };
  364: 		if (grep(/^\Q$response\E$/,@names)) {
  365: 		    push(@Apache::hint::which,$name);
  366: 		}
  367: 	    }
  368: 	}
  369:     } elsif ($target eq 'edit') { $result.=&Apache::edit::end_table; }
  370:     &Apache::response::end_hintresponse();
  371:     return $result;
  372: }
  373: 1;
  374: __END__

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