1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.47 2002/09/23 17:20:41 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: use HTML::Entities();
33:
34: BEGIN {
35: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
36: }
37:
38: sub start_radiobuttonresponse {
39: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
40: my $result;
41: #when in a radiobutton response use these
42: &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
43: push (@Apache::lonxml::namespace,'radiobuttonresponse');
44: my $id = &Apache::response::start_response($parstack,$safeeval);
45: if ($target eq 'meta') {
46: $result=&Apache::response::meta_package_write('radiobuttonresponse');
47: } elsif ($target eq 'edit' ) {
48: $result.=&Apache::edit::start_table($token).
49: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
50: &Apache::edit::deletelist($target,$token)
51: ."</td><td> ".&Apache::edit::end_row()
52: .&Apache::edit::start_spanning_row();
53:
54: $result.=
55: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
56: &Apache::edit::select_arg('Randomize Foil Order','randomize',
57: ['yes','no'],$token).
58: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
59: } elsif ($target eq 'modified') {
60: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
61: $safeeval,'max','randomize');
62: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
63: } elsif ($target eq 'tex') {
64: $result .= '\begin{enumerate}';
65: }
66: return $result;
67: }
68:
69: sub end_radiobuttonresponse {
70: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
71: my $result;
72: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
73: if ($target eq 'tex') { $result .= '\end{enumerate}'; }
74: &Apache::response::end_response;
75: pop @Apache::lonxml::namespace;
76: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
77: return $result;
78: }
79:
80: %Apache::response::foilgroup=();
81: sub start_foilgroup {
82: %Apache::response::foilgroup=();
83: $Apache::radiobuttonresponse::conceptgroup=0;
84: &Apache::response::setrandomnumber();
85: return '';
86: }
87:
88: sub storesurvey {
89: if ( !defined($ENV{'form.submitted'})) { return ''; }
90: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
91: &Apache::lonxml::debug("Here I am!:$response:");
92: if ( $response !~ /[0-9]+/) { return ''; }
93: my $id = $Apache::inputtags::response['-1'];
94: my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
95: my %responsehash;
96: $responsehash{$whichfoils[$response]}=$response;
97: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
98: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
99: &Apache::lonxml::debug("submitted a $response<br />\n");
100: return '';
101: }
102:
103: sub grade_response {
104: my ($max,$answer,$randomize)=@_;
105: if (!defined($ENV{'form.submitted'})) { return; }
106: my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
107: if ( $response !~ /[0-9]+/) { return; }
108: my $part=$Apache::inputtags::part;
109: my $id = $Apache::inputtags::response['-1'];
110: my @whichfoils=&whichfoils($max,$answer,$randomize);
111: my %responsehash;
112: $responsehash{$whichfoils[$response]}=$response;
113: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
114: my %previous=&Apache::response::check_for_previous($responsestr,
115: $part,$id);
116: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
117: $responsestr;
118: &Apache::lonxml::debug("submitted a $response<br />\n");
119: my $ad;
120: if ($response == $answer) {
121: $ad='EXACT_ANS';
122: } else {
123: $ad='INCORRECT';
124: }
125: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
126: &Apache::response::handle_previous(\%previous,$ad);
127: }
128:
129: sub end_foilgroup {
130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
131:
132: my $result;
133: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
134: my $style = $Apache::lonhomework::type;
135: if ( $style eq 'survey' ) {
136: if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
137: $result=&displayallfoils();
138: } elsif ( $target eq 'grade' ) {
139: $result=&storesurvey();
140: }
141: } else {
142: my $name;
143: my ($truecnt,$falsecnt,$max) = &getfoilcounts($parstack,$safeeval);
144: my $count=0;
145: # we will add in 1 of the true statements
146: if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
147: my $answer = int(rand ($count));
148: &Apache::lonxml::debug("Answer is $answer, $count from $max, $falsecnt");
149: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
150: $safeeval,'-2');
151: if ($target eq 'web' || $target eq 'tex') {
152: $result=&displayfoils($target,$max,$answer,$randomize);
153: } elsif ($target eq 'answer' ) {
154: $result=&displayanswers($max,$answer,$randomize);
155: } elsif ( $target eq 'grade') {
156: &grade_response($max,$answer,$randomize);
157: }
158: }
159: }
160: return $result;
161: }
162:
163: sub getfoilcounts {
164: my ($parstack,$safeeval)=@_;
165: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
166: my @names;
167: my $truecnt=0;
168: my $falsecnt=0;
169: my $name;
170: if ( $Apache::response::foilgroup{'names'} ) {
171: @names= @{ $Apache::response::foilgroup{'names'} };
172: }
173: foreach $name (@names) {
174: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
175: $truecnt++;
176: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
177: $falsecnt++;
178: }
179: }
180: return ($truecnt,$falsecnt,$max);
181: }
182:
183: sub displayallfoils {
184: my $result;
185: &Apache::lonxml::debug("survey style display");
186: my @names = @{ $Apache::response::foilgroup{'names'} };
187: my $temp=0;
188: my $id=$Apache::inputtags::response['-1'];
189: my $part=$Apache::inputtags::part;
190: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
191: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
192: if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/) || ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER')) {
193: foreach my $name (@names) {
194: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
195: $result.="<br />".$Apache::response::foilgroup{$name.'.value'};
196: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
197: $result.='<b>';
198: }
199: $result .= $Apache::response::foilgroup{$name.'.text'};
200: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
201: $result.='</b>';
202: }
203: }
204: }
205: } else {
206: foreach my $name (@names) {
207: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
208: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
209: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
210: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
211: $temp++;
212: }
213: }
214: }
215: return $result;
216: }
217:
218: sub whichfoils {
219: my ($max,$answer,$randomize)=@_;
220:
221: my @truelist;
222: my @falselist;
223: my @whichfalse =();
224:
225: my @names;
226: if ( $Apache::response::foilgroup{'names'} ) {
227: @names= @{ $Apache::response::foilgroup{'names'} };
228: }
229: if (&Apache::response::showallfoils()) {
230: @whichfalse=@names;
231: } elsif ($randomize=='no') {
232: &Apache::lonxml::debug("No randomization");
233: my $havetrue=0;
234: foreach my $name (@names) {
235: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
236: if (!$havetrue ) {
237: push (@whichfalse,$name);
238: $havetrue++;
239: }
240: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
241: push (@whichfalse,$name);
242: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
243: } else {
244: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
245: }
246: }
247: } else {
248: foreach my $name (@names) {
249: #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
250: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
251: push (@truelist,$name);
252: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
253: push (@falselist,$name);
254: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
255: } else {
256: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
257: }
258: }
259: my $whichtrue = int(rand($#truelist+1));
260: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
261: while ((($#whichfalse+1) < $max-1) && ($#falselist > -1)) {
262: &Apache::lonxml::debug("Have $#whichfalse max is $max");
263: my $afalse=int(rand($#falselist+1));
264: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
265: $afalse=splice(@falselist,$afalse,1);
266: &Apache::lonxml::debug("Picked $afalse");
267: push (@whichfalse,$afalse);
268: }
269: splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
270: &Apache::lonxml::debug("the true statement is $answer");
271: }
272: return @whichfalse;
273: }
274:
275: sub displayfoils {
276: my ($target,$max,$answer,$randomize)=@_;
277: my $result;
278:
279: my @whichfoils=&whichfoils($max,$answer,$randomize);
280: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ || ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER')) {
281: foreach my $name (@whichfoils) {
282: if ($target ne 'tex') {
283: $result.="<br />";
284: } else {
285: $result.='\vskip 0 mm \item ';
286: }
287: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
288: if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: ';}
289: } else {
290: $result.='Incorrect:';
291: }
292: if ($target ne 'tex') {
293: $result.=$Apache::response::foilgroup{$name.'.text'}."</input>\n";
294: } else {
295: $result.=$Apache::response::foilgroup{$name.'.text'};
296: }
297: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
298: if ($target ne 'tex') { $result.='</b>';}
299: }
300: }
301: } else {
302: my $temp=0;
303: my $id=$Apache::inputtags::response['-1'];
304: my $part=$Apache::inputtags::part;
305: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
306: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
307: foreach my $name (@whichfoils) {
308: if ($target ne 'tex') {
309: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
310: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
311: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
312: } else {
313: $result .= '\item '.$Apache::response::foilgroup{$name.'.text'};
314: }
315: $temp++;
316: }
317: }
318: if ($target ne 'tex') { $result.="<br />"; } else { $result.='\vskip 0 mm '; }
319: return $result;
320: }
321:
322: sub displayanswers {
323: my ($max,$answer,$randomize)=@_;
324: my @whichopt = &whichfoils($max,$answer,$randomize);
325: my $result=&Apache::response::answer_header('radiobuttonresponse');
326: foreach my $name (@whichopt) {
327: $result.=&Apache::response::answer_part('radiobuttonresponse',
328: $Apache::response::foilgroup{$name.'.value'})
329: }
330: $result.=&Apache::response::answer_footer('radiobuttonresponse');
331: return $result;
332: }
333:
334: sub start_conceptgroup {
335: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
336: $Apache::radiobuttonresponse::conceptgroup=1;
337: %Apache::response::conceptgroup=();
338: my $result;
339: if ($target eq 'edit') {
340: $result.=&Apache::edit::tag_start($target,$token);
341: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
342: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
343: } elsif ($target eq 'modified') {
344: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
345: $safeeval,'concept');
346: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
347: }
348: return $result;
349: }
350:
351: sub end_conceptgroup {
352: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
353: $Apache::radiobuttonresponse::conceptgroup=0;
354: my $result;
355: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
356: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
357: my @names = @{ $Apache::response::conceptgroup{'names'} };
358: my $pick=int(rand($#names+1));
359: my $name=$names[$pick];
360: push @{ $Apache::response::foilgroup{'names'} }, $name;
361: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
362: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
363: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
364: $Apache::response::foilgroup{"$name.concept"} = $concept;
365: &Apache::lonxml::debug("Selecting $name in $concept");
366: }
367: } elsif ($target eq 'edit') {
368: $result=&Apache::edit::end_table();
369: }
370: return $result;
371: }
372:
373: sub insert_conceptgroup {
374: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
375: return $result;
376: }
377:
378: sub start_foil {
379: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
380: my $result='';
381: if ($target eq 'web' || $target eq 'tex') {
382: &Apache::lonxml::startredirection;
383: } elsif ($target eq 'edit') {
384: $result=&Apache::edit::tag_start($target,$token);
385: $result.=&Apache::edit::text_arg('Name:','name',$token);
386: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
387: ['unused','true','false'],$token);
388: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
389: } elsif ($target eq 'modified') {
390: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
391: 'value','name');
392: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
393: }
394: return $result;
395: }
396:
397: sub end_foil {
398: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
399: my $text='';
400: if ($target eq 'web' || $target eq 'tex') { $text=&Apache::lonxml::endredirection; }
401: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
402: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
403: if ($value ne 'unused') {
404: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
405: if (!$name) { $name=$Apache::lonxml::curdepth; }
406: if ( $Apache::radiobuttonresponse::conceptgroup
407: && !&Apache::response::showallfoils() ) {
408: push @{ $Apache::response::conceptgroup{'names'} }, $name;
409: $Apache::response::conceptgroup{"$name.value"} = $value;
410: $Apache::response::conceptgroup{"$name.text"} = $text;
411: } else {
412: push @{ $Apache::response::foilgroup{'names'} }, $name;
413: $Apache::response::foilgroup{"$name.value"} = $value;
414: $Apache::response::foilgroup{"$name.text"} = $text;
415: }
416: }
417: }
418: return '';
419: }
420:
421: sub insert_foil {
422: return '
423: <foil name="" value="unused">
424: <startouttext />
425: <endouttext />
426: </foil>';
427: }
428: 1;
429: __END__
430:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>