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

1.22      albertel    1: # The LearningOnline Network with CAPA
                      2: # mutliple choice style responses
1.31      albertel    3: #
1.35    ! matthew     4: # $Id: radiobuttonresponse.pm,v 1.34 2002/01/07 18:03:57 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).
1.35    ! matthew    48: 	'<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
1.26      albertel   49: 	&Apache::edit::deletelist($target,$token)
1.35    ! matthew    50: 	."</td><td>&nbsp".&Apache::edit::end_row()
        !            51:         .&Apache::edit::start_spanning_row();
        !            52: 
1.26      albertel   53:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
1.35    ! matthew    54: 	&Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
1.26      albertel   55:   } elsif ($target eq 'modified') {
                     56:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                     57: 						 $safeeval,'max');
                     58:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.25      albertel   59:   }
                     60:   return $result;
1.1       albertel   61: }
                     62: 
                     63: sub end_radiobuttonresponse {
1.26      albertel   64:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     65:   my $result;
                     66:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.22      albertel   67:   &Apache::response::end_response;
1.26      albertel   68:   pop @Apache::lonxml::namespace;
1.33      albertel   69:   &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26      albertel   70:   return $result;
1.1       albertel   71: }
                     72: 
1.22      albertel   73: %Apache::response::foilgroup={};
1.1       albertel   74: sub start_foilgroup {
1.22      albertel   75:   %Apache::response::foilgroup={};
                     76:   $Apache::radiobuttonresponse::conceptgroup=0;
                     77:   &Apache::response::setrandomnumber();
                     78:   return '';
1.5       albertel   79: }
                     80: 
1.15      albertel   81: sub storesurvey {
1.33      albertel   82:   if ( !defined($ENV{'form.submitted'})) { return ''; }
                     83:   my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
                     84:   &Apache::lonxml::debug("Here I am!:$response:");
                     85:   if ( $response !~ /[0-9]+/) { return ''; }
                     86:   my $id = $Apache::inputtags::response['-1'];
                     87:   my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
                     88:   my %responsehash;
                     89:   $responsehash{$whichfoils[$response]}=$response;
                     90:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
                     91:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
                     92:   &Apache::lonxml::debug("submitted a $response<br />\n");
1.22      albertel   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');
1.34      albertel  157:   my @names;
1.22      albertel  158:   my $truecnt=0;
                    159:   my $falsecnt=0;
                    160:   my $name;
1.34      albertel  161:   if ( $Apache::response::foilgroup{'names'} ) {
                    162:     @names= @{ $Apache::response::foilgroup{'names'} };
                    163:   }
1.22      albertel  164:   foreach $name (@names) {
                    165:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    166:       $truecnt++;
                    167:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    168:       $falsecnt++;
1.6       albertel  169:     }
1.22      albertel  170:   }
                    171:   return ($truecnt,$falsecnt,$max);
1.5       albertel  172: }
                    173: 
1.15      albertel  174: sub displayallfoils {
1.22      albertel  175:   my $result;
                    176:   &Apache::lonxml::debug("survey style display");
                    177:   my @names = @{ $Apache::response::foilgroup{'names'} };
                    178:   my $temp=0;
                    179:   my $id=$Apache::inputtags::response['-1'];
                    180:   my $part=$Apache::inputtags::part;
                    181:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32      albertel  182:   my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
                    183:   &Apache::lonhomework::showhash(%lastresponse);
1.22      albertel  184:   foreach my $name (@names) {
                    185:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
                    186:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.32      albertel  187:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.22      albertel  188:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    189:       $temp++;
1.15      albertel  190:     }
1.22      albertel  191:   }
                    192:   return $result;
1.15      albertel  193: }
                    194: 
1.28      albertel  195: sub whichfoils {
1.22      albertel  196:   my ($max,$answer)=@_;
1.28      albertel  197: 
1.22      albertel  198:   my @truelist;
                    199:   my @falselist;
                    200: 
1.34      albertel  201:   my @names;
                    202:   if ( $Apache::response::foilgroup{'names'} ) {
                    203:     @names= @{ $Apache::response::foilgroup{'names'} };
                    204:   }
1.28      albertel  205:   foreach my $name (@names) {
1.22      albertel  206:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
                    207:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
                    208:       push (@truelist,$name);
                    209:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
                    210:       push (@falselist,$name);
                    211:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
                    212:     } else {
                    213:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3       albertel  214:     }
1.22      albertel  215:   }
                    216:   my $whichtrue = int(rand($#truelist+1));
                    217:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
                    218:   my @whichfalse =();
                    219:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
                    220:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
                    221:     my $afalse=int(rand($#falselist+1));
                    222:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
                    223:     $afalse=splice(@falselist,$afalse,1);
                    224:     &Apache::lonxml::debug("Picked $afalse");
                    225:     push (@whichfalse,$afalse);
                    226:   }
                    227:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
                    228:   &Apache::lonxml::debug("the true statement is $answer");
1.28      albertel  229:   return @whichfalse;
                    230: }
                    231: 
                    232: sub displayfoils {
                    233:   my ($max,$answer)=@_;
                    234:   my $result;
                    235: 
                    236:   my @whichfoils=&whichfoils($max,$answer);
1.22      albertel  237:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.28      albertel  238:     foreach my $name (@whichfoils) {
1.22      albertel  239:       $result.="<br />";
                    240:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
                    241: 	$result.='Correct';
                    242:       } else {
                    243: 	$result.='Incorrect';
                    244:       }
                    245:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
1.10      albertel  246:     }
1.22      albertel  247:   } else {
                    248:     my $temp=0;
                    249:     my $id=$Apache::inputtags::response['-1'];
                    250:     my $part=$Apache::inputtags::part;
                    251:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32      albertel  252:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.28      albertel  253:     foreach my $name (@whichfoils) {
1.22      albertel  254:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.32      albertel  255:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.22      albertel  256:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
                    257:       $temp++;
1.10      albertel  258:     }
1.22      albertel  259:   }
                    260:   return $result."<br />";
1.14      albertel  261: }
                    262: 
