Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.30

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
                      3: # 2/21 Guy
1.1       albertel    4: 
                      5: package Apache::radiobuttonresponse;
                      6: use strict;
                      7: 
                      8: sub BEGIN {
1.22      albertel    9:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1       albertel   10: }
                     11: 
                     12: sub start_radiobuttonresponse {
1.22      albertel   13:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26      albertel   14:   my $result;
1.3       albertel   15:   #when in a radiobutton response use these
1.22      albertel   16:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26      albertel   17:   push (@Apache::lonxml::namespace,'radiobuttonresponse');
1.4       albertel   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
1.25      albertel   19:   if ($target eq 'meta') {
                     20:     $result=&Apache::response::meta_package_write('radiobuttonresponse');
1.26      albertel   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); }
1.25      albertel   33:   }
                     34:   return $result;
1.1       albertel   35: }
                     36: 
                     37: sub end_radiobuttonresponse {
1.26      albertel   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     39:   my $result;
                     40:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.22      albertel   41:   &Apache::response::end_response;
1.26      albertel   42:   pop @Apache::lonxml::namespace;
                     43:   return $result;
1.1       albertel   44: }
                     45: 
1.22      albertel   46: %Apache::response::foilgroup={};
1.1       albertel   47: sub start_foilgroup {
1.22      albertel   48:   %Apache::response::foilgroup={};
                     49:   $Apache::radiobuttonresponse::conceptgroup=0;
                     50:   &Apache::response::setrandomnumber();
                     51:   return '';
1.5       albertel   52: }
                     53: 
1.15      albertel   54: sub storesurvey {
1.22      albertel   55:   if ( defined $ENV{'form.submitted'}) {
                     56:     my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     57:     &Apache::lonxml::debug("Here I am!:$response:");
1.30    ! albertel   58:     if ( $response =~ /[0-9]+/) {
1.22      albertel   59:       my $id = $Apache::inputtags::response['-1'];
1.30    ! albertel   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);
1.22      albertel   64:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
                     65:       &Apache::lonxml::debug("submitted a $response<br />\n");
1.15      albertel   66:     }
1.22      albertel   67:   }
                     68:   return '';
1.15      albertel   69: }
                     70: 
1.1       albertel   71: sub end_foilgroup {
1.22      albertel   72:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29      albertel   73: 
1.22      albertel   74:   my $result;
1.28      albertel   75:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
1.29      albertel   76:     my $style = $Apache::lonhomework::type;
1.22      albertel   77:     if ( $style eq 'survey' ) {
1.30    ! albertel   78:       if ($target eq 'web' || $target eq 'answer') {
1.22      albertel   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);
1.28      albertel   93:       } elsif ($target eq 'answer' ) {
                     94: 	$result=&displayanswers($max,$answer);
1.22      albertel   95:       } elsif ( $target eq 'grade') {
                     96: 	if ( defined $ENV{'form.submitted'}) {
                     97: 	  my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
1.30    ! albertel   98: 	  if ( $response =~ /[0-9]+/) {
1.22      albertel   99: 	    my $id = $Apache::inputtags::response['-1'];
1.30    ! albertel  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);
1.22      albertel  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';
1.15      albertel  109: 	    }
1.22      albertel  110: 	  }
1.5       albertel  111: 	}
1.22      albertel  112:       }
1.5       albertel  113:     }
1.22      albertel  114:   }
                    115:   return $result;
1.6       albertel  116: }
                    117: 
                    118: sub getfoilcounts {
1.22      albertel  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++;
1.6       albertel  131:     }
1.22      albertel  132:   }
                    133:   return ($truecnt,$falsecnt,$max);
1.5       albertel  134: }
                    135: 
1.15      albertel  136: sub displayallfoils {
1.22      albertel  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++;
1.15      albertel  150:     }
1.22      albertel  151:   }
                    152:   return $result;
1.15      albertel  153: }
                    154: 
1.28      albertel  155: sub whichfoils {
1.22      albertel  156:   my ($max,$answer)=@_;
1.28      albertel  157: 
1.22      albertel  158:   my @truelist;
                    159:   my @falselist;
                    160: 
1.28      albertel  161:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    162:   foreach my $name (@names) {
1.22      albertel  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>");
1.3       albertel  171:     }
1.22      albertel  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");
1.28      albertel  186:   return @whichfalse;
                    187: }
                    188: 
                    189: sub displayfoils {
                    190:   my ($max,$answer)=@_;
                    191:   my $result;
                    192: 
                    193:   my @whichfoils=&whichfoils($max,$answer);
1.22      albertel  194:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.28      albertel  195:     foreach my $name (@whichfoils) {
1.22      albertel  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";
1.10      albertel  203:     }
1.22      albertel  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"};
1.28      albertel  209:     foreach my $name (@whichfoils) {
1.22      albertel  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++;
1.10      albertel  214:     }
1.22      albertel  215:   }
                    216:   return $result."<br />";
1.14      albertel  217: }
                    218: 
1.28      albertel  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: 
1.14      albertel  232: sub start_conceptgroup {
1.27      albertel  233:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22      albertel  234:   $Apache::radiobuttonresponse::conceptgroup=1;
                    235:   %Apache::response::conceptgroup={};
1.26      albertel  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;
1.14      albertel  247: }
                    248: 
                    249: sub end_conceptgroup {
1.22      albertel  250:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    251:   $Apache::radiobuttonresponse::conceptgroup=0;
1.26      albertel  252:   my $result;
1.28      albertel  253:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.27      albertel  254:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    255:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.28      albertel  256:       my $pick=int(rand($#names+1));
1.27      albertel  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:     }
1.26      albertel  265:   } elsif ($target eq 'edit') {
                    266:     $result=&Apache::edit::end_table();
1.22      albertel  267:   }
1.26      albertel  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;
1.1       albertel  274: }
                    275: 
                    276: sub start_foil {
1.24      albertel  277:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    278:   my $result='';
1.27      albertel  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',
1.28      albertel  285: 				       ['unused','true','false'],$token);
1.27      albertel  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;
1.1       albertel  292: }
                    293: 
                    294: sub end_foil {
1.22      albertel  295:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    296:   my $text='';
                    297:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.28      albertel  298:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.19      albertel  299:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  300:     if ($value ne 'unused') {
1.19      albertel  301:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.27      albertel  302:       if (!$name) { $name=$Apache::lonxml::curdepth; }
1.18      albertel  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:     }
1.4       albertel  313:   }
1.1       albertel  314:   return '';
                    315: }
                    316: 
1.27      albertel  317: sub insert_foil {
                    318:   return '
                    319: <foil name="" value="unused">
                    320: <startouttext />
                    321: <endouttext />
                    322: </foil>';
                    323: }
1.1       albertel  324: 1;
                    325: __END__
                    326:  

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