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

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

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