1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.33 2001/12/14 23:00:52 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> </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: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
69: return $result;
70: }
71:
72: %Apache::response::foilgroup={};
73: sub start_foilgroup {
74: %Apache::response::foilgroup={};
75: $Apache::radiobuttonresponse::conceptgroup=0;
76: &Apache::response::setrandomnumber();
77: return '';
78: }
79:
80: sub storesurvey {
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");
92: return '';
93: }
94:
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:
121: sub end_foilgroup {
122: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
123:
124: my $result;
125: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
126: my $style = $Apache::lonhomework::type;
127: if ( $style eq 'survey' ) {
128: if ($target eq 'web' || $target eq 'answer') {
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);
143: } elsif ($target eq 'answer' ) {
144: $result=&displayanswers($max,$answer);
145: } elsif ( $target eq 'grade') {
146: &grade_response($max,$answer);
147: }
148: }
149: }
150: return $result;
151: }
152:
153: sub getfoilcounts {
154: my ($parstack,$safeeval)=@_;
155: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
156: my @names = @{ $Apache::response::foilgroup{'names'} };
157: my $truecnt=0;
158: my $falsecnt=0;
159: my $name;
160:
161: foreach $name (@names) {
162: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
163: $truecnt++;
164: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
165: $falsecnt++;
166: }
167: }
168: return ($truecnt,$falsecnt,$max);
169: }
170:
171: sub displayallfoils {
172: my $result;
173: &Apache::lonxml::debug("survey style display");
174: my @names = @{ $Apache::response::foilgroup{'names'} };
175: my $temp=0;
176: my $id=$Apache::inputtags::response['-1'];
177: my $part=$Apache::inputtags::part;
178: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
179: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
180: &Apache::lonhomework::showhash(%lastresponse);
181: foreach my $name (@names) {
182: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
183: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
184: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
185: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
186: $temp++;
187: }
188: }
189: return $result;
190: }
191:
192: sub whichfoils {
193: my ($max,$answer)=@_;
194:
195: my @truelist;
196: my @falselist;
197:
198: my @names = @{ $Apache::response::foilgroup{'names'} };
199: foreach my $name (@names) {
200: #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
201: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
202: push (@truelist,$name);
203: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
204: push (@falselist,$name);
205: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
206: } else {
207: &Apache::lonxml::error("Unknown state $Apache::response::foilgroup{$name.'.value'} for $name in <foilgroup>");
208: }
209: }
210: my $whichtrue = int(rand($#truelist+1));
211: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
212: my @whichfalse =();
213: while ((($#whichfalse+1) < $max) && ($#falselist > -1)) {
214: &Apache::lonxml::debug("Have $#whichfalse max is $max");
215: my $afalse=int(rand($#falselist+1));
216: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
217: $afalse=splice(@falselist,$afalse,1);
218: &Apache::lonxml::debug("Picked $afalse");
219: push (@whichfalse,$afalse);
220: }
221: splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
222: &Apache::lonxml::debug("the true statement is $answer");
223: return @whichfalse;
224: }
225:
226: sub displayfoils {
227: my ($max,$answer)=@_;
228: my $result;
229:
230: my @whichfoils=&whichfoils($max,$answer);
231: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
232: foreach my $name (@whichfoils) {
233: $result.="<br />";
234: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
235: $result.='Correct';
236: } else {
237: $result.='Incorrect';
238: }
239: $result.=":".$Apache::response::foilgroup{$name.'.text'}."</input>\n";
240: }
241: } else {
242: my $temp=0;
243: my $id=$Apache::inputtags::response['-1'];
244: my $part=$Apache::inputtags::part;
245: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
246: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
247: foreach my $name (@whichfoils) {
248: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
249: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
250: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
251: $temp++;
252: }
253: }
254: return $result."<br />";
255: }
256:
257: sub displayanswers {
258: my ($max,$answer)=@_;
259: my @names = @{ $Apache::response::foilgroup{'names'} };
260: my @whichopt = &whichfoils($max,$answer);
261: my $result=&Apache::response::answer_header('radiobuttonresponse');
262: foreach my $name (@whichopt) {
263: $result.=&Apache::response::answer_part('radiobuttonresponse',
264: $Apache::response::foilgroup{$name.'.value'})
265: }
266: $result.=&Apache::response::answer_footer('radiobuttonresponse');
267: return $result;
268: }
269:
270: sub start_conceptgroup {
271: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
272: $Apache::radiobuttonresponse::conceptgroup=1;
273: %Apache::response::conceptgroup={};
274: my $result;
275: if ($target eq 'edit') {
276: $result.=&Apache::edit::tag_start($target,$token);
277: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
278: "</td></tr><tr><td colspan=\"3\">\n";
279: } elsif ($target eq 'modified') {
280: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
281: $safeeval,'concept');
282: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
283: }
284: return $result;
285: }
286:
287: sub end_conceptgroup {
288: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
289: $Apache::radiobuttonresponse::conceptgroup=0;
290: my $result;
291: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
292: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
293: my @names = @{ $Apache::response::conceptgroup{'names'} };
294: my $pick=int(rand($#names+1));
295: my $name=$names[$pick];
296: push @{ $Apache::response::foilgroup{'names'} }, $name;
297: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
298: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
299: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
300: $Apache::response::foilgroup{"$name.concept"} = $concept;
301: &Apache::lonxml::debug("Selecting $name in $concept");
302: }
303: } elsif ($target eq 'edit') {
304: $result=&Apache::edit::end_table();
305: }
306: return $result;
307: }
308:
309: sub insert_conceptgroup {
310: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
311: return $result;
312: }
313:
314: sub start_foil {
315: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
316: my $result='';
317: if ($target eq 'web') {
318: &Apache::lonxml::startredirection;
319: } elsif ($target eq 'edit') {
320: $result=&Apache::edit::tag_start($target,$token);
321: $result.=&Apache::edit::text_arg('Name:','name',$token);
322: $result.=&Apache::edit::select_arg('Correct Option:','value',
323: ['unused','true','false'],$token);
324: } elsif ($target eq 'modified') {
325: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
326: 'value','name');
327: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
328: }
329: return $result;
330: }
331:
332: sub end_foil {
333: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
334: my $text='';
335: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
336: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
337: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
338: if ($value ne 'unused') {
339: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
340: if (!$name) { $name=$Apache::lonxml::curdepth; }
341: if ( $Apache::radiobuttonresponse::conceptgroup ) {
342: push @{ $Apache::response::conceptgroup{'names'} }, $name;
343: $Apache::response::conceptgroup{"$name.value"} = $value;
344: $Apache::response::conceptgroup{"$name.text"} = $text;
345: } else {
346: push @{ $Apache::response::foilgroup{'names'} }, $name;
347: $Apache::response::foilgroup{"$name.value"} = $value;
348: $Apache::response::foilgroup{"$name.text"} = $text;
349: }
350: }
351: }
352: return '';
353: }
354:
355: sub insert_foil {
356: return '
357: <foil name="" value="unused">
358: <startouttext />
359: <endouttext />
360: </foil>';
361: }
362: 1;
363: __END__
364:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>