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