File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.27: download - view: text, annotated - select for diffs
Wed Nov 7 22:28:44 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- edit mode works on rbresponse

    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:   my $result;
   15:   #when in a radiobutton response use these
   16:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   17:   push (@Apache::lonxml::namespace,'radiobuttonresponse');
   18:   my $id = &Apache::response::start_response($parstack,$safeeval);
   19:   if ($target eq 'meta') {
   20:     $result=&Apache::response::meta_package_write('radiobuttonresponse');
   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); }
   33:   }
   34:   return $result;
   35: }
   36: 
   37: sub end_radiobuttonresponse {
   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   39:   my $result;
   40:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   41:   &Apache::response::end_response;
   42:   pop @Apache::lonxml::namespace;
   43:   return $result;
   44: }
   45: 
   46: %Apache::response::foilgroup={};
   47: sub start_foilgroup {
   48:   %Apache::response::foilgroup={};
   49:   $Apache::radiobuttonresponse::conceptgroup=0;
   50:   &Apache::response::setrandomnumber();
   51:   return '';
   52: }
   53: 
   54: sub storesurvey {
   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");
   63:     }
   64:   }
   65:   return '';
   66: }
   67: 
   68: sub end_foilgroup {
   69:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   70:   
   71:   my $result;
   72:   if ($target eq 'grade' || $target eq 'web') {
   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';
  101: 	    }
  102: 	  }
  103: 	}
  104:       }
  105:     }
  106:   }
  107:   return $result;
  108: }
  109: 
  110: sub getfoilcounts {
  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++;
  123:     }
  124:   }
  125:   return ($truecnt,$falsecnt,$max);
  126: }
  127: 
  128: sub displayallfoils {
  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++;
  142:     }
  143:   }
  144:   return $result;
  145: }
  146: 
  147: sub displayfoils {
  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>");
  164:     }
  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";
  188:     }
  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++;
  199:     }
  200:   }
  201:   return $result."<br />";
  202: }
  203: 
  204: sub start_conceptgroup {
  205:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  206:   $Apache::radiobuttonresponse::conceptgroup=1;
  207:   %Apache::response::conceptgroup={};
  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;
  219: }
  220: 
  221: sub end_conceptgroup {
  222:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  223:   $Apache::radiobuttonresponse::conceptgroup=0;
  224:   my $result;
  225:   if ($target eq 'web' || $target eq 'grade') {
  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:     }
  237:   } elsif ($target eq 'edit') {
  238:     $result=&Apache::edit::end_table();
  239:   }
  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;
  246: }
  247: 
  248: sub start_foil {
  249:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  250:   my $result='';
  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;
  264: }
  265: 
  266: sub end_foil {
  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') {
  271:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  272:     if ($value ne 'unused') {
  273:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  274:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  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:     }
  285:   }
  286:   return '';
  287: }
  288: 
  289: sub insert_foil {
  290:   return '
  291: <foil name="" value="unused">
  292: <startouttext />
  293: <endouttext />
  294: </foil>';
  295: }
  296: 1;
  297: __END__
  298:  

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