File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.20: download - view: text, annotated - select for diffs
Sat Jun 2 03:59:59 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added new arg $tagstack to all start_ or end_ routines

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: # 2/21 Guy
    4: 
    5: package Apache::radiobuttonresponse;
    6: use strict;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   10: }
   11: 
   12: sub start_radiobuttonresponse {
   13:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   14:   #when in a radiobutton response use these
   15:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   16:   my $id = &Apache::response::start_response($parstack,$safeeval);
   17:   return '';
   18: }
   19: 
   20: sub end_radiobuttonresponse {
   21:   &Apache::response::end_response;
   22:   return '';
   23: }
   24: 
   25: %Apache::response::foilgroup={};
   26: sub start_foilgroup {
   27:   %Apache::response::foilgroup={};
   28:   $Apache::radiobuttonresponse::conceptgroup=0;
   29:   &Apache::response::setrandomnumber();
   30:   return '';
   31: }
   32: 
   33: sub storesurvey {
   34:   if ( defined $ENV{'form.submitted'}) {
   35:     my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   36:     &Apache::lonxml::debug("Here I am!:$response:");
   37:     if ( $response =~ /[^\s]/) {
   38:       my $id = $Apache::inputtags::response['-1'];
   39:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   40:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
   41:       &Apache::lonxml::debug("submitted a $response<br />\n");
   42:     }
   43:   }
   44:   return '';
   45: }
   46: 
   47: sub end_foilgroup {
   48:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   49:   
   50:   my $result;
   51:   if ($target ne 'meta') {
   52:     my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval,'-2');
   53:     if ( $style eq 'survey' ) {
   54:       if ($target eq 'web') {
   55: 	$result=&displayallfoils();
   56:       } elsif ( $target eq 'grade' ) {
   57: 	$result=&storesurvey();
   58:       }
   59:     } else {
   60:       my $name;
   61:       my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
   62:       my $count=0;
   63:       # we will add in 1 of the true statements
   64:       if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
   65:       my $answer = int(rand ($count));
   66:       &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
   67:       if ($target eq 'web') {
   68: 	$result=&displayfoils($max,$answer);
   69:       } elsif ( $target eq 'grade') {
   70: 	if ( defined $ENV{'form.submitted'}) {
   71: 	  my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   72: 	  if ( $response =~ /[^\s]/) {
   73: 	    my $id = $Apache::inputtags::response['-1'];
   74: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   75: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
   76: 	    if ($response == $answer) {
   77: 	      $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
   78: 	    } else {
   79: 	      $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
   80: 	    }
   81: 	  }
   82: 	}
   83:       }
   84:     }
   85:   }
   86:   return $result;
   87: }
   88: 
   89: sub getfoilcounts {
   90:   my ($parstack,$safeeval)=@_;
   91:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
   92:   my @names = @{ $Apache::response::foilgroup{'names'} };
   93:   my $truecnt=0;
   94:   my $falsecnt=0;
   95:   my $name;
   96: 
   97:   foreach $name (@names) {
   98:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
   99:       $truecnt++;
  100:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  101:       $falsecnt++;
  102:     }
  103:   }
  104:   return ($truecnt,$falsecnt,$max);
  105: }
  106: 
  107: sub displayallfoils {
  108:   my $result;
  109:   &Apache::lonxml::debug("survey style display");
  110:   my @names = @{ $Apache::response::foilgroup{'names'} };
  111:   my $temp=0;
  112:   my $id=$Apache::inputtags::response['-1'];
  113:   my $part=$Apache::inputtags::part;
  114:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  115:   foreach my $name (@names) {
  116:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  117:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  118:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
  119:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  120:       $temp++;
  121:     }
  122:   }
  123:   return $result;
  124: }
  125: 
  126: sub displayfoils {
  127:   my ($max,$answer)=@_;
  128:   my @names = @{ $Apache::response::foilgroup{'names'} };
  129:   my @truelist;
  130:   my @falselist;
  131:   my $result;
  132:   my $name;
  133: 
  134:   foreach $name (@names) {
  135:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  136:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  137:       push (@truelist,$name);
  138:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  139:       push (@falselist,$name);
  140:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  141:     } else {
  142:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  143:     }
  144:   }
  145:   my $whichtrue = int(rand($#truelist+1));
  146:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  147:   my @whichfalse =();
  148:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
  149:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
  150:     my $afalse=int(rand($#falselist+1));
  151:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  152:     $afalse=splice(@falselist,$afalse,1);
  153:     &Apache::lonxml::debug("Picked $afalse");
  154:     push (@whichfalse,$afalse);
  155:   }
  156:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  157:   &Apache::lonxml::debug("the true statement is $answer");
  158:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  159:     foreach $name (@whichfalse) {
  160:       $result.="<br />";
  161:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  162: 	$result.='Correct';
  163:       } else {
  164: 	$result.='Incorrect';
  165:       }
  166:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  167:     }
  168:   } else {
  169:     my $temp=0;
  170:     my $id=$Apache::inputtags::response['-1'];
  171:     my $part=$Apache::inputtags::part;
  172:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  173:     foreach $name (@whichfalse) {
  174:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  175:       if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
  176:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  177:       $temp++;
  178:     }
  179:   }
  180:   return $result."<br />";
  181: }
  182: 
  183: sub start_conceptgroup {
  184:   $Apache::radiobuttonresponse::conceptgroup=1;
  185:   %Apache::response::conceptgroup={};
  186:   return '';
  187: }
  188: 
  189: sub end_conceptgroup {
  190:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  191:   $Apache::radiobuttonresponse::conceptgroup=0;
  192:   if ($target eq 'web' || $target eq 'grade') {
  193:     my @names = @{ $Apache::response::conceptgroup{'names'} };
  194:     my $pick=int rand $#names+1;
  195:     my $name=$names[$pick];
  196:     push @{ $Apache::response::foilgroup{'names'} }, $name;
  197:     $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  198:     $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
  199:     my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  200:     $Apache::response::foilgroup{"$name.concept"} = $concept;
  201:     &Apache::lonxml::debug("Selecting $name in $concept");
  202:   }
  203:   return '';
  204: }
  205: 
  206: sub start_foil {
  207:   &Apache::lonxml::startredirection;
  208:   return '';
  209: }
  210: 
  211: sub end_foil {
  212:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  213:   my $text='';
  214:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  215:   if ($target eq 'web' || $target eq 'grade') {
  216:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  217:     if ($value ne 'unused') {
  218:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  219:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
  220: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  221: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  222: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  223:       } else {
  224: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  225: 	$Apache::response::foilgroup{"$name.value"} = $value;
  226: 	$Apache::response::foilgroup{"$name.text"} = $text;
  227:       }
  228:     }
  229:   }
  230:   return '';
  231: }
  232: 
  233: 1;
  234: __END__
  235:  

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