1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.49 2002/09/23 22:32: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,$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 ($answer,@whichfoils)=&whichfoils($max,$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 $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
144: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
145: $safeeval,'-2');
146: if ($target eq 'web' || $target eq 'tex') {
147: $result=&displayfoils($target,$max,$randomize);
148: } elsif ($target eq 'answer' ) {
149: $result=&displayanswers($max,$randomize);
150: } elsif ( $target eq 'grade') {
151: &grade_response($max,$randomize);
152: }
153: }
154: }
155: return $result;
156: }
157:
158: sub getfoilcounts {
159: my ($max)=@_;
160: my @names;
161: my $truecnt=0;
162: my $falsecnt=0;
163: my $name;
164: if ( $Apache::response::foilgroup{'names'} ) {
165: @names= @{ $Apache::response::foilgroup{'names'} };
166: }
167: foreach $name (@names) {
168: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
169: $truecnt++;
170: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
171: $falsecnt++;
172: }
173: }
174: return ($truecnt,$falsecnt,$max);
175: }
176:
177: sub displayallfoils {
178: my $result;
179: &Apache::lonxml::debug("survey style display");
180: my @names = @{ $Apache::response::foilgroup{'names'} };
181: my $temp=0;
182: my $id=$Apache::inputtags::response['-1'];
183: my $part=$Apache::inputtags::part;
184: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
185: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
186: if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/) || ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER')) {
187: foreach my $name (@names) {
188: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
189: $result.="<br />".$Apache::response::foilgroup{$name.'.value'};
190: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
191: $result.='<b>';
192: }
193: $result .= $Apache::response::foilgroup{$name.'.text'};
194: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
195: $result.='</b>';
196: }
197: }
198: }
199: } else {
200: foreach my $name (@names) {
201: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
202: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
203: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
204: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
205: $temp++;
206: }
207: }
208: }
209: return $result;
210: }
211:
212: sub whichfoils {
213: my ($max,$randomize)=@_;
214:
215: my @truelist;
216: my @falselist;
217: my @whichfalse =();
218: my ($truecnt,$falsecnt,$max) = &getfoilcounts($max);
219: my $count=0;
220: # we will add in 1 of the true statements
221: if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
222: my $answer=int(rand($count));
223: &Apache::lonxml::debug("Count is $count, $answer is $answer");
224: my @names;
225: if ( $Apache::response::foilgroup{'names'} ) {
226: @names= @{ $Apache::response::foilgroup{'names'} };
227: }
228: if (&Apache::response::showallfoils()) {
229: @whichfalse=@names;
230: } elsif ($randomize eq 'no') {
231: &Apache::lonxml::debug("No randomization");
232: my $havetrue=0;
233: foreach my $name (@names) {
234: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
235: if (!$havetrue ) {
236: push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
237: }
238: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
239: push (@whichfalse,$name);
240: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
241: } else {
242: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
243: }
244: }
245: } else {
246: my $current=0;
247: &Apache::lonhomework::showhash(%Apache::response::foilgroup);
248: my (%top,%bottom);
249: #first find out where everyone wants to be
250: foreach my $name (@names) {
251: $current++;
252: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
253: push (@truelist,$name);
254: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
255: $top{$name}=$current;
256: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
257: $bottom{$name}=$current;
258: }
259: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
260: push (@falselist,$name);
261: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
262: $top{$name}=$current;
263: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
264: $bottom{$name}=$current;
265: }
266: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
267: } else {
268: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
269: }
270: }
271: #pick a true statement
272: my $whichtrue = int(rand($#truelist+1));
273: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
274: my $numinserted;
275: my (@toplist, @bottomlist);
276: my $topcount=0;
277: # assign everyone to either toplist/bottomlist or whichfalse
278: # which false is randomized, toplist bottomlist are in order
279: while ((($numinserted) < $max-1) && ($#falselist > -1)) {
280: &Apache::lonxml::debug("Have $#whichfalse max is $max");
281: my $afalse=int(rand($#falselist+1));
282: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
283: $afalse=splice(@falselist,$afalse,1);
284: &Apache::lonxml::debug("Picked $afalse");
285: &Apache::lonhomework::showhash(('names'=>\@names));
286: &Apache::lonhomework::showhash(%top);
287: if ($top{$afalse}) {
288: $toplist[$top{$afalse}]=$afalse;
289: $topcount++;
290: } elsif ($bottom{$afalse}) {
291: $bottomlist[$bottom{$afalse}]=$afalse;
292: } else {
293: push (@whichfalse,$afalse);
294: }
295: }
296: my $truename=$truelist[$whichtrue];
297: my $dosplice=1;
298: #insert the true statement, keeping track of where it wants to be
299: if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' ) {
300: $toplist[$top{$truename}]=$truename;
301: $answer=-1;
302: foreach my $top (reverse(@toplist)) {
303: if ($top) { $answer++;}
304: if ($top eq $truename) { last; }
305: }
306: $dosplice=0;
307: } elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom') {
308: $bottomlist[$bottom{$truename}]=$truename;
309: $answer=-1;
310: foreach my $bot (@bottomlist) {
311: if ($bot) { $answer++;}
312: if ($bot eq $truename) { last; }
313: }
314: $answer+=$topcount+$#whichfalse+1;
315: $dosplice=0;
316: } else {
317: if ($topcount>0) { $answer = int(rand($#whichfalse+1))+$topcount; }
318: }
319: #add the top items to the top, bottom items to the bottom
320: for (my $i=0;$i<=$#toplist;$i++) {
321: if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
322: }
323: for (my $i=0;$i<=$#bottomlist;$i++) {
324: if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
325: }
326: #if the true statement is randomized insert it into the list
327: if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
328: }
329: &Apache::lonxml::debug("Answer is $answer");
330: return ($answer,@whichfalse);
331: }
332:
333: sub displayfoils {
334: my ($target,$max,$randomize)=@_;
335: my $result;
336:
337: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
338: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ || ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER')) {
339: foreach my $name (@whichfoils) {
340: if ($target ne 'tex') {
341: $result.="<br />";
342: } else {
343: $result.='\vskip 0 mm \item ';
344: }
345: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
346: if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: ';}
347: } else {
348: $result.='Incorrect:';
349: }
350: if ($target ne 'tex') {
351: $result.=$Apache::response::foilgroup{$name.'.text'}."</input>\n";
352: } else {
353: $result.=$Apache::response::foilgroup{$name.'.text'};
354: }
355: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
356: if ($target ne 'tex') { $result.='</b>';}
357: }
358: }
359: } else {
360: my $temp=0;
361: my $id=$Apache::inputtags::response['-1'];
362: my $part=$Apache::inputtags::part;
363: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
364: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
365: foreach my $name (@whichfoils) {
366: if ($target ne 'tex') {
367: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
368: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
369: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
370: } else {
371: $result .= '\item '.$Apache::response::foilgroup{$name.'.text'};
372: }
373: $temp++;
374: }
375: }
376: if ($target ne 'tex') { $result.="<br />"; } else { $result.='\vskip 0 mm '; }
377: return $result;
378: }
379:
380: sub displayanswers {
381: my ($max,$randomize)=@_;
382: my ($answer,@whichopt) = &whichfoils($max,$randomize);
383: my $result=&Apache::response::answer_header('radiobuttonresponse');
384: foreach my $name (@whichopt) {
385: $result.=&Apache::response::answer_part('radiobuttonresponse',
386: $Apache::response::foilgroup{$name.'.value'})
387: }
388: $result.=&Apache::response::answer_footer('radiobuttonresponse');
389: return $result;
390: }
391:
392: sub start_conceptgroup {
393: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
394: $Apache::radiobuttonresponse::conceptgroup=1;
395: %Apache::response::conceptgroup=();
396: my $result;
397: if ($target eq 'edit') {
398: $result.=&Apache::edit::tag_start($target,$token);
399: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
400: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
401: } elsif ($target eq 'modified') {
402: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
403: $safeeval,'concept');
404: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
405: }
406: return $result;
407: }
408:
409: sub end_conceptgroup {
410: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
411: $Apache::radiobuttonresponse::conceptgroup=0;
412: my $result;
413: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
414: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
415: my @names = @{ $Apache::response::conceptgroup{'names'} };
416: my $pick=int(rand($#names+1));
417: my $name=$names[$pick];
418: push @{ $Apache::response::foilgroup{'names'} }, $name;
419: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
420: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
421: $Apache::response::foilgroup{"$name.location"} = $Apache::response::conceptgroup{"$name.location"};
422: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
423: $Apache::response::foilgroup{"$name.concept"} = $concept;
424: &Apache::lonxml::debug("Selecting $name in $concept");
425: }
426: } elsif ($target eq 'edit') {
427: $result=&Apache::edit::end_table();
428: }
429: return $result;
430: }
431:
432: sub insert_conceptgroup {
433: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
434: return $result;
435: }
436:
437: sub start_foil {
438: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
439: my $result='';
440: if ($target eq 'web' || $target eq 'tex') {
441: &Apache::lonxml::startredirection;
442: } elsif ($target eq 'edit') {
443: $result=&Apache::edit::tag_start($target,$token);
444: $result.=&Apache::edit::text_arg('Name:','name',$token);
445: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
446: ['unused','true','false'],$token);
447: $result.=&Apache::edit::select_arg('Location:','location',
448: ['random','bottom','top'],$token);
449: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
450: } elsif ($target eq 'modified') {
451: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
452: 'value','name','location');
453: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
454: }
455: return $result;
456: }
457:
458: sub end_foil {
459: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
460: my $text='';
461: if ($target eq 'web' || $target eq 'tex') { $text=&Apache::lonxml::endredirection; }
462: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
463: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
464: if ($value ne 'unused') {
465: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
466: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
467: if (!$name) { $name=$Apache::lonxml::curdepth; }
468: if ( $Apache::radiobuttonresponse::conceptgroup
469: && !&Apache::response::showallfoils() ) {
470: push @{ $Apache::response::conceptgroup{'names'} }, $name;
471: $Apache::response::conceptgroup{"$name.value"} = $value;
472: $Apache::response::conceptgroup{"$name.text"} = $text;
473: $Apache::response::conceptgroup{"$name.location"} = $location;
474: } else {
475: push @{ $Apache::response::foilgroup{'names'} }, $name;
476: $Apache::response::foilgroup{"$name.value"} = $value;
477: $Apache::response::foilgroup{"$name.text"} = $text;
478: $Apache::response::foilgroup{"$name.location"} = $location;
479: }
480: }
481: }
482: return '';
483: }
484:
485: sub insert_foil {
486: return '
487: <foil name="" value="unused">
488: <startouttext />
489: <endouttext />
490: </foil>';
491: }
492: 1;
493: __END__
494:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>