File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.34: download - view: text, annotated - select for diffs
Mon Jan 7 18:03:57 2002 UTC (22 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- stop bombing on incomplete problems

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.34 2002/01/07 18:03:57 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: # 2/21 Guy
   29: 
   30: package Apache::radiobuttonresponse;
   31: use strict;
   32: 
   33: sub BEGIN {
   34:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   35: }
   36: 
   37: sub start_radiobuttonresponse {
   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   39:   my $result;
   40:   #when in a radiobutton response use these
   41:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   42:   push (@Apache::lonxml::namespace,'radiobuttonresponse');
   43:   my $id = &Apache::response::start_response($parstack,$safeeval);
   44:   if ($target eq 'meta') {
   45:     $result=&Apache::response::meta_package_write('radiobuttonresponse');
   46:   } elsif ($target eq 'edit' ) {
   47:     $result.=&Apache::edit::start_table($token).
   48:       '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
   49: 	&Apache::edit::deletelist($target,$token)
   50: 	  ."</td><td>&nbsp</td></tr><tr><td colspan=\"3\">\n";
   51:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
   52:       "</td></tr>";
   53:     $result.="<tr><td colspan=\"3\">\n";
   54:   } elsif ($target eq 'modified') {
   55:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   56: 						 $safeeval,'max');
   57:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   58:   }
   59:   return $result;
   60: }
   61: 
   62: sub end_radiobuttonresponse {
   63:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   64:   my $result;
   65:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   66:   &Apache::response::end_response;
   67:   pop @Apache::lonxml::namespace;
   68:   &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   69:   return $result;
   70: }
   71: 
   72: %Apache::response::foilgroup={};
   73: sub start_foilgroup {
   74:   %Apache::response::foilgroup={};
   75:   $Apache::radiobuttonresponse::conceptgroup=0;
   76:   &Apache::response::setrandomnumber();
   77:   return '';
   78: }
   79: 
   80: sub storesurvey {
   81:   if ( !defined($ENV{'form.submitted'})) { return ''; }
   82:   my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   83:   &Apache::lonxml::debug("Here I am!:$response:");
   84:   if ( $response !~ /[0-9]+/) { return ''; }
   85:   my $id = $Apache::inputtags::response['-1'];
   86:   my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
   87:   my %responsehash;
   88:   $responsehash{$whichfoils[$response]}=$response;
   89:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
   90:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
   91:   &Apache::lonxml::debug("submitted a $response<br />\n");
   92:   return '';
   93: }
   94: 
   95: sub grade_response {
   96:   my ($max,$answer)=@_;
   97:   if (!defined($ENV{'form.submitted'})) { return; }
   98:   my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   99:   if ( $response !~ /[0-9]+/) { return; }
  100:   my $part=$Apache::inputtags::part;
  101:   my $id = $Apache::inputtags::response['-1'];
  102:   my @whichfoils=&whichfoils($max,$answer);
  103:   my %responsehash;
  104:   $responsehash{$whichfoils[$response]}=$response;
  105:   my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  106:   my %previous=&Apache::response::check_for_previous($responsestr,
  107: 						     $part,$id);
  108:   $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  109:     $responsestr;
  110:   &Apache::lonxml::debug("submitted a $response<br />\n");
  111:   my $ad;
  112:   if ($response == $answer) {
  113:     $ad='EXACT_ANS';
  114:   } else {
  115:     $ad='INCORRECT';
  116:   }
  117:   $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  118:   &Apache::response::handle_previous(\%previous,$ad);
  119: }
  120: 
  121: sub end_foilgroup {
  122:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  123: 
  124:   my $result;
  125:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
  126:     my $style = $Apache::lonhomework::type;
  127:     if ( $style eq 'survey' ) {
  128:       if ($target eq 'web' || $target eq 'answer') {
  129: 	$result=&displayallfoils();
  130:       } elsif ( $target eq 'grade' ) {
  131: 	$result=&storesurvey();
  132:       }
  133:     } else {
  134:       my $name;
  135:       my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
  136:       my $count=0;
  137:       # we will add in 1 of the true statements
  138:       if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
  139:       my $answer = int(rand ($count));
  140:       &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
  141:       if ($target eq 'web') {
  142: 	$result=&displayfoils($max,$answer);
  143:       } elsif ($target eq 'answer' ) {
  144: 	$result=&displayanswers($max,$answer);
  145:       } elsif ( $target eq 'grade') {
  146: 	&grade_response($max,$answer);
  147:       }
  148:     }
  149:   }
  150:   return $result;
  151: }
  152: 
  153: sub getfoilcounts {
  154:   my ($parstack,$safeeval)=@_;
  155:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  156:   my @names;
  157:   my $truecnt=0;
  158:   my $falsecnt=0;
  159:   my $name;
  160:   if ( $Apache::response::foilgroup{'names'} ) {
  161:     @names= @{ $Apache::response::foilgroup{'names'} };
  162:   }
  163:   foreach $name (@names) {
  164:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  165:       $truecnt++;
  166:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  167:       $falsecnt++;
  168:     }
  169:   }
  170:   return ($truecnt,$falsecnt,$max);
  171: }
  172: 
  173: sub displayallfoils {
  174:   my $result;
  175:   &Apache::lonxml::debug("survey style display");
  176:   my @names = @{ $Apache::response::foilgroup{'names'} };
  177:   my $temp=0;
  178:   my $id=$Apache::inputtags::response['-1'];
  179:   my $part=$Apache::inputtags::part;
  180:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  181:   my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  182:   &Apache::lonhomework::showhash(%lastresponse);
  183:   foreach my $name (@names) {
  184:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  185:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  186:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  187:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  188:       $temp++;
  189:     }
  190:   }
  191:   return $result;
  192: }
  193: 
  194: sub whichfoils {
  195:   my ($max,$answer)=@_;
  196: 
  197:   my @truelist;
  198:   my @falselist;
  199: 
  200:   my @names;
  201:   if ( $Apache::response::foilgroup{'names'} ) {
  202:     @names= @{ $Apache::response::foilgroup{'names'} };
  203:   }
  204:   foreach my $name (@names) {
  205:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  206:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  207:       push (@truelist,$name);
  208:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  209:       push (@falselist,$name);
  210:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  211:     } else {
  212:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  213:     }
  214:   }
  215:   my $whichtrue = int(rand($#truelist+1));
  216:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  217:   my @whichfalse =();
  218:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
  219:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
  220:     my $afalse=int(rand($#falselist+1));
  221:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  222:     $afalse=splice(@falselist,$afalse,1);
  223:     &Apache::lonxml::debug("Picked $afalse");
  224:     push (@whichfalse,$afalse);
  225:   }
  226:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  227:   &Apache::lonxml::debug("the true statement is $answer");
  228:   return @whichfalse;
  229: }
  230: 
  231: sub displayfoils {
  232:   my ($max,$answer)=@_;
  233:   my $result;
  234: 
  235:   my @whichfoils=&whichfoils($max,$answer);
  236:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  237:     foreach my $name (@whichfoils) {
  238:       $result.="<br />";
  239:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  240: 	$result.='Correct';
  241:       } else {
  242: 	$result.='Incorrect';
  243:       }
  244:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  245:     }
  246:   } else {
  247:     my $temp=0;
  248:     my $id=$Apache::inputtags::response['-1'];
  249:     my $part=$Apache::inputtags::part;
  250:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  251:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  252:     foreach my $name (@whichfoils) {
  253:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  254:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  255:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  256:       $temp++;
  257:     }
  258:   }
  259:   return $result."<br />";
  260: }
  261: 
  262: sub displayanswers {
  263:   my ($max,$answer)=@_;
  264:   my @whichopt = &whichfoils($max,$answer);
  265:   my $result=&Apache::response::answer_header('radiobuttonresponse');
  266:   foreach my $name (@whichopt) {
  267:     $result.=&Apache::response::answer_part('radiobuttonresponse',
  268: 		     $Apache::response::foilgroup{$name.'.value'})
  269:   }
  270:   $result.=&Apache::response::answer_footer('radiobuttonresponse');
  271:   return $result;
  272: }
  273: 
  274: sub start_conceptgroup {
  275:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  276:   $Apache::radiobuttonresponse::conceptgroup=1;
  277:   %Apache::response::conceptgroup={};
  278:   my $result;
  279:   if ($target eq 'edit') {
  280:     $result.=&Apache::edit::tag_start($target,$token);
  281:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  282:       "</td></tr><tr><td colspan=\"3\">\n";
  283:   } elsif ($target eq 'modified') {
  284:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  285: 						 $safeeval,'concept');
  286:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  287:   }
  288:   return $result;
  289: }
  290: 
  291: sub end_conceptgroup {
  292:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  293:   $Apache::radiobuttonresponse::conceptgroup=0;
  294:   my $result;
  295:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  296:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  297:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  298:       my $pick=int(rand($#names+1));
  299:       my $name=$names[$pick];
  300:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  301:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  302:       $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
  303:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  304:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  305:       &Apache::lonxml::debug("Selecting $name in $concept");
  306:     }
  307:   } elsif ($target eq 'edit') {
  308:     $result=&Apache::edit::end_table();
  309:   }
  310:   return $result;
  311: }
  312: 
  313: sub insert_conceptgroup {
  314:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  315:   return $result;
  316: }
  317: 
  318: sub start_foil {
  319:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  320:   my $result='';
  321:   if ($target eq 'web') {
  322:     &Apache::lonxml::startredirection;
  323:   } elsif ($target eq 'edit') {
  324:     $result=&Apache::edit::tag_start($target,$token);
  325:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  326:     $result.=&Apache::edit::select_arg('Correct Option:','value',
  327: 				       ['unused','true','false'],$token);
  328:   } elsif ($target eq 'modified') {
  329:      my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  330: 						  'value','name');
  331:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  332:   }
  333:   return $result;
  334: }
  335: 
  336: sub end_foil {
  337:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  338:   my $text='';
  339:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  340:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  341:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  342:     if ($value ne 'unused') {
  343:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  344:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  345:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
  346: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  347: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  348: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  349:       } else {
  350: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  351: 	$Apache::response::foilgroup{"$name.value"} = $value;
  352: 	$Apache::response::foilgroup{"$name.text"} = $text;
  353:       }
  354:     }
  355:   }
  356:   return '';
  357: }
  358: 
  359: sub insert_foil {
  360:   return '
  361: <foil name="" value="unused">
  362: <startouttext />
  363: <endouttext />
  364: </foil>';
  365: }
  366: 1;
  367: __END__
  368:  

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