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