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