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

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:");
                     58:     if ( $response =~ /[^\s]/) {
                     59:       my $id = $Apache::inputtags::response['-1'];
                     60:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
                     61:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
                     62:       &Apache::lonxml::debug("submitted a $response<br />\n");
1.15      albertel   63:     }
1.22      albertel   64:   }
                     65:   return '';
1.15      albertel   66: }
                     67: 
1.1       albertel   68: sub end_foilgroup {
1.22      albertel   69:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     70:   
                     71:   my $result;
1.28    ! albertel   72:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
1.22      albertel   73:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval,'-2');
                     74:     if ( $style eq 'survey' ) {
                     75:       if ($target eq 'web') {
                     76: 	$result=&displayallfoils();
                     77:       } elsif ( $target eq 'grade' ) {
                     78: 	$result=&storesurvey();
                     79:       }
                     80:     } else {
                     81:       my $name;
                     82:       my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
                     83:       my $count=0;
                     84:       # we will add in 1 of the true statements
                     85:       if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
                     86:       my $answer = int(rand ($count));
                     87:       &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
                     88:       if ($target eq 'web') {
                     89: 	$result=&displayfoils($max,$answer);
1.28    ! albertel   90:       } elsif ($target eq 'answer' ) {
        !            91: 	$result=&displayanswers($max,$answer);
1.22      albertel   92:       } elsif ( $target eq 'grade') {
                     93: 	if ( defined $ENV{'form.submitted'}) {
                     94: 	  my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     95: 	  if ( $response =~ /[^\s]/) {
                     96: 	    my $id = $Apache::inputtags::response['-1'];
                     97: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
                     98: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
                     99: 	    if ($response == $answer) {
                    100: 	      $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
                    101: 	    } else {
                    102: 	      $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
1.15      albertel  103: 	    }
1.22      albertel  104: 	  }
1.5       albertel  105: 	}
1.22      albertel  106:       }
1.5       albertel  107:     }
1.22      albertel  108:   }
                    109:   return $result;
1.6       albertel  110: }
                    111: 
                    112: sub getfoilcounts {
1.22      albertel  113:   my ($parstack,$safeeval)=@_;
                    114:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
                    115:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    116:   my $truecnt=0;
                    117:   my $falsecnt=0;
                    118:   my $name;
                    119: 
                    120:   foreach $name (@names) {
                    121:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    122:       $truecnt++;
                    123:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    124:       $falsecnt++;
1.6       albertel  125:     }
1.22      albertel  126:   }
                    127:   return ($truecnt,$falsecnt,$max);
1.5       albertel  128: }
                    129: 
1.15      albertel  130: sub displayallfoils {
1.22      albertel  131:   my $result;
                    132:   &Apache::lonxml::debug("survey style display");
                    133:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    134:   my $temp=0;
                    135:   my $id=$Apache::inputtags::response['-1'];
                    136:   my $part=$Apache::inputtags::part;
                    137:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
                    138:   foreach my $name (@names) {
                    139:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
                    140:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
                    141:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
                    142:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    143:       $temp++;
1.15      albertel  144:     }
1.22      albertel  145:   }
                    146:   return $result;
1.15      albertel  147: }
                    148: 
1.28    ! albertel  149: sub whichfoils {
1.22      albertel  150:   my ($max,$answer)=@_;
1.28    ! albertel  151: 
1.22      albertel  152:   my @truelist;
                    153:   my @falselist;
                    154: 
1.28    ! albertel  155:   my @names = @{ $Apache::response::foilgroup{'names'} };
        !           156:   foreach my $name (@names) {
1.22      albertel  157:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
                    158:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    159:       push (@truelist,$name);
                    160:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    161:       push (@falselist,$name);
                    162:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    163:     } else {
                    164:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  165:     }
1.22      albertel  166:   }
                    167:   my $whichtrue = int(rand($#truelist+1));
                    168:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    169:   my @whichfalse =();
                    170:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
                    171:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    172:     my $afalse=int(rand($#falselist+1));
                    173:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    174:     $afalse=splice(@falselist,$afalse,1);
                    175:     &Apache::lonxml::debug("Picked $afalse");
                    176:     push (@whichfalse,$afalse);
                    177:   }
                    178:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
                    179:   &Apache::lonxml::debug("the true statement is $answer");
1.28    ! albertel  180:   return @whichfalse;
        !           181: }
        !           182: 
        !           183: sub displayfoils {
        !           184:   my ($max,$answer)=@_;
        !           185:   my $result;
        !           186: 
        !           187:   my @whichfoils=&whichfoils($max,$answer);
1.22      albertel  188:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.28    ! albertel  189:     foreach my $name (@whichfoils) {
1.22      albertel  190:       $result.="<br />";
                    191:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    192: 	$result.='Correct';
                    193:       } else {
                    194: 	$result.='Incorrect';
                    195:       }
                    196:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
1.10      albertel  197:     }
1.22      albertel  198:   } else {
                    199:     my $temp=0;
                    200:     my $id=$Apache::inputtags::response['-1'];
                    201:     my $part=$Apache::inputtags::part;
                    202:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.28    ! albertel  203:     foreach my $name (@whichfoils) {
1.22      albertel  204:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
                    205:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
                    206:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    207:       $temp++;
1.10      albertel  208:     }
1.22      albertel  209:   }
                    210:   return $result."<br />";
1.14      albertel  211: }
                    212: 
1.28    ! albertel  213: sub displayanswers {
        !           214:   my ($max,$answer)=@_;
        !           215:   my @names = @{ $Apache::response::foilgroup{'names'} };
        !           216:   my @whichopt = &whichfoils($max,$answer);
        !           217:   my $result=&Apache::response::answer_header('radiobuttonresponse');
        !           218:   foreach my $name (@whichopt) {
        !           219:     $result.=&Apache::response::answer_part('radiobuttonresponse',
        !           220: 		     $Apache::response::foilgroup{$name.'.value'})
        !           221:   }
        !           222:   $result.=&Apache::response::answer_footer('radiobuttonresponse');
        !           223:   return $result;
        !           224: }
        !           225: 
1.14      albertel  226: sub start_conceptgroup {
1.27      albertel  227:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22      albertel  228:   $Apache::radiobuttonresponse::conceptgroup=1;
                    229:   %Apache::response::conceptgroup={};
1.26      albertel  230:   my $result;
                    231:   if ($target eq 'edit') {
                    232:     $result.=&Apache::edit::tag_start($target,$token);
                    233:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
                    234:       "</td></tr><tr><td colspan=\"3\">\n";
                    235:   } elsif ($target eq 'modified') {
                    236:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    237: 						 $safeeval,'concept');
                    238:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    239:   }
                    240:   return $result;
1.14      albertel  241: }
                    242: 
                    243: sub end_conceptgroup {
1.22      albertel  244:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    245:   $Apache::radiobuttonresponse::conceptgroup=0;
1.26      albertel  246:   my $result;
1.28    ! albertel  247:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.27      albertel  248:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    249:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.28    ! albertel  250:       my $pick=int(rand($#names+1));
1.27      albertel  251:       my $name=$names[$pick];
                    252:       push @{ $Apache::response::foilgroup{'names'} }, $name;
                    253:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
                    254:       $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
                    255:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    256:       $Apache::response::foilgroup{"$name.concept"} = $concept;
                    257:       &Apache::lonxml::debug("Selecting $name in $concept");
                    258:     }
1.26      albertel  259:   } elsif ($target eq 'edit') {
                    260:     $result=&Apache::edit::end_table();
1.22      albertel  261:   }
1.26      albertel  262:   return $result;
                    263: }
                    264: 
                    265: sub insert_conceptgroup {
                    266:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    267:   return $result;
1.1       albertel  268: }
                    269: 
                    270: sub start_foil {
1.24      albertel  271:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    272:   my $result='';
1.27      albertel  273:   if ($target eq 'web') {
                    274:     &Apache::lonxml::startredirection;
                    275:   } elsif ($target eq 'edit') {
                    276:     $result=&Apache::edit::tag_start($target,$token);
                    277:     $result.=&Apache::edit::text_arg('Name:','name',$token);
                    278:     $result.=&Apache::edit::select_arg('Correct Option:','value',
1.28    ! albertel  279: 				       ['unused','true','false'],$token);
1.27      albertel  280:   } elsif ($target eq 'modified') {
                    281:      my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    282: 						  'value','name');
                    283:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    284:   }
                    285:   return $result;
1.1       albertel  286: }
                    287: 
                    288: sub end_foil {
1.22      albertel  289:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    290:   my $text='';
                    291:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.28    ! albertel  292:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.19      albertel  293:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  294:     if ($value ne 'unused') {
1.19      albertel  295:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.27      albertel  296:       if (!$name) { $name=$Apache::lonxml::curdepth; }
1.18      albertel  297:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
                    298: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    299: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    300: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
                    301:       } else {
                    302: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    303: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    304: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    305:       }
                    306:     }
1.4       albertel  307:   }
1.1       albertel  308:   return '';
                    309: }
                    310: 
1.27      albertel  311: sub insert_foil {
                    312:   return '
                    313: <foil name="" value="unused">
                    314: <startouttext />
                    315: <endouttext />
                    316: </foil>';
                    317: }
1.1       albertel  318: 1;
                    319: __END__
                    320:  

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