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

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

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