File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.30: download - view: text, annotated - select for diffs
Tue Dec 4 14:57:31 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Broken version, attempitng to improve the structture of the submissions data stored.

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

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