File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.29: download - view: text, annotated - select for diffs
Mon Nov 12 20:36:41 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
- survey mode now triggered by being a .survey file or resource.0.type being
    set to "survey"

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

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