Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.33
1.22 albertel 1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
1.31 albertel 3: #
1.33 ! albertel 4: # $Id: radiobuttonresponse.pm,v 1.32 2001/12/13 23:36:39 albertel Exp $
1.31 albertel 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: #
1.22 albertel 28: # 2/21 Guy
1.1 albertel 29:
30: package Apache::radiobuttonresponse;
31: use strict;
32:
33: sub BEGIN {
1.22 albertel 34: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1 albertel 35: }
36:
37: sub start_radiobuttonresponse {
1.22 albertel 38: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.26 albertel 39: my $result;
1.3 albertel 40: #when in a radiobutton response use these
1.22 albertel 41: &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26 albertel 42: push (@Apache::lonxml::namespace,'radiobuttonresponse');
1.4 albertel 43: my $id = &Apache::response::start_response($parstack,$safeeval);
1.25 albertel 44: if ($target eq 'meta') {
45: $result=&Apache::response::meta_package_write('radiobuttonresponse');
1.26 albertel 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); }
1.25 albertel 58: }
59: return $result;
1.1 albertel 60: }
61:
62: sub end_radiobuttonresponse {
1.26 albertel 63: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
64: my $result;
65: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.22 albertel 66: &Apache::response::end_response;
1.26 albertel 67: pop @Apache::lonxml::namespace;
1.33 ! albertel 68: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26 albertel 69: return $result;
1.1 albertel 70: }
71:
1.22 albertel 72: %Apache::response::foilgroup={};
1.1 albertel 73: sub start_foilgroup {
1.22 albertel 74: %Apache::response::foilgroup={};
75: $Apache::radiobuttonresponse::conceptgroup=0;
76: &Apache::response::setrandomnumber();
77: return '';
1.5 albertel 78: }
79:
1.15 albertel 80: sub storesurvey {
1.33 ! albertel 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");
1.22 albertel 92: return '';
1.15 albertel 93: }
94:
1.32 albertel 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:
1.1 albertel 121: sub end_foilgroup {
1.22 albertel 122: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29 albertel 123:
1.22 albertel 124: my $result;
1.28 albertel 125: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
1.29 albertel 126: my $style = $Apache::lonhomework::type;
1.22 albertel 127: if ( $style eq 'survey' ) {
1.30 albertel 128: if ($target eq 'web' || $target eq 'answer') {
1.22 albertel 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);
1.28 albertel 143: } elsif ($target eq 'answer' ) {
144: $result=&displayanswers($max,$answer);
1.22 albertel 145: } elsif ( $target eq 'grade') {
1.32 albertel 146: &grade_response($max,$answer);
1.22 albertel 147: }
1.5 albertel 148: }
1.22 albertel 149: }
150: return $result;
1.6 albertel 151: }
152:
153: sub getfoilcounts {
1.22 albertel 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++;
1.6 albertel 166: }
1.22 albertel 167: }
168: return ($truecnt,$falsecnt,$max);
1.5 albertel 169: }
170:
1.15 albertel 171: sub displayallfoils {
1.22 albertel 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"};
1.32 albertel 179: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
180: &Apache::lonhomework::showhash(%lastresponse);
1.22 albertel 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\" ";
1.32 albertel 184: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.22 albertel 185: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
186: $temp++;
1.15 albertel 187: }
1.22 albertel 188: }
189: return $result;
1.15 albertel 190: }
191:
1.28 albertel 192: sub whichfoils {
1.22 albertel 193: my ($max,$answer)=@_;
1.28 albertel 194:
1.22 albertel 195: my @truelist;
196: my @falselist;
197:
1.28 albertel 198: my @names = @{ $Apache::response::foilgroup{'names'} };
199: foreach my $name (@names) {
1.22 albertel 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>");
1.3 albertel 208: }
1.22 albertel 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");
1.28 albertel 223: return @whichfalse;
224: }
225:
226: sub displayfoils {
227: my ($max,$answer)=@_;
228: my $result;
229:
230: my @whichfoils=&whichfoils($max,$answer);
1.22 albertel 231: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
1.28 albertel 232: foreach my $name (@whichfoils) {
1.22 albertel 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";
1.10 albertel 240: }
1.22 albertel 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"};
1.32 albertel 246: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.28 albertel 247: foreach my $name (@whichfoils) {
1.22 albertel 248: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
1.32 albertel 249: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.22 albertel 250: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
251: $temp++;
1.10 albertel 252: }
1.22 albertel 253: }
254: return $result."<br />";
1.14 albertel 255: }
256:
1.28 albertel 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:
1.14 albertel 270: sub start_conceptgroup {
1.27 albertel 271: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22 albertel 272: $Apache::radiobuttonresponse::conceptgroup=1;
273: %Apache::response::conceptgroup={};
1.26 albertel 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;
1.14 albertel 285: }
286:
287: sub end_conceptgroup {
1.22 albertel 288: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
289: $Apache::radiobuttonresponse::conceptgroup=0;
1.26 albertel 290: my $result;
1.28 albertel 291: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.27 albertel 292: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
293: my @names = @{ $Apache::response::conceptgroup{'names'} };
1.28 albertel 294: my $pick=int(rand($#names+1));
1.27 albertel 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: }
1.26 albertel 303: } elsif ($target eq 'edit') {
304: $result=&Apache::edit::end_table();
1.22 albertel 305: }
1.26 albertel 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;
1.1 albertel 312: }
313:
314: sub start_foil {
1.24 albertel 315: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
316: my $result='';
1.27 albertel 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',
1.28 albertel 323: ['unused','true','false'],$token);
1.27 albertel 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;
1.1 albertel 330: }
331:
332: sub end_foil {
1.22 albertel 333: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
334: my $text='';
335: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.28 albertel 336: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
1.19 albertel 337: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18 albertel 338: if ($value ne 'unused') {
1.19 albertel 339: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.27 albertel 340: if (!$name) { $name=$Apache::lonxml::curdepth; }
1.18 albertel 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: }
1.4 albertel 351: }
1.1 albertel 352: return '';
353: }
354:
1.27 albertel 355: sub insert_foil {
356: return '
357: <foil name="" value="unused">
358: <startouttext />
359: <endouttext />
360: </foil>';
361: }
1.1 albertel 362: 1;
363: __END__
364:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>