File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.41: download - view: text, annotated - select for diffs
Fri May 3 20:13:14 2002 UTC (22 years ago) by albertel
Branches: MAIN
CVS tags: version_0_4, stable_2002_july, STABLE, HEAD
- implementing BUG#238 fro radiobuttonresponse

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

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