File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.42: download - view: text, annotated - select for diffs
Wed Jul 17 18:06:18 2002 UTC (21 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- supports variables in the possible args to a foil for a rbr type question BUG#584

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

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