Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.34
1.22 albertel 1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
1.31 albertel 3: #
1.34 ! albertel 4: # $Id: radiobuttonresponse.pm,v 1.33 2001/12/14 23:00:52 albertel Exp $
1.31 albertel 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: #
1.22 albertel 28: # 2/21 Guy
1.1 albertel 29:
30: package Apache::radiobuttonresponse;
31: use strict;
32:
33: sub BEGIN {
1.22 albertel 34: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1 albertel 35: }
36:
37: sub start_radiobuttonresponse {
1.22 albertel 38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26 albertel 39: my $result;
1.3 albertel 40: #when in a radiobutton response use these
1.22 albertel 41: &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26 albertel 42: push (@Apache::lonxml::namespace,'radiobuttonresponse');
1.4 albertel 43: my $id = &Apache::response::start_response($parstack,$safeeval);
1.25 albertel 44: if ($target eq 'meta') {
45: $result=&Apache::response::meta_package_write('radiobuttonresponse');
1.26 albertel 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> </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); }
1.25 albertel 58: }
59: return $result;
1.1 albertel 60: }
61:
62: sub end_radiobuttonresponse {
1.26 albertel 63: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
64: my $result;
65: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.22 albertel 66: &Apache::response::end_response;
1.26 albertel 67: pop @Apache::lonxml::namespace;
1.33 albertel 68: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26 albertel 69: return $result;
1.1 albertel 70: }
71:
1.22 albertel 72: %Apache::response::foilgroup={};
1.1 albertel 73: sub start_foilgroup {
1.22 albertel 74: %Apache::response::foilgroup={};
75: $Apache::radiobuttonresponse::conceptgroup=0;
76: &Apache::response::setrandomnumber();
77: return '';
1.5 albertel 78: }
79:
1.15 albertel 80: sub storesurvey {
1.33 albertel 81: if ( !defined($ENV{'form.submitted'})) { return ''; }
82: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
83: &Apache::lonxml::debug("Here I am!:$response:");
84: if ( $response !~ /[0-9]+/) { return ''; }
85: my $id = $Apache::inputtags::response['-1'];
86: my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
87: my %responsehash;
88: $responsehash{$whichfoils[$response]}=$response;
89: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
90: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
91: &Apache::lonxml::debug("submitted a $response<br />\n");
1.22 albertel 92: return '';
1.15 albertel 93: }
94:
1.32 albertel 95: sub grade_response {
96: my ($max,$answer)=@_;
97: if (!defined($ENV{'form.submitted'})) { return; }
98: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
99: if ( $response !~ /[0-9]+/) { return; }
100: my $part=$Apache::inputtags::part;
101: my $id = $Apache::inputtags::response['-1'];
102: my @whichfoils=&whichfoils($max,$answer);
103: my %responsehash;
104: $responsehash{$whichfoils[$response]}=$response;
105: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
106: my %previous=&Apache::response::check_for_previous($responsestr,
107: $part,$id);
108: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
109: $responsestr;
110: &Apache::lonxml::debug("submitted a $response<br />\n");
111: my $ad;
112: if ($response == $answer) {
113: $ad='EXACT_ANS';
114: } else {
115: $ad='INCORRECT';
116: }
117: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
118: &Apache::response::handle_previous(\%previous,$ad);
119: }
120:
1.1 albertel 121: sub end_foilgroup {
1.22 albertel 122: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29 albertel 123:
1.22 albertel 124: my $result;
1.28 albertel 125: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
1.29 albertel 126: my $style = $Apache::lonhomework::type;
1.22 albertel 127: if ( $style eq 'survey' ) {
1.30 albertel 128: if ($target eq 'web' || $target eq 'answer') {
1.22 albertel 129: $result=&displayallfoils();
130: } elsif ( $target eq 'grade' ) {
131: $result=&storesurvey();
132: }
133: } else {
134: my $name;
135: my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
136: my $count=0;
137: # we will add in 1 of the true statements
138: if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
139: my $answer = int(rand ($count));
140: &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
141: if ($target eq 'web') {
142: $result=&displayfoils($max,$answer);
1.28 albertel 143: } elsif ($target eq 'answer' ) {
144: $result=&displayanswers($max,$answer);
1.22 albertel 145: } elsif ( $target eq 'grade') {
1.32 albertel 146: &grade_response($max,$answer);
1.22 albertel 147: }
1.5 albertel 148: }
1.22 albertel 149: }
150: return $result;
1.6 albertel 151: }
152:
153: sub getfoilcounts {
1.22 albertel 154: my ($parstack,$safeeval)=@_;
155: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
1.34 ! albertel 156: my @names;
1.22 albertel 157: my $truecnt=0;
158: my $falsecnt=0;
159: my $name;
1.34 ! albertel 160: if ( $Apache::response::foilgroup{'names'} ) {
! 161: @names= @{ $Apache::response::foilgroup{'names'} };
! 162: }
1.22 albertel 163: foreach $name (@names) {
164: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
165: $truecnt++;
166: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
167: $falsecnt++;
1.6 albertel 168: }
1.22 albertel 169: }
170: return ($truecnt,$falsecnt,$max);
1.5 albertel 171: }
172:
1.15 albertel 173: sub displayallfoils {
1.22 albertel 174: my $result;
175: &Apache::lonxml::debug("survey style display");
176: my @names = @{ $Apache::response::foilgroup{'names'} };
177: my $temp=0;
178: my $id=$Apache::inputtags::response['-1'];
179: my $part=$Apache::inputtags::part;
180: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32 albertel 181: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
182: &Apache::lonhomework::showhash(%lastresponse);
1.22 albertel 183: foreach my $name (@names) {
184: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
185: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.32 albertel 186: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.22 albertel 187: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
188: $temp++;
1.15 albertel 189: }
1.22 albertel 190: }
191: return $result;
1.15 albertel 192: }
193:
1.28 albertel 194: sub whichfoils {
1.22 albertel 195: my ($max,$answer)=@_;
1.28 albertel 196:
1.22 albertel 197: my @truelist;
198: my @falselist;
199:
1.34 ! albertel 200: my @names;
! 201: if ( $Apache::response::foilgroup{'names'} ) {
! 202: @names= @{ $Apache::response::foilgroup{'names'} };
! 203: }
1.28 albertel 204: foreach my $name (@names) {
1.22 albertel 205: #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
206: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
207: push (@truelist,$name);
208: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
209: push (@falselist,$name);
210: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
211: } else {
212: &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
1.3 albertel 213: }
1.22 albertel 214: }
215: my $whichtrue = int(rand($#truelist+1));
216: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
217: my @whichfalse =();
218: while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
219: &Apache::lonxml::debug("Have $#whichfalse max is $max");
220: my $afalse=int(rand($#falselist+1));
221: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
222: $afalse=splice(@falselist,$afalse,1);
223: &Apache::lonxml::debug("Picked $afalse");
224: push (@whichfalse,$afalse);
225: }
226: splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
227: &Apache::lonxml::debug("the true statement is $answer");
1.28 albertel 228: return @whichfalse;
229: }
230:
231: sub displayfoils {
232: my ($max,$answer)=@_;
233: my $result;
234:
235: my @whichfoils=&whichfoils($max,$answer);
1.22 albertel 236: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.28 albertel 237: foreach my $name (@whichfoils) {
1.22 albertel 238: $result.="<br />";
239: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
240: $result.='Correct';
241: } else {
242: $result.='Incorrect';
243: }
244: $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
1.10 albertel 245: }
1.22 albertel 246: } else {
247: my $temp=0;
248: my $id=$Apache::inputtags::response['-1'];
249: my $part=$Apache::inputtags::part;
250: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32 albertel 251: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.28 albertel 252: foreach my $name (@whichfoils) {
1.22 albertel 253: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.32 albertel 254: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.22 albertel 255: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
256: $temp++;
1.10 albertel 257: }
1.22 albertel 258: }
259: return $result."<br />";
1.14 albertel 260: }
261:
1.28 albertel 262: sub displayanswers {
263: my ($max,$answer)=@_;
264: my @whichopt = &whichfoils($max,$answer);
265: my $result=&Apache::response::answer_header('radiobuttonresponse');
266: foreach my $name (@whichopt) {
267: $result.=&Apache::response::answer_part('radiobuttonresponse',
268: $Apache::response::foilgroup{$name.'.value'})
269: }
270: $result.=&Apache::response::answer_footer('radiobuttonresponse');
271: return $result;
272: }
273:
1.14 albertel 274: sub start_conceptgroup {
1.27 albertel 275: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22 albertel 276: $Apache::radiobuttonresponse::conceptgroup=1;
277: %Apache::response::conceptgroup={};
1.26 albertel 278: my $result;
279: if ($target eq 'edit') {
280: $result.=&Apache::edit::tag_start($target,$token);
281: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
282: "</td></tr><tr><td colspan=\"3\">\n";
283: } elsif ($target eq 'modified') {
284: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
285: $safeeval,'concept');
286: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
287: }
288: return $result;
1.14 albertel 289: }
290:
291: sub end_conceptgroup {
1.22 albertel 292: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
293: $Apache::radiobuttonresponse::conceptgroup=0;
1.26 albertel 294: my $result;
1.28 albertel 295: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.27 albertel 296: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
297: my @names = @{ $Apache::response::conceptgroup{'names'} };
1.28 albertel 298: my $pick=int(rand($#names+1));
1.27 albertel 299: my $name=$names[$pick];
300: push @{ $Apache::response::foilgroup{'names'} }, $name;
301: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
302: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
303: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
304: $Apache::response::foilgroup{"$name.concept"} = $concept;
305: &Apache::lonxml::debug("Selecting $name in $concept");
306: }
1.26 albertel 307: } elsif ($target eq 'edit') {
308: $result=&Apache::edit::end_table();
1.22 albertel 309: }
1.26 albertel 310: return $result;
311: }
312:
313: sub insert_conceptgroup {
314: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
315: return $result;
1.1 albertel 316: }
317:
318: sub start_foil {
1.24 albertel 319: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
320: my $result='';
1.27 albertel 321: if ($target eq 'web') {
322: &Apache::lonxml::startredirection;
323: } elsif ($target eq 'edit') {
324: $result=&Apache::edit::tag_start($target,$token);
325: $result.=&Apache::edit::text_arg('Name:','name',$token);
326: $result.=&Apache::edit::select_arg('Correct Option:','value',
1.28 albertel 327: ['unused','true','false'],$token);
1.27 albertel 328: } elsif ($target eq 'modified') {
329: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
330: 'value','name');
331: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
332: }
333: return $result;
1.1 albertel 334: }
335:
336: sub end_foil {
1.22 albertel 337: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
338: my $text='';
339: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.28 albertel 340: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.19 albertel 341: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18 albertel 342: if ($value ne 'unused') {
1.19 albertel 343: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.27 albertel 344: if (!$name) { $name=$Apache::lonxml::curdepth; }
1.18 albertel 345: if ( $Apache::radiobuttonresponse::conceptgroup ) {
346: push @{ $Apache::response::conceptgroup{'names'} }, $name;
347: $Apache::response::conceptgroup{"$name.value"} = $value;
348: $Apache::response::conceptgroup{"$name.text"} = $text;
349: } else {
350: push @{ $Apache::response::foilgroup{'names'} }, $name;
351: $Apache::response::foilgroup{"$name.value"} = $value;
352: $Apache::response::foilgroup{"$name.text"} = $text;
353: }
354: }
1.4 albertel 355: }
1.1 albertel 356: return '';
357: }
358:
1.27 albertel 359: sub insert_foil {
360: return '
361: <foil name="" value="unused">
362: <startouttext />
363: <endouttext />
364: </foil>';
365: }
1.1 albertel 366: 1;
367: __END__
368:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>