File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.26: download - view: text, annotated - select for diffs
Wed Nov 7 21:23:15 2001 UTC (22 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding edit mode stuff

    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:   $Apache::radiobuttonresponse::conceptgroup=1;
  206:   %Apache::response::conceptgroup={};
  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;
  218: }
  219: 
  220: sub end_conceptgroup {
  221:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  222:   $Apache::radiobuttonresponse::conceptgroup=0;
  223:   my $result;
  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");
  234:   } elsif ($target eq 'edit') {
  235:     $result=&Apache::edit::end_table();
  236:   }
  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;
  243: }
  244: 
  245: sub start_foil {
  246:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  247:   my $result='';
  248:   if ($target eq 'web') { &Apache::lonxml::startredirection; }
  249:   return '';
  250: }
  251: 
  252: sub end_foil {
  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') {
  257:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  258:     if ($value ne 'unused') {
  259:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  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:     }
  270:   }
  271:   return '';
  272: }
  273: 
  274: 1;
  275: __END__
  276:  

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