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

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

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