Annotation of loncom/homework/hint.pm, revision 1.37

1.21      albertel    1: # The LearningOnline Network with CAPA 
                      2: # implements the tags that control the hints
                      3: #
1.37    ! sakharuk    4: # $Id: hint.pm,v 1.36 2003/02/23 20:37:56 www Exp $
1.21      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
1.33      albertel   10: # LON-CAPA me&aree software; you can redistribute it and/or modify
1.21      albertel   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: 
1.1       albertel   29: package Apache::hinttags; 
                     30: 
                     31: use strict;
                     32: use Apache::lonnet;
1.6       albertel   33: use capa;
1.1       albertel   34: 
1.25      harris41   35: BEGIN {
1.29      albertel   36:   &Apache::lonxml::register('Apache::hinttags',('hintgroup','hintpart','numericalhint','stringhint','formulahint','optionhint','radiobuttonhint'));
1.1       albertel   37: }
                     38: 
1.2       albertel   39: 
1.15      albertel   40: @Apache::hint::which=();
1.1       albertel   41: sub start_hintgroup {
1.15      albertel   42:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     43:   my $skiptoend='0';
                     44:   my $result;
1.20      albertel   45: 
1.15      albertel   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 {
1.37    ! sakharuk   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: 	}
1.9       albertel   61:     }
1.20      albertel   62:     if ($skiptoend) {
1.34      albertel   63:       &Apache::lonxml::get_all_text("/hintgroup",$parser);
1.20      albertel   64:     }
1.24      sakharuk   65:   } elsif ($target eq 'tex') {
1.27      sakharuk   66:     $result .= '\keephidden{'; 
1.15      albertel   67:   }
                     68:   @Apache::hint::which=();
                     69:   return $result;
1.1       albertel   70: }
                     71: 
                     72: sub end_hintgroup {
1.15      albertel   73:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     74:   my $result;
1.20      albertel   75: 
1.15      albertel   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:");
1.37    ! sakharuk   83:     if ( $numtries >= $hinttries && $Apache::lonhomework::type ne 'exam') {
1.19      albertel   84:       $result='</td></tr></table>';
1.37    ! sakharuk   85:     } elsif ($Apache::lonhomework::type eq 'exam') {
        !            86:       my $garbage = &Apache::lonxml::endredirection;
1.19      albertel   87:     }
1.20      albertel   88:   } elsif ($target eq 'edit') {
                     89:     $result.=&Apache::edit::end_table();
1.27      sakharuk   90:   } elsif ($target eq 'tex') {
                     91:     $result .= '}'; 
1.15      albertel   92:   }
1.19      albertel   93:   @Apache::hint::which=();
1.15      albertel   94:   return $result;
1.3       albertel   95: }
                     96: 
                     97: sub start_numericalhint {
1.16      albertel   98:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.15      albertel   99:   #do everything in end, so intervening <responseparams> work
1.16      albertel  100:   &Apache::response::start_hintresponse($parstack,$safeeval);
1.20      albertel  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);
1.28      albertel  106:     if ($token->[1] eq 'numericalhint') {
1.36      www       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');
1.28      albertel  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:     }
1.26      albertel  116:     $result.=&Apache::edit::end_row();
                    117:     $result.=&Apache::edit::start_spanning_row();
1.20      albertel  118:   } elsif ($target eq 'modified') {
1.28      albertel  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:     }
1.20      albertel  130:     if ($constructtag) {
                    131:       $result  = &Apache::edit::rebuild_tag($token);
                    132:       $result .= &Apache::edit::handle_insert();
                    133:     }
1.33      albertel  134:   } elsif ($target eq 'web') {
                    135:       &Apache::response::reset_params();
1.20      albertel  136:   }
                    137:   return $result;
1.3       albertel  138: }
                    139: 
                    140: sub end_numericalhint {
1.15      albertel  141:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    142:   my $result;
                    143:   if ($target eq 'web') {
1.32      albertel  144:     if (!$Apache::lonxml::default_homework_loaded) {
                    145: 	&Apache::lonxml::default_homework_load($safeeval);
                    146:     }
1.15      albertel  147:     $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
                    148:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.18      albertel  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"};
1.15      albertel  156:     &Apache::lonxml::debug("hintgroup is using $response<br />\n");
1.18      albertel  157:     #build safe space expression
1.15      albertel  158:     my $expression="&caparesponse_check_list('".$response."','".
1.18      albertel  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.="');";
1.15      albertel  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='';
1.17      albertel  172:   } elsif ($target eq 'meta') {
1.35      albertel  173:     $result=&Apache::response::meta_package_write($token->[1]);
1.20      albertel  174:   } elsif ($target eq 'edit') {
                    175:     $result.='</td></tr>'.&Apache::edit::end_table;
1.15      albertel  176:   }
1.16      albertel  177:   &Apache::response::end_hintresponse();
1.15      albertel  178:   return $result;
1.28      albertel  179: }
                    180: 
                    181: sub start_stringhint {
1.35      albertel  182:     return &start_numericalhint(@_);
1.28      albertel  183: }
                    184: 
                    185: sub end_stringhint {
1.35      albertel  186:     return &end_numericalhint(@_);
1.28      albertel  187: }
                    188: 
                    189: sub start_formulahint {
1.35      albertel  190:     return &start_numericalhint(@_);
1.28      albertel  191: }
                    192: 
                    193: sub end_formulahint {
1.35      albertel  194:     return end_numericalhint(@_);
1.1       albertel  195: }
                    196: 
1.2       albertel  197: # a part shows if it is on, if no specific parts are on, then default shows
1.1       albertel  198: sub start_hintpart {
1.15      albertel  199:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    200: 
                    201:   my $show ='0';
1.20      albertel  202:   my $result = '';
1.15      albertel  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) {
1.34      albertel  213:       &Apache::lonxml::get_all_text("/hintpart",$parser);
1.1       albertel  214:     }
1.15      albertel  215:   } elsif ($target eq 'grade') {
1.34      albertel  216:     &Apache::lonxml::get_all_text("/hintpart",$parser);
1.20      albertel  217:   } elsif ($target eq 'edit') {
1.26      albertel  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();
1.20      albertel  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:     }
1.15      albertel  229:   }
1.20      albertel  230:   return $result;
1.1       albertel  231: }
                    232: 
                    233: sub end_hintpart {
1.29      albertel  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;
1.30      albertel  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:     }
1.29      albertel  260:     return $result;
                    261: }
                    262: 
                    263: sub end_optionhint {
                    264:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    265:     my $result;
1.30      albertel  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; }
1.29      albertel  313:     if ($target eq 'edit') { $result.=&Apache::edit::end_table; }
1.30      albertel  314:     &Apache::response::end_hintresponse();
1.29      albertel  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;
1.1       albertel  338: }
                    339: 
1.31      albertel  340: sub end_radiobuttonhint {
1.29      albertel  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: }
1.1       albertel  369: 1;
                    370: __END__

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