1.28      albertel  263: sub displayanswers {
                    264:   my ($max,$answer)=@_;
                    265:   my @whichopt = &whichfoils($max,$answer);
                    266:   my $result=&Apache::response::answer_header('radiobuttonresponse');
                    267:   foreach my $name (@whichopt) {
                    268:     $result.=&Apache::response::answer_part('radiobuttonresponse',
                    269: 		     $Apache::response::foilgroup{$name.'.value'})
                    270:   }
                    271:   $result.=&Apache::response::answer_footer('radiobuttonresponse');
                    272:   return $result;
                    273: }
                    274: 
1.14      albertel  275: sub start_conceptgroup {
1.27      albertel  276:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22      albertel  277:   $Apache::radiobuttonresponse::conceptgroup=1;
                    278:   %Apache::response::conceptgroup={};
1.26      albertel  279:   my $result;
                    280:   if ($target eq 'edit') {
                    281:     $result.=&Apache::edit::tag_start($target,$token);
                    282:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
1.35    ! matthew   283: 	&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.26      albertel  284:   } elsif ($target eq 'modified') {
                    285:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    286: 						 $safeeval,'concept');
                    287:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    288:   }
                    289:   return $result;
1.14      albertel  290: }
                    291: 
                    292: sub end_conceptgroup {
1.22      albertel  293:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    294:   $Apache::radiobuttonresponse::conceptgroup=0;
1.26      albertel  295:   my $result;
1.28      albertel  296:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.27      albertel  297:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
                    298:       my @names = @{ $Apache::response::conceptgroup{'names'} };
1.28      albertel  299:       my $pick=int(rand($#names+1));
1.27      albertel  300:       my $name=$names[$pick];
                    301:       push @{ $Apache::response::foilgroup{'names'} }, $name;
                    302:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
                    303:       $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
                    304:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    305:       $Apache::response::foilgroup{"$name.concept"} = $concept;
                    306:       &Apache::lonxml::debug("Selecting $name in $concept");
                    307:     }
1.26      albertel  308:   } elsif ($target eq 'edit') {
                    309:     $result=&Apache::edit::end_table();
1.22      albertel  310:   }
1.26      albertel  311:   return $result;
                    312: }
                    313: 
                    314: sub insert_conceptgroup {
                    315:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
                    316:   return $result;
1.1       albertel  317: }
                    318: 
                    319: sub start_foil {
1.24      albertel  320:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    321:   my $result='';
1.27      albertel  322:   if ($target eq 'web') {
                    323:     &Apache::lonxml::startredirection;
                    324:   } elsif ($target eq 'edit') {
                    325:     $result=&Apache::edit::tag_start($target,$token);
                    326:     $result.=&Apache::edit::text_arg('Name:','name',$token);
                    327:     $result.=&Apache::edit::select_arg('Correct Option:','value',
1.28      albertel  328: 				       ['unused','true','false'],$token);
1.35    ! matthew   329:     $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.27      albertel  330:   } elsif ($target eq 'modified') {
                    331:      my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
                    332: 						  'value','name');
                    333:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    334:   }
                    335:   return $result;
1.1       albertel  336: }
                    337: 
                    338: sub end_foil {
1.22      albertel  339:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    340:   my $text='';
                    341:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.28      albertel  342:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.19      albertel  343:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  344:     if ($value ne 'unused') {
1.19      albertel  345:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.27      albertel  346:       if (!$name) { $name=$Apache::lonxml::curdepth; }
1.18      albertel  347:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
                    348: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    349: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    350: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
                    351:       } else {
                    352: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    353: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    354: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    355:       }
                    356:     }
1.4       albertel  357:   }
1.1       albertel  358:   return '';
                    359: }
                    360: 
1.27      albertel  361: sub insert_foil {
                    362:   return '
                    363: <foil name="" value="unused">
                    364: <startouttext />
                    365: <endouttext />
                    366: </foil>';
                    367: }
1.1       albertel  368: 1;
                    369: __END__
                    370:  

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