File:  [LON-CAPA] / loncom / homework / hint.pm
Revision 1.36: download - view: text, annotated - select for diffs
Sun Feb 23 20:37:56 2003 UTC (21 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Adding links to help on units and format.

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

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