File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.32: download - view: text, annotated - select for diffs
Thu Dec 13 23:36:39 2001 UTC (22 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- detects previous submissions
- detects blank submissions

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.32 2001/12/13 23:36:39 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 2/21 Guy
   29: 
   30: package Apache::radiobuttonresponse;
   31: use strict;
   32: 
   33: sub BEGIN {
   34:   &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
   35: }
   36: 
   37: sub start_radiobuttonresponse {
   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   39:   my $result;
   40:   #when in a radiobutton response use these
   41:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   42:   push (@Apache::lonxml::namespace,'radiobuttonresponse');
   43:   my $id = &Apache::response::start_response($parstack,$safeeval);
   44:   if ($target eq 'meta') {
   45:     $result=&Apache::response::meta_package_write('radiobuttonresponse');
   46:   } elsif ($target eq 'edit' ) {
   47:     $result.=&Apache::edit::start_table($token).
   48:       '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
   49: 	&Apache::edit::deletelist($target,$token)
   50: 	  ."</td><td>&nbsp</td></tr><tr><td colspan=\"3\">\n";
   51:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
   52:       "</td></tr>";
   53:     $result.="<tr><td colspan=\"3\">\n";
   54:   } elsif ($target eq 'modified') {
   55:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   56: 						 $safeeval,'max');
   57:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   58:   }
   59:   return $result;
   60: }
   61: 
   62: sub end_radiobuttonresponse {
   63:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   64:   my $result;
   65:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   66:   &Apache::response::end_response;
   67:   pop @Apache::lonxml::namespace;
   68:   return $result;
   69: }
   70: 
   71: %Apache::response::foilgroup={};
   72: sub start_foilgroup {
   73:   %Apache::response::foilgroup={};
   74:   $Apache::radiobuttonresponse::conceptgroup=0;
   75:   &Apache::response::setrandomnumber();
   76:   return '';
   77: }
   78: 
   79: sub storesurvey {
   80:   if ( defined $ENV{'form.submitted'}) {
   81:     my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   82:     &Apache::lonxml::debug("Here I am!:$response:");
   83:     if ( $response =~ /[0-9]+/) {
   84:       my $id = $Apache::inputtags::response['-1'];
   85:       my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
   86:       my %responsehash;
   87:       $responsehash{$whichfoils[$response]}=$response;
   88:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
   89:       $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
   90:       &Apache::lonxml::debug("submitted a $response<br />\n");
   91:     }
   92:   }
   93:   return '';
   94: }
   95: 
   96: sub grade_response {
   97:   my ($max,$answer)=@_;
   98:   if (!defined($ENV{'form.submitted'})) { return; }
   99:   my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
  100:   if ( $response !~ /[0-9]+/) { return; }
  101:   my $part=$Apache::inputtags::part;
  102:   my $id = $Apache::inputtags::response['-1'];
  103:   my @whichfoils=&whichfoils($max,$answer);
  104:   my %responsehash;
  105:   $responsehash{$whichfoils[$response]}=$response;
  106:   my $responsestr=&Apache::lonnet::hash2str(%responsehash);
  107:   my %previous=&Apache::response::check_for_previous($responsestr,
  108: 						     $part,$id);
  109:   $Apache::lonhomework::results{"resource.$part.$id.submission"}=
  110:     $responsestr;
  111:   &Apache::lonxml::debug("submitted a $response<br />\n");
  112:   my $ad;
  113:   if ($response == $answer) {
  114:     $ad='EXACT_ANS';
  115:   } else {
  116:     $ad='INCORRECT';
  117:   }
  118:   $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
  119:   &Apache::response::handle_previous(\%previous,$ad);
  120: }
  121: 
  122: sub end_foilgroup {
  123:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  124: 
  125:   my $result;
  126:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
  127:     my $style = $Apache::lonhomework::type;
  128:     if ( $style eq 'survey' ) {
  129:       if ($target eq 'web' || $target eq 'answer') {
  130: 	$result=&displayallfoils();
  131:       } elsif ( $target eq 'grade' ) {
  132: 	$result=&storesurvey();
  133:       }
  134:     } else {
  135:       my $name;
  136:       my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
  137:       my $count=0;
  138:       # we will add in 1 of the true statements
  139:       if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
  140:       my $answer = int(rand ($count));
  141:       &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
  142:       if ($target eq 'web') {
  143: 	$result=&displayfoils($max,$answer);
  144:       } elsif ($target eq 'answer' ) {
  145: 	$result=&displayanswers($max,$answer);
  146:       } elsif ( $target eq 'grade') {
  147: 	&grade_response($max,$answer);
  148:       }
  149:     }
  150:   }
  151:   return $result;
  152: }
  153: 
  154: sub getfoilcounts {
  155:   my ($parstack,$safeeval)=@_;
  156:   my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  157:   my @names = @{ $Apache::response::foilgroup{'names'} };
  158:   my $truecnt=0;
  159:   my $falsecnt=0;
  160:   my $name;
  161: 
  162:   foreach $name (@names) {
  163:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  164:       $truecnt++;
  165:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  166:       $falsecnt++;
  167:     }
  168:   }
  169:   return ($truecnt,$falsecnt,$max);
  170: }
  171: 
  172: sub displayallfoils {
  173:   my $result;
  174:   &Apache::lonxml::debug("survey style display");
  175:   my @names = @{ $Apache::response::foilgroup{'names'} };
  176:   my $temp=0;
  177:   my $id=$Apache::inputtags::response['-1'];
  178:   my $part=$Apache::inputtags::part;
  179:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  180:   my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  181:   &Apache::lonhomework::showhash(%lastresponse);
  182:   foreach my $name (@names) {
  183:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  184:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  185:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  186:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  187:       $temp++;
  188:     }
  189:   }
  190:   return $result;
  191: }
  192: 
  193: sub whichfoils {
  194:   my ($max,$answer)=@_;
  195: 
  196:   my @truelist;
  197:   my @falselist;
  198: 
  199:   my @names = @{ $Apache::response::foilgroup{'names'} };
  200:   foreach my $name (@names) {
  201:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  202:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  203:       push (@truelist,$name);
  204:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  205:       push (@falselist,$name);
  206:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  207:     } else {
  208:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  209:     }
  210:   }
  211:   my $whichtrue = int(rand($#truelist+1));
  212:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  213:   my @whichfalse =();
  214:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
  215:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
  216:     my $afalse=int(rand($#falselist+1));
  217:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  218:     $afalse=splice(@falselist,$afalse,1);
  219:     &Apache::lonxml::debug("Picked $afalse");
  220:     push (@whichfalse,$afalse);
  221:   }
  222:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  223:   &Apache::lonxml::debug("the true statement is $answer");
  224:   return @whichfalse;
  225: }
  226: 
  227: sub displayfoils {
  228:   my ($max,$answer)=@_;
  229:   my $result;
  230: 
  231:   my @whichfoils=&whichfoils($max,$answer);
  232:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  233:     foreach my $name (@whichfoils) {
  234:       $result.="<br />";
  235:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  236: 	$result.='Correct';
  237:       } else {
  238: 	$result.='Incorrect';
  239:       }
  240:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  241:     }
  242:   } else {
  243:     my $temp=0;
  244:     my $id=$Apache::inputtags::response['-1'];
  245:     my $part=$Apache::inputtags::part;
  246:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  247:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  248:     foreach my $name (@whichfoils) {
  249:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  250:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  251:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  252:       $temp++;
  253:     }
  254:   }
  255:   return $result."<br />";
  256: }
  257: 
  258: sub displayanswers {
  259:   my ($max,$answer)=@_;
  260:   my @names = @{ $Apache::response::foilgroup{'names'} };
  261:   my @whichopt = &whichfoils($max,$answer);
  262:   my $result=&Apache::response::answer_header('radiobuttonresponse');
  263:   foreach my $name (@whichopt) {
  264:     $result.=&Apache::response::answer_part('radiobuttonresponse',
  265: 		     $Apache::response::foilgroup{$name.'.value'})
  266:   }
  267:   $result.=&Apache::response::answer_footer('radiobuttonresponse');
  268:   return $result;
  269: }
  270: 
  271: sub start_conceptgroup {
  272:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  273:   $Apache::radiobuttonresponse::conceptgroup=1;
  274:   %Apache::response::conceptgroup={};
  275:   my $result;
  276:   if ($target eq 'edit') {
  277:     $result.=&Apache::edit::tag_start($target,$token);
  278:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  279:       "</td></tr><tr><td colspan=\"3\">\n";
  280:   } elsif ($target eq 'modified') {
  281:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  282: 						 $safeeval,'concept');
  283:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  284:   }
  285:   return $result;
  286: }
  287: 
  288: sub end_conceptgroup {
  289:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  290:   $Apache::radiobuttonresponse::conceptgroup=0;
  291:   my $result;
  292:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  293:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  294:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  295:       my $pick=int(rand($#names+1));
  296:       my $name=$names[$pick];
  297:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  298:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  299:       $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
  300:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  301:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  302:       &Apache::lonxml::debug("Selecting $name in $concept");
  303:     }
  304:   } elsif ($target eq 'edit') {
  305:     $result=&Apache::edit::end_table();
  306:   }
  307:   return $result;
  308: }
  309: 
  310: sub insert_conceptgroup {
  311:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  312:   return $result;
  313: }
  314: 
  315: sub start_foil {
  316:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  317:   my $result='';
  318:   if ($target eq 'web') {
  319:     &Apache::lonxml::startredirection;
  320:   } elsif ($target eq 'edit') {
  321:     $result=&Apache::edit::tag_start($target,$token);
  322:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  323:     $result.=&Apache::edit::select_arg('Correct Option:','value',
  324: 				       ['unused','true','false'],$token);
  325:   } elsif ($target eq 'modified') {
  326:      my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  327: 						  'value','name');
  328:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  329:   }
  330:   return $result;
  331: }
  332: 
  333: sub end_foil {
  334:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  335:   my $text='';
  336:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  337:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  338:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  339:     if ($value ne 'unused') {
  340:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  341:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  342:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
  343: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  344: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  345: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  346:       } else {
  347: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  348: 	$Apache::response::foilgroup{"$name.value"} = $value;
  349: 	$Apache::response::foilgroup{"$name.text"} = $text;
  350:       }
  351:     }
  352:   }
  353:   return '';
  354: }
  355: 
  356: sub insert_foil {
  357:   return '
  358: <foil name="" value="unused">
  359: <startouttext />
  360: <endouttext />
  361: </foil>';
  362: }
  363: 1;
  364: __END__
  365:  

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