File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.35: download - view: text, annotated - select for diffs
Fri Jan 11 16:32:29 2002 UTC (22 years, 3 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Update to use edit::end_row() and edit::start_spanning_row() as a cleanup
for edit::select_arg and edit::select_or_text_arg.

    1: # The LearningOnline Network with CAPA
    2: # mutliple choice style responses
    3: #
    4: # $Id: radiobuttonresponse.pm,v 1.35 2002/01/11 16:32:29 matthew 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".&Apache::edit::end_row()
   51:         .&Apache::edit::start_spanning_row();
   52: 
   53:     $result.=&Apache::edit::text_arg('Max Number Of Foils:','max',$token,'4').
   54: 	&Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
   55:   } elsif ($target eq 'modified') {
   56:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
   57: 						 $safeeval,'max');
   58:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   59:   }
   60:   return $result;
   61: }
   62: 
   63: sub end_radiobuttonresponse {
   64:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   65:   my $result;
   66:   if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
   67:   &Apache::response::end_response;
   68:   pop @Apache::lonxml::namespace;
   69:   &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
   70:   return $result;
   71: }
   72: 
   73: %Apache::response::foilgroup={};
   74: sub start_foilgroup {
   75:   %Apache::response::foilgroup={};
   76:   $Apache::radiobuttonresponse::conceptgroup=0;
   77:   &Apache::response::setrandomnumber();
   78:   return '';
   79: }
   80: 
   81: sub storesurvey {
   82:   if ( !defined($ENV{'form.submitted'})) { return ''; }
   83:   my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   84:   &Apache::lonxml::debug("Here I am!:$response:");
   85:   if ( $response !~ /[0-9]+/) { return ''; }
   86:   my $id = $Apache::inputtags::response['-1'];
   87:   my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
   88:   my %responsehash;
   89:   $responsehash{$whichfoils[$response]}=$response;
   90:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
   91:   $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
   92:   &Apache::lonxml::debug("submitted a $response<br />\n");
   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;
  158:   my $truecnt=0;
  159:   my $falsecnt=0;
  160:   my $name;
  161:   if ( $Apache::response::foilgroup{'names'} ) {
  162:     @names= @{ $Apache::response::foilgroup{'names'} };
  163:   }
  164:   foreach $name (@names) {
  165:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  166:       $truecnt++;
  167:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  168:       $falsecnt++;
  169:     }
  170:   }
  171:   return ($truecnt,$falsecnt,$max);
  172: }
  173: 
  174: sub displayallfoils {
  175:   my $result;
  176:   &Apache::lonxml::debug("survey style display");
  177:   my @names = @{ $Apache::response::foilgroup{'names'} };
  178:   my $temp=0;
  179:   my $id=$Apache::inputtags::response['-1'];
  180:   my $part=$Apache::inputtags::part;
  181:   my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  182:   my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  183:   &Apache::lonhomework::showhash(%lastresponse);
  184:   foreach my $name (@names) {
  185:     if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  186:       $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  187:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  188:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  189:       $temp++;
  190:     }
  191:   }
  192:   return $result;
  193: }
  194: 
  195: sub whichfoils {
  196:   my ($max,$answer)=@_;
  197: 
  198:   my @truelist;
  199:   my @falselist;
  200: 
  201:   my @names;
  202:   if ( $Apache::response::foilgroup{'names'} ) {
  203:     @names= @{ $Apache::response::foilgroup{'names'} };
  204:   }
  205:   foreach my $name (@names) {
  206:     #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  207:     if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  208:       push (@truelist,$name);
  209:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  210:       push (@falselist,$name);
  211:     } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  212:     } else {
  213:       &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
  214:     }
  215:   }
  216:   my $whichtrue = int(rand($#truelist+1));
  217:   &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
  218:   my @whichfalse =();
  219:   while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
  220:     &Apache::lonxml::debug("Have $#whichfalse max is $max");
  221:     my $afalse=int(rand($#falselist+1));
  222:     &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  223:     $afalse=splice(@falselist,$afalse,1);
  224:     &Apache::lonxml::debug("Picked $afalse");
  225:     push (@whichfalse,$afalse);
  226:   }
  227:   splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  228:   &Apache::lonxml::debug("the true statement is $answer");
  229:   return @whichfalse;
  230: }
  231: 
  232: sub displayfoils {
  233:   my ($max,$answer)=@_;
  234:   my $result;
  235: 
  236:   my @whichfoils=&whichfoils($max,$answer);
  237:   if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  238:     foreach my $name (@whichfoils) {
  239:       $result.="<br />";
  240:       if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  241: 	$result.='Correct';
  242:       } else {
  243: 	$result.='Incorrect';
  244:       }
  245:       $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  246:     }
  247:   } else {
  248:     my $temp=0;
  249:     my $id=$Apache::inputtags::response['-1'];
  250:     my $part=$Apache::inputtags::part;
  251:     my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
  252:     my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
  253:     foreach my $name (@whichfoils) {
  254:        $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
  255:       if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
  256:       $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
  257:       $temp++;
  258:     }
  259:   }
  260:   return $result."<br />";
  261: }
  262: 
  263: sub displayanswers {
  264:   my ($max,$answer)=@_;
  265:   my @whichopt = &whichfoils($max,$answer);
  266:   my $result=&Apache::response::answer_header('radiobuttonresponse');
  267:   foreach my $name (@whichopt) {
  268:     $result.=&Apache::response::answer_part('radiobuttonresponse',
  269: 		     $Apache::response::foilgroup{$name.'.value'})
  270:   }
  271:   $result.=&Apache::response::answer_footer('radiobuttonresponse');
  272:   return $result;
  273: }
  274: 
  275: sub start_conceptgroup {
  276:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  277:   $Apache::radiobuttonresponse::conceptgroup=1;
  278:   %Apache::response::conceptgroup={};
  279:   my $result;
  280:   if ($target eq 'edit') {
  281:     $result.=&Apache::edit::tag_start($target,$token);
  282:     $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
  283: 	&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  284:   } elsif ($target eq 'modified') {
  285:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  286: 						 $safeeval,'concept');
  287:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  288:   }
  289:   return $result;
  290: }
  291: 
  292: sub end_conceptgroup {
  293:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  294:   $Apache::radiobuttonresponse::conceptgroup=0;
  295:   my $result;
  296:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  297:     if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
  298:       my @names = @{ $Apache::response::conceptgroup{'names'} };
  299:       my $pick=int(rand($#names+1));
  300:       my $name=$names[$pick];
  301:       push @{ $Apache::response::foilgroup{'names'} }, $name;
  302:       $Apache::response::foilgroup{"$name.text"} =  $Apache::response::conceptgroup{"$name.text"};
  303:       $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
  304:       my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  305:       $Apache::response::foilgroup{"$name.concept"} = $concept;
  306:       &Apache::lonxml::debug("Selecting $name in $concept");
  307:     }
  308:   } elsif ($target eq 'edit') {
  309:     $result=&Apache::edit::end_table();
  310:   }
  311:   return $result;
  312: }
  313: 
  314: sub insert_conceptgroup {
  315:   my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
  316:   return $result;
  317: }
  318: 
  319: sub start_foil {
  320:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  321:   my $result='';
  322:   if ($target eq 'web') {
  323:     &Apache::lonxml::startredirection;
  324:   } elsif ($target eq 'edit') {
  325:     $result=&Apache::edit::tag_start($target,$token);
  326:     $result.=&Apache::edit::text_arg('Name:','name',$token);
  327:     $result.=&Apache::edit::select_arg('Correct Option:','value',
  328: 				       ['unused','true','false'],$token);
  329:     $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  330:   } elsif ($target eq 'modified') {
  331:      my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  332: 						  'value','name');
  333:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  334:   }
  335:   return $result;
  336: }
  337: 
  338: sub end_foil {
  339:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  340:   my $text='';
  341:   if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
  342:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
  343:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  344:     if ($value ne 'unused') {
  345:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  346:       if (!$name) { $name=$Apache::lonxml::curdepth; }
  347:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
  348: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  349: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  350: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  351:       } else {
  352: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  353: 	$Apache::response::foilgroup{"$name.value"} = $value;
  354: 	$Apache::response::foilgroup{"$name.text"} = $text;
  355:       }
  356:     }
  357:   }
  358:   return '';
  359: }
  360: 
  361: sub insert_foil {
  362:   return '
  363: <foil name="" value="unused">
  364: <startouttext />
  365: <endouttext />
  366: </foil>';
  367: }
  368: 1;
  369: __END__
  370:  

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