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

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

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