File:  [LON-CAPA] / loncom / homework / radiobuttonresponse.pm
Revision 1.21: download - view: text, annotated - select for diffs
Mon Aug 6 19:36:27 2001 UTC (22 years, 9 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
beautify/optimize

    1: # The LON-CAPA radio button response handler
    2: #
    3: # Multiple choice style responses
    4: #
    5: # YEAR=2001
    6: # 1/8,1/11,1/12,1/15,1/19,2/5,2/19,2/21,2/22,3/1,4/23,5/4,5/31 Guy Albertelli
    7: # 6/1,6/2 Guy Albertelli
    8: # 8/6 Scott Harrison
    9: 
   10: package Apache::radiobuttonresponse;
   11: use strict;
   12: 
   13: # ======================================================================= BEGIN
   14: sub BEGIN {
   15:   &Apache::lonxml::register('Apache::radiobuttonresponse',
   16: 			    ('radiobuttonresponse'));
   17: }
   18: 
   19: # ================================================= Start radio button response
   20: sub start_radiobuttonresponse {
   21:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   22:   #when in a radiobutton response use these
   23:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil',
   24: 							   'conceptgroup'));
   25:   my $id = &Apache::response::start_response($parstack,$safeeval);
   26:   return '';
   27: }
   28: 
   29: # =================================================== End radio button response
   30: sub end_radiobuttonresponse {
   31:     &Apache::response::end_response;
   32:     return '';
   33: }
   34: 
   35: %Apache::response::foilgroup = {};
   36: # ============================================================ Start foil group
   37: sub start_foilgroup {
   38:     %Apache::response::foilgroup = {};
   39:     $Apache::radiobuttonresponse::conceptgroup = 0;
   40:     &Apache::response::setrandomnumber();
   41:     return '';
   42: }
   43: 
   44: # ================================================================ Store survey
   45: sub storesurvey {
   46:     if ( defined $ENV{'form.submitted'}) {
   47: 	my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   48: 	&Apache::lonxml::debug("Here I am!:$response:");
   49: 	if ( $response =~ /[^\s]/) {
   50: 	    my $id = $Apache::inputtags::response['-1'];
   51: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part".
   52: 					  ".$id.submission"}=$response;
   53: 	    $Apache::lonhomework::results{"resource.$Apache::inputtags::part".
   54: 					  ".$id.awarddetail"}='SUBMITTED';
   55: 	    &Apache::lonxml::debug("submitted a $response<br />\n");
   56: 	}
   57:     }
   58:     return '';
   59: }
   60: 
   61: # ======================================= End foil group (return scalar string)
   62: sub end_foilgroup {
   63:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
   64:     my $result;
   65:     if ($target ne 'meta') {
   66: 	my $style = &Apache::lonxml::get_param('style',$parstack,$safeeval,
   67: 					       '-2');
   68: 	if ( $style eq 'survey' ) {
   69: 	    if ($target eq 'web') {
   70: 		$result = &displayallfoils();
   71: 	    } elsif ( $target eq 'grade' ) {
   72: 		$result = &storesurvey();
   73: 	    }
   74: 	} else {
   75: 	    my $name;
   76: 	    my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
   77: 	    my $count = 0;
   78: 	    # we will add in 1 of the true statements
   79: 	    if (($falsecnt+1) > $max) { $count = $max } else {
   80: 		$count = $falsecnt+1; }
   81: 	    my $answer = int(rand ($count));
   82: 	    &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
   83: 	    if ($target eq 'web') {
   84: 		$result=&displayfoils($max,$answer);
   85: 	    } elsif ( $target eq 'grade') {
   86: 		if ( defined $ENV{'form.submitted'}) {
   87: 		    my $response = $ENV{'form.HWVAL'.
   88: 				   $Apache::inputtags::response['-1']};
   89: 		    if ( $response =~ /[^\s]/) {
   90: 			my $id = $Apache::inputtags::response['-1'];
   91: 			$Apache::lonhomework::results{"resource.".
   92: 				                  $Apache::inputtags::part.".
   93:                                                   $id.submission"} = $response;
   94: 			&Apache::lonxml::debug("submitted a $response<br />".
   95: 					       "\n");
   96: 			if ($response == $answer) {
   97: 			    $Apache::lonhomework::results{"resource.".
   98: 					      $Apache::inputtags::part.
   99: 					     ".$id.awarddetail"} = 'EXACT_ANS';
  100: 			} else {
  101: 			    $Apache::lonhomework::results{"resource.".
  102: 					      $Apache::inputtags::part.
  103: 					     ".$id.awarddetail"} = 'INCORRECT';
  104:  			}
  105: 		    }
  106: 		}
  107: 	    }
  108: 	}
  109:     }
  110:     return $result;
  111: }
  112: 
  113: # ==================================== Get foil counts (return 3 element array)
  114: sub getfoilcounts {
  115:     my ($parstack,$safeeval)=@_;
  116:     my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
  117:     my @names = @{ $Apache::response::foilgroup{'names'} };
  118:     my $truecnt = 0;
  119:     my $falsecnt = 0;
  120:     my $name;
  121:     foreach $name (@names) {
  122: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  123: 	    $truecnt++;
  124: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  125: 	    $falsecnt++;
  126: 	}
  127:     }
  128:     return ($truecnt,$falsecnt,$max);
  129: }
  130: 
  131: # ==================================== Display all foils (return scalar string)
  132: sub displayallfoils {
  133:     my $result;
  134:     &Apache::lonxml::debug("survey style display");
  135:     my @names = @{ $Apache::response::foilgroup{'names'} };
  136:     my $temp = 0;
  137:     my $id = $Apache::inputtags::response['-1'];
  138:     my $part = $Apache::inputtags::part;
  139:     my $lastresponse = $Apache::lonhomework::history{"resource.$part.$id.".
  140: 						   "submission"};
  141:     foreach my $name (@names) {
  142: 	if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
  143: 	    $result .= "<br /><input type=\"radio\" name=\"HWVAL".
  144: 		       $Apache::inputtags::response['-1'].
  145: 		       "\" value=\"$temp\" ";
  146: 	    if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
  147: 	    $result .= '>'.$Apache::response::foilgroup{$name.'.text'}.
  148: 		       "</input>\n";
  149: 	    $temp++;
  150: 	}
  151:     }
  152:     return $result;
  153: }
  154: 
  155: # ======================================== Display foils (return scalar string)
  156: sub displayfoils {
  157:     my ($max,$answer) = @_;
  158:     my @names = @{ $Apache::response::foilgroup{'names'} };
  159:     my @truelist;
  160:     my @falselist;
  161:     my $result;
  162:     my $name;
  163:     foreach $name (@names) {
  164: 	#result .= "<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
  165: 	if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
  166: 	    push (@truelist,$name);
  167: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
  168: 	    push (@falselist,$name);
  169: 	} elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
  170: 	} else {
  171: 	    &Apache::lonxml::error("Unknown state ".
  172: 				  $Apache::response::foilgroup{$name.'.value'}.
  173: 				  " for $name in <foilgroup>");
  174: 	}
  175:     }
  176:     my $whichtrue = int(rand($#truelist+1));
  177:     &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking ".
  178: 			   $whichtrue);
  179:     my @whichfalse = ();
  180:     while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
  181: 	&Apache::lonxml::debug("Have $#whichfalse max is $max");
  182: 	my $afalse = int(rand($#falselist+1));
  183: 	&Apache::lonxml::debug("From $#falselist elms, picking $afalse");
  184: 	$afalse = splice(@falselist,$afalse,1);
  185: 	&Apache::lonxml::debug("Picked $afalse");
  186: 	push (@whichfalse,$afalse);
  187:     }
  188:     splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
  189:     &Apache::lonxml::debug("the true statement is $answer");
  190:     if ($Apache::lonhomework::history{
  191: 	"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
  192: 	foreach $name (@whichfalse) {
  193: 	    $result .= "<br />";
  194: 	    if ($Apache::response::foilgroup{$name.'.value'} eq 'true') { 
  195: 		$result .= 'Correct';
  196: 	    } else {
  197: 		$result.='Incorrect';
  198: 	    }
  199: 	    $result .= ":".$Apache::response::foilgroup{$name.'.text'}.
  200: 		       "</input>\n";
  201: 	}
  202:     } else {
  203: 	my $temp=0;
  204: 	my $id=$Apache::inputtags::response['-1'];
  205: 	my $part=$Apache::inputtags::part;
  206: 	my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.".
  207: 						       "submission"};
  208: 	foreach $name (@whichfalse) {
  209: 	    $result.="<br /><input type=\"radio\" name=\"HWVAL".
  210: 		     $Apache::inputtags::response['-1'].
  211: 		     "\" value=\"$temp\" ";
  212: 	    if ($lastresponse eq $temp) { $result .= 'checked="on"'; }
  213: 	    $result .= '>'.$Apache::response::foilgroup{$name.'.text'}.
  214: 		"</input>\n";
  215: 	    $temp++;
  216: 	}
  217:     }
  218:     return $result."<br />";
  219: }
  220: 
  221: # ========================================================= Start concept group
  222: sub start_conceptgroup {
  223:     $Apache::radiobuttonresponse::conceptgroup = 1;
  224:     %Apache::response::conceptgroup = {};
  225:     return '';
  226: }
  227: 
  228: # =========================================================== End concept group
  229: sub end_conceptgroup {
  230:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  231:     $Apache::radiobuttonresponse::conceptgroup = 0;
  232:     if ($target eq 'web' || $target eq 'grade') {
  233: 	my @names = @{ $Apache::response::conceptgroup{'names'} };
  234: 	my $pick = int rand $#names+1;
  235: 	my $name = $names[$pick];
  236: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  237: 	$Apache::response::foilgroup{"$name.text"} = 
  238: 	    $Apache::response::conceptgroup{"$name.text"};
  239: 	$Apache::response::foilgroup{"$name.value"} =
  240: 	    $Apache::response::conceptgroup{"$name.value"};
  241: 	my $concept = 
  242: 	    &Apache::lonxml::get_param('concept',$parstack,$safeeval);
  243: 	$Apache::response::foilgroup{"$name.concept"} = $concept;
  244: 	&Apache::lonxml::debug("Selecting $name in $concept");
  245:     }
  246:     return '';
  247: }
  248: 
  249: # ================================================================== Start foil
  250: sub start_foil {
  251:     &Apache::lonxml::startredirection;
  252:     return '';
  253: }
  254: 
  255: # ==================================================================== End foil
  256: sub end_foil {
  257:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
  258:     my $text = '';
  259:     if ($target eq 'web') { $text = &Apache::lonxml::endredirection; }
  260:     if ($target eq 'web' || $target eq 'grade') {
  261:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
  262:     if ($value ne 'unused') {
  263:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  264:       if ( $Apache::radiobuttonresponse::conceptgroup ) {
  265: 	push @{ $Apache::response::conceptgroup{'names'} }, $name;
  266: 	$Apache::response::conceptgroup{"$name.value"} = $value;
  267: 	$Apache::response::conceptgroup{"$name.text"} = $text;	
  268:       } else {
  269: 	push @{ $Apache::response::foilgroup{'names'} }, $name;
  270: 	$Apache::response::foilgroup{"$name.value"} = $value;
  271: 	$Apache::response::foilgroup{"$name.text"} = $text;
  272:       }
  273:     }
  274:   }
  275:   return '';
  276: }
  277: 
  278: 1;
  279: 
  280: __END__
  281:  

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