Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.48
1.22 albertel 1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
1.31 albertel 3: #
1.48 ! albertel 4: # $Id: radiobuttonresponse.pm,v 1.47 2002/09/23 17:20:41 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.47 albertel 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";
1.26 albertel 59: } elsif ($target eq 'modified') {
60: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.47 albertel 61: $safeeval,'max','randomize');
1.26 albertel 62: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38 sakharuk 63: } elsif ($target eq 'tex') {
64: $result .= '\begin{enumerate}';
1.25 albertel 65: }
66: return $result;
1.1 albertel 67: }
68:
69: sub end_radiobuttonresponse {
1.26 albertel 70: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
71: my $result;
72: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.38 sakharuk 73: if ($target eq 'tex') { $result .= '\end{enumerate}'; }
1.22 albertel 74: &Apache::response::end_response;
1.26 albertel 75: pop @Apache::lonxml::namespace;
1.33 albertel 76: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.26 albertel 77: return $result;
1.1 albertel 78: }
79:
1.43 albertel 80: %Apache::response::foilgroup=();
1.1 albertel 81: sub start_foilgroup {
1.43 albertel 82: %Apache::response::foilgroup=();
1.22 albertel 83: $Apache::radiobuttonresponse::conceptgroup=0;
84: &Apache::response::setrandomnumber();
85: return '';
1.5 albertel 86: }
87:
1.15 albertel 88: sub storesurvey {
1.33 albertel 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");
1.22 albertel 100: return '';
1.15 albertel 101: }
102:
1.32 albertel 103: sub grade_response {
1.47 albertel 104: my ($max,$answer,$randomize)=@_;
1.32 albertel 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'];
1.47 albertel 110: my @whichfoils=&whichfoils($max,$answer,$randomize);
1.32 albertel 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:
1.1 albertel 129: sub end_foilgroup {
1.22 albertel 130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29 albertel 131:
1.22 albertel 132: my $result;
1.38 sakharuk 133: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.29 albertel 134: my $style = $Apache::lonhomework::type;
1.22 albertel 135: if ( $style eq 'survey' ) {
1.38 sakharuk 136: if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
1.22 albertel 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");
1.47 albertel 149: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
150: $safeeval,'-2');
1.38 sakharuk 151: if ($target eq 'web' || $target eq 'tex') {
1.47 albertel 152: $result=&displayfoils($target,$max,$answer,$randomize);
1.28 albertel 153: } elsif ($target eq 'answer' ) {
1.47 albertel 154: $result=&displayanswers($max,$answer,$randomize);
1.22 albertel 155: } elsif ( $target eq 'grade') {
1.47 albertel 156: &grade_response($max,$answer,$randomize);
1.22 albertel 157: }
1.5 albertel 158: }
1.22 albertel 159: }
160: return $result;
1.6 albertel 161: }
162:
163: sub getfoilcounts {
1.22 albertel 164: my ($parstack,$safeeval)=@_;
165: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
1.34 albertel 166: my @names;
1.22 albertel 167: my $truecnt=0;
168: my $falsecnt=0;
169: my $name;
1.34 albertel 170: if ( $Apache::response::foilgroup{'names'} ) {
171: @names= @{ $Apache::response::foilgroup{'names'} };
172: }
1.22 albertel 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++;
1.6 albertel 178: }
1.22 albertel 179: }
180: return ($truecnt,$falsecnt,$max);
1.5 albertel 181: }
182:
1.15 albertel 183: sub displayallfoils {
1.22 albertel 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"};
1.32 albertel 191: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.45 albertel 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: }
1.15 albertel 213: }
1.22 albertel 214: }
215: return $result;
1.15 albertel 216: }
217:
1.28 albertel 218: sub whichfoils {
1.47 albertel 219: my ($max,$answer,$randomize)=@_;
1.28 albertel 220:
1.22 albertel 221: my @truelist;
222: my @falselist;
1.41 albertel 223: my @whichfalse =();
1.22 albertel 224:
1.34 albertel 225: my @names;
226: if ( $Apache::response::foilgroup{'names'} ) {
227: @names= @{ $Apache::response::foilgroup{'names'} };
228: }
1.41 albertel 229: if (&Apache::response::showallfoils()) {
230: @whichfalse=@names;
1.48 ! albertel 231: } elsif ($randomize eq 'no') {
1.47 albertel 232: &Apache::lonxml::debug("No randomization");
233: my $havetrue=0;
234: foreach my $name (@names) {
235: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
1.48 ! albertel 236: if (!$havetrue ) { push(@whichfalse,$name); $havetrue++; }
1.47 albertel 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: }
1.41 albertel 244: } else {
245: foreach my $name (@names) {
246: #result.="<br /><b>$name</b> is <i> $Apache::response::foilgroup{$name.'.value'} </i>";
247: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
248: push (@truelist,$name);
249: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
250: push (@falselist,$name);
251: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
252: } else {
1.42 albertel 253: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
1.41 albertel 254: }
255: }
256: my $whichtrue = int(rand($#truelist+1));
257: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
258: while ((($#whichfalse+1) < $max-1) && ($#falselist > -1)) {
259: &Apache::lonxml::debug("Have $#whichfalse max is $max");
260: my $afalse=int(rand($#falselist+1));
261: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
262: $afalse=splice(@falselist,$afalse,1);
263: &Apache::lonxml::debug("Picked $afalse");
264: push (@whichfalse,$afalse);
1.3 albertel 265: }
1.41 albertel 266: splice(@whichfalse,$answer,0,$truelist[$whichtrue]);
267: &Apache::lonxml::debug("the true statement is $answer");
1.22 albertel 268: }
1.28 albertel 269: return @whichfalse;
270: }
271:
272: sub displayfoils {
1.47 albertel 273: my ($target,$max,$answer,$randomize)=@_;
1.28 albertel 274: my $result;
275:
1.47 albertel 276: my @whichfoils=&whichfoils($max,$answer,$randomize);
1.45 albertel 277: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ || ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER')) {
1.28 albertel 278: foreach my $name (@whichfoils) {
1.38 sakharuk 279: if ($target ne 'tex') {
280: $result.="<br />";
1.44 sakharuk 281: } else {
1.46 sakharuk 282: $result.='\vskip 0 mm \item ';
1.38 sakharuk 283: }
1.22 albertel 284: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
1.46 sakharuk 285: if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: ';}
1.22 albertel 286: } else {
1.45 albertel 287: $result.='Incorrect:';
288: }
1.46 sakharuk 289: if ($target ne 'tex') {
290: $result.=$Apache::response::foilgroup{$name.'.text'}."</input>\n";
291: } else {
292: $result.=$Apache::response::foilgroup{$name.'.text'};
293: }
1.45 albertel 294: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
1.46 sakharuk 295: if ($target ne 'tex') { $result.='</b>';}
1.22 albertel 296: }
1.10 albertel 297: }
1.22 albertel 298: } else {
299: my $temp=0;
300: my $id=$Apache::inputtags::response['-1'];
301: my $part=$Apache::inputtags::part;
302: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
1.32 albertel 303: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.28 albertel 304: foreach my $name (@whichfoils) {
1.38 sakharuk 305: if ($target ne 'tex') {
306: $result.="<br /><input type=\"radio\" name=\"HWVAL$Apache::inputtags::response['-1']\" value=\"$temp\" ";
307: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
308: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
309: } else {
1.44 sakharuk 310: $result .= '\item '.$Apache::response::foilgroup{$name.'.text'};
1.38 sakharuk 311: }
1.22 albertel 312: $temp++;
1.10 albertel 313: }
1.22 albertel 314: }
1.44 sakharuk 315: if ($target ne 'tex') { $result.="<br />"; } else { $result.='\vskip 0 mm '; }
1.39 albertel 316: return $result;
1.14 albertel 317: }
318:
1.28 albertel 319: sub displayanswers {
1.47 albertel 320: my ($max,$answer,$randomize)=@_;
321: my @whichopt = &whichfoils($max,$answer,$randomize);
1.28 albertel 322: my $result=&Apache::response::answer_header('radiobuttonresponse');
323: foreach my $name (@whichopt) {
324: $result.=&Apache::response::answer_part('radiobuttonresponse',
325: $Apache::response::foilgroup{$name.'.value'})
326: }
327: $result.=&Apache::response::answer_footer('radiobuttonresponse');
328: return $result;
329: }
330:
1.14 albertel 331: sub start_conceptgroup {
1.27 albertel 332: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.22 albertel 333: $Apache::radiobuttonresponse::conceptgroup=1;
1.43 albertel 334: %Apache::response::conceptgroup=();
1.26 albertel 335: my $result;
336: if ($target eq 'edit') {
337: $result.=&Apache::edit::tag_start($target,$token);
338: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
1.35 matthew 339: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.26 albertel 340: } elsif ($target eq 'modified') {
341: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
342: $safeeval,'concept');
343: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
344: }
345: return $result;
1.14 albertel 346: }
347:
348: sub end_conceptgroup {
1.22 albertel 349: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
350: $Apache::radiobuttonresponse::conceptgroup=0;
1.26 albertel 351: my $result;
1.38 sakharuk 352: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.27 albertel 353: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
354: my @names = @{ $Apache::response::conceptgroup{'names'} };
1.28 albertel 355: my $pick=int(rand($#names+1));
1.27 albertel 356: my $name=$names[$pick];
357: push @{ $Apache::response::foilgroup{'names'} }, $name;
358: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
359: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
360: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
361: $Apache::response::foilgroup{"$name.concept"} = $concept;
362: &Apache::lonxml::debug("Selecting $name in $concept");
363: }
1.26 albertel 364: } elsif ($target eq 'edit') {
365: $result=&Apache::edit::end_table();
1.22 albertel 366: }
1.26 albertel 367: return $result;
368: }
369:
370: sub insert_conceptgroup {
371: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
372: return $result;
1.1 albertel 373: }
374:
375: sub start_foil {
1.24 albertel 376: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
377: my $result='';
1.38 sakharuk 378: if ($target eq 'web' || $target eq 'tex') {
1.27 albertel 379: &Apache::lonxml::startredirection;
380: } elsif ($target eq 'edit') {
381: $result=&Apache::edit::tag_start($target,$token);
382: $result.=&Apache::edit::text_arg('Name:','name',$token);
1.42 albertel 383: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
1.28 albertel 384: ['unused','true','false'],$token);
1.48 ! albertel 385: $result.=&Apache::edit::select_or_text_arg('Location:','location',
! 386: ['random','bottom','top'],$token);
1.35 matthew 387: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.27 albertel 388: } elsif ($target eq 'modified') {
389: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
1.48 ! albertel 390: 'value','name','location');
1.27 albertel 391: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38 sakharuk 392: }
1.27 albertel 393: return $result;
1.1 albertel 394: }
395:
396: sub end_foil {
1.22 albertel 397: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
398: my $text='';
1.38 sakharuk 399: if ($target eq 'web' || $target eq 'tex') { $text=&Apache::lonxml::endredirection; }
400: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
1.19 albertel 401: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
1.18 albertel 402: if ($value ne 'unused') {
1.19 albertel 403: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.48 ! albertel 404: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
1.27 albertel 405: if (!$name) { $name=$Apache::lonxml::curdepth; }
1.41 albertel 406: if ( $Apache::radiobuttonresponse::conceptgroup
407: && !&Apache::response::showallfoils() ) {
1.18 albertel 408: push @{ $Apache::response::conceptgroup{'names'} }, $name;
409: $Apache::response::conceptgroup{"$name.value"} = $value;
410: $Apache::response::conceptgroup{"$name.text"} = $text;
1.48 ! albertel 411: $Apache::response::conceptgroup{"$name.location"} = $location;
1.18 albertel 412: } else {
413: push @{ $Apache::response::foilgroup{'names'} }, $name;
414: $Apache::response::foilgroup{"$name.value"} = $value;
415: $Apache::response::foilgroup{"$name.text"} = $text;
1.48 ! albertel 416: $Apache::response::foilgroup{"$name.location"} = $location;
1.18 albertel 417: }
418: }
1.4 albertel 419: }
1.1 albertel 420: return '';
421: }
422:
1.27 albertel 423: sub insert_foil {
424: return '
425: <foil name="" value="unused">
426: <startouttext />
427: <endouttext />
428: </foil>';
429: }
1.1 albertel 430: 1;
431: __END__
432:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>