Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.21

1.21    ! harris41    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
1.1       albertel    9: 
                     10: package Apache::radiobuttonresponse;
                     11: use strict;
                     12: 
1.21    ! harris41   13: # ======================================================================= BEGIN
1.1       albertel   14: sub BEGIN {
1.21    ! harris41   15:   &Apache::lonxml::register('Apache::radiobuttonresponse',
        !            16: 			    ('radiobuttonresponse'));
1.1       albertel   17: }
                     18: 
1.21    ! harris41   19: # ================================================= Start radio button response
1.1       albertel   20: sub start_radiobuttonresponse {
1.21    ! harris41   21:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
1.3       albertel   22:   #when in a radiobutton response use these
1.21    ! harris41   23:   &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil',
        !            24: 							   'conceptgroup'));
1.4       albertel   25:   my $id = &Apache::response::start_response($parstack,$safeeval);
                     26:   return '';
1.1       albertel   27: }
                     28: 
1.21    ! harris41   29: # =================================================== End radio button response
1.1       albertel   30: sub end_radiobuttonresponse {
1.21    ! harris41   31:     &Apache::response::end_response;
        !            32:     return '';
1.1       albertel   33: }
                     34: 
1.21    ! harris41   35: %Apache::response::foilgroup = {};
        !            36: # ============================================================ Start foil group
1.1       albertel   37: sub start_foilgroup {
1.21    ! harris41   38:     %Apache::response::foilgroup = {};
        !            39:     $Apache::radiobuttonresponse::conceptgroup = 0;
        !            40:     &Apache::response::setrandomnumber();
        !            41:     return '';
1.5       albertel   42: }
                     43: 
1.21    ! harris41   44: # ================================================================ Store survey
1.15      albertel   45: sub storesurvey {
1.21    ! harris41   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: 	}
1.15      albertel   57:     }
1.21    ! harris41   58:     return '';
1.15      albertel   59: }
                     60: 
1.21    ! harris41   61: # ======================================= End foil group (return scalar string)
1.1       albertel   62: sub end_foilgroup {
1.21    ! harris41   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: 		}
1.15      albertel  107: 	    }
1.5       albertel  108: 	}
                    109:     }
1.21    ! harris41  110:     return $result;
1.6       albertel  111: }
                    112: 
1.21    ! harris41  113: # ==================================== Get foil counts (return 3 element array)
1.6       albertel  114: sub getfoilcounts {
1.21    ! harris41  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: 	}
1.6       albertel  127:     }
1.21    ! harris41  128:     return ($truecnt,$falsecnt,$max);
1.5       albertel  129: }
                    130: 
1.21    ! harris41  131: # ==================================== Display all foils (return scalar string)
1.15      albertel  132: sub displayallfoils {
1.21    ! harris41  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: 	}
1.15      albertel  151:     }
1.21    ! harris41  152:     return $result;
1.15      albertel  153: }
                    154: 
1.21    ! harris41  155: # ======================================== Display foils (return scalar string)
1.5       albertel  156: sub displayfoils {
1.21    ! harris41  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: 	}
1.3       albertel  175:     }
1.21    ! harris41  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);
1.10      albertel  187:     }
1.21    ! harris41  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: 	}
1.10      albertel  217:     }
1.21    ! harris41  218:     return $result."<br />";
1.14      albertel  219: }
                    220: 
1.21    ! harris41  221: # ========================================================= Start concept group
1.14      albertel  222: sub start_conceptgroup {
1.21    ! harris41  223:     $Apache::radiobuttonresponse::conceptgroup = 1;
        !           224:     %Apache::response::conceptgroup = {};
        !           225:     return '';
1.14      albertel  226: }
                    227: 
1.21    ! harris41  228: # =========================================================== End concept group
1.14      albertel  229: sub end_conceptgroup {
1.21    ! harris41  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 '';
1.1       albertel  247: }
                    248: 
1.21    ! harris41  249: # ================================================================== Start foil
1.1       albertel  250: sub start_foil {
1.21    ! harris41  251:     &Apache::lonxml::startredirection;
        !           252:     return '';
1.1       albertel  253: }
                    254: 
1.21    ! harris41  255: # ==================================================================== End foil
1.1       albertel  256: sub end_foil {
1.21    ! harris41  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') {
1.19      albertel  261:     my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18      albertel  262:     if ($value ne 'unused') {
1.19      albertel  263:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.18      albertel  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:     }
1.4       albertel  274:   }
1.1       albertel  275:   return '';
                    276: }
                    277: 
                    278: 1;
1.21    ! harris41  279: 
1.1       albertel  280: __END__
                    281:  

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