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

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.22      albertel  205:   $Apache::radiobuttonresponse::conceptgroup=1;
                    206:   %Apache::response::conceptgroup={};
1.26    ! albertel  207:   my $result;
        !           208:   if ($target eq 'edit') {
        !           209:     $result.=&Apache::edit::tag_start($target,$token);
        !           210:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
        !           211:       "</td></tr><tr><td colspan=\"3\">\n";
        !           212:   } elsif ($target eq 'modified') {
        !           213:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
        !           214: 						 $safeeval,'concept');
        !           215:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
        !           216:   }
        !           217:   return $result;
1.14      albertel  218: }
                    219: 
                    220: sub end_conceptgroup {
1.22      albertel  221:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    222:   $Apache::radiobuttonresponse::conceptgroup=0;
1.26    ! albertel  223:   my $result;
1.22      albertel  224:   if ($target eq 'web' || $target eq 'grade') {
                    225:     my @names = @{ $Apache::response::conceptgroup{'names'} };
                    226:     my $pick=int rand $#names+1;
                    227:     my $name=$names[$pick];
                    228:     push @{ $Apache::response::foilgroup{'names'} }, $name;
                    229:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
                    230:     $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
                    231:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
                    232:     $Apache::response::foilgroup{"$name.concept"} = $concept;
                    233:     &Apache::lonxml::debug("Selecting $name in $concept");
1.26    ! albertel  234:   } elsif ($target eq 'edit') {
        !           235:     $result=&Apache::edit::end_table();
1.22      albertel  236:   }
1.26    ! albertel  237:   return $result;
        !           238: }
        !           239: 
        !           240: sub insert_conceptgroup {
        !           241:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
        !           242:   return $result;
1.1       albertel  243: }
                    244: 
                    245: sub start_foil {
1.24      albertel  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    247:   my $result='';
                    248:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.22      albertel  249:   return '';
1.1       albertel  250: }
                    251: 
                    252: sub end_foil {
1.22      albertel  253:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                    254:   my $text='';
                    255:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
                    256:   if ($target eq 'web' || $target eq 'grade') {
1.19      albertel  257:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  258:     if ($value ne 'unused') {
1.19      albertel  259:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.18      albertel  260:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
                    261: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
                    262: 	$Apache::response::conceptgroup{"$name.value"} = $value;
                    263: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
                    264:       } else {
                    265: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
                    266: 	$Apache::response::foilgroup{"$name.value"} = $value;
                    267: 	$Apache::response::foilgroup{"$name.text"} = $text;
                    268:       }
                    269:     }
1.4       albertel  270:   }
1.1       albertel  271:   return '';
                    272: }
                    273: 
                    274: 1;
                    275: __END__
                    276:  

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