File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.37: download - view: text, annotated - select for diffs
Wed Apr 2 18:51:11 2003 UTC (21 years, 1 month ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
 In the exam mode (on-line) hints not appear on the screen.

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

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