1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.69 2003/04/18 06:26:43 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: %Apache::hint::radiobutton=();
46: if ($target eq 'meta') {
47: $result=&Apache::response::meta_package_write('radiobuttonresponse');
48: } elsif ($target eq 'edit' ) {
49: $result.=&Apache::edit::start_table($token).
50: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
51: &Apache::edit::deletelist($target,$token)
52: ."</td><td> ".&Apache::edit::end_row()
53: .&Apache::edit::start_spanning_row();
54:
55: $result.=
56: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
57: &Apache::edit::select_arg('Randomize Foil Order','randomize',
58: ['yes','no'],$token).
59: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
60: } elsif ($target eq 'modified') {
61: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
62: $safeeval,'max','randomize');
63: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
64: } elsif ($target eq 'tex') {
65: $result .= '\begin{enumerate}';
66: }
67: return $result;
68: }
69:
70: sub end_radiobuttonresponse {
71: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
72: my $result;
73: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
74: if ($target eq 'tex') { $result .= '\end{enumerate}'; }
75: &Apache::response::end_response;
76: pop @Apache::lonxml::namespace;
77: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
78: return $result;
79: }
80:
81: %Apache::response::foilgroup=();
82: sub start_foilgroup {
83: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
84: my $result;
85: %Apache::response::foilgroup=();
86: $Apache::radiobuttonresponse::conceptgroup=0;
87: &Apache::response::setrandomnumber();
88: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
89: $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
90: }
91: return $result;
92: }
93:
94: sub storesurvey {
95: if ( !defined($ENV{'form.submitted'})) { return ''; }
96: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1']};
97: &Apache::lonxml::debug("Here I am!:$response:");
98: if ( $response !~ /[0-9]+/) { return ''; }
99: my $id = $Apache::inputtags::response['-1'];
100: my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
101: my %responsehash;
102: $responsehash{$whichfoils[$response]}=$response;
103: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=&Apache::lonnet::hash2str(%responsehash);
104: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='SUBMITTED';
105: &Apache::lonxml::debug("submitted a $response<br />\n");
106: return '';
107: }
108:
109: sub grade_response {
110: my ($max,$randomize)=@_;
111: #keep the random numbers the same must always call this
112: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
113: if (!defined($ENV{'form.submitted'})) { return; }
114: my $response;
115: if ($ENV{'form.submitted'} eq 'scantron') {
116: $response=&Apache::response::getresponse();
117: } else {
118: $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1']};
119: }
120: if ( $response !~ /[0-9]+/) { return; }
121: my $part=$Apache::inputtags::part;
122: my $id = $Apache::inputtags::response['-1'];
123: my %responsehash;
124: $responsehash{$whichfoils[$response]}=$response;
125: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
126: my %previous=&Apache::response::check_for_previous($responsestr,
127: $part,$id);
128: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
129: $responsestr;
130: &Apache::lonxml::debug("submitted a $response<br />\n");
131: my $ad;
132: if ($response == $answer) {
133: $ad='EXACT_ANS';
134: } else {
135: $ad='INCORRECT';
136: }
137: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
138: &Apache::response::handle_previous(\%previous,$ad);
139: }
140:
141: sub end_foilgroup {
142: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
143:
144: my $result;
145: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
146: my $style = $Apache::lonhomework::type;
147: if ( $style eq 'survey' ) {
148: if ($target eq 'web' || $target eq 'answer' || $target eq 'tex') {
149: $result=&displayallfoils();
150: } elsif ( $target eq 'grade' ) {
151: $result=&storesurvey();
152: }
153: } else {
154: my $name;
155: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
156: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
157: $safeeval,'-2');
158: if ($target eq 'web' || $target eq 'tex') {
159: $result=&displayfoils($target,$max,$randomize);
160: } elsif ($target eq 'answer' ) {
161: $result=&displayanswers($max,$randomize);
162: } elsif ( $target eq 'grade') {
163: &grade_response($max,$randomize);
164: }
165: }
166: }
167: &Apache::lonxml::increment_counter();
168: return $result;
169: }
170:
171: sub getfoilcounts {
172: my @names;
173: my $truecnt=0;
174: my $falsecnt=0;
175: my $name;
176: if ( $Apache::response::foilgroup{'names'} ) {
177: @names= @{ $Apache::response::foilgroup{'names'} };
178: }
179: foreach $name (@names) {
180: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
181: $truecnt++;
182: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
183: $falsecnt++;
184: }
185: }
186: return ($truecnt,$falsecnt);
187: }
188:
189: sub displayallfoils {
190: my $result;
191: &Apache::lonxml::debug("survey style display");
192: my @names = @{ $Apache::response::foilgroup{'names'} };
193: my $temp=0;
194: my $id=$Apache::inputtags::response['-1'];
195: my $part=$Apache::inputtags::part;
196: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
197: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
198: if (($Apache::lonhomework::history{"resource.$part.solved"} =~ /^correct/) || ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER')) {
199: foreach my $name (@names) {
200: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
201: $result.="<br />".$Apache::response::foilgroup{$name.'.value'};
202: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
203: $result.='<b>';
204: }
205: $result .= $Apache::response::foilgroup{$name.'.text'};
206: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
207: $result.='</b>';
208: }
209: }
210: }
211: } else {
212: foreach my $name (@names) {
213: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
214: $result.="<br /><input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
215: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
216: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
217: $temp++;
218: }
219: }
220: }
221: return $result;
222: }
223:
224: sub whichfoils {
225: my ($max,$randomize)=@_;
226:
227: my @truelist;
228: my @falselist;
229: my @whichfalse =();
230: my ($truecnt,$falsecnt) = &getfoilcounts();
231: my $count=0;
232: # we will add in 1 of the true statements
233: if (($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; }
234: my $answer=int(&Math::Random::random_uniform() * ($count));
235: &Apache::lonxml::debug("Count is $count, $answer is $answer");
236: my @names;
237: if ( $Apache::response::foilgroup{'names'} ) {
238: @names= @{ $Apache::response::foilgroup{'names'} };
239: }
240: if (&Apache::response::showallfoils()) {
241: @whichfalse=@names;
242: } elsif ($randomize eq 'no') {
243: &Apache::lonxml::debug("No randomization");
244: my $havetrue=0;
245: foreach my $name (@names) {
246: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
247: if (!$havetrue ) {
248: push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
249: }
250: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
251: push (@whichfalse,$name);
252: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
253: } else {
254: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
255: }
256: }
257: } else {
258: my $current=0;
259: &Apache::lonhomework::showhash(%Apache::response::foilgroup);
260: my (%top,%bottom);
261: #first find out where everyone wants to be
262: foreach my $name (@names) {
263: $current++;
264: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
265: push (@truelist,$name);
266: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
267: $top{$name}=$current;
268: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
269: $bottom{$name}=$current;
270: }
271: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
272: push (@falselist,$name);
273: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
274: $top{$name}=$current;
275: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
276: $bottom{$name}=$current;
277: }
278: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
279: } else {
280: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>"));
281: }
282: }
283: #pick a true statement
284: my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
285: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
286: my (@toplist, @bottomlist);
287: my $topcount=0;
288: # assign everyone to either toplist/bottomlist or whichfalse
289: # which false is randomized, toplist bottomlist are in order
290: while ((($#whichfalse) < $max-2) && ($#falselist > -1)) {
291: &Apache::lonxml::debug("Have $#whichfalse max is $max");
292: my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
293: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
294: $afalse=splice(@falselist,$afalse,1);
295: &Apache::lonxml::debug("Picked $afalse");
296: &Apache::lonhomework::showhash(('names'=>\@names));
297: &Apache::lonhomework::showhash(%top);
298: if ($top{$afalse}) {
299: $toplist[$top{$afalse}]=$afalse;
300: $topcount++;
301: } elsif ($bottom{$afalse}) {
302: $bottomlist[$bottom{$afalse}]=$afalse;
303: } else {
304: push (@whichfalse,$afalse);
305: }
306: }
307: my $truename=$truelist[$whichtrue];
308: my $dosplice=1;
309: #insert the true statement, keeping track of where it wants to be
310: if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' ) {
311: $toplist[$top{$truename}]=$truename;
312: $answer=-1;
313: foreach my $top (reverse(@toplist)) {
314: if ($top) { $answer++;}
315: if ($top eq $truename) { last; }
316: }
317: $dosplice=0;
318: } elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom') {
319: $bottomlist[$bottom{$truename}]=$truename;
320: $answer=-1;
321: foreach my $bot (@bottomlist) {
322: if ($bot) { $answer++;}
323: if ($bot eq $truename) { last; }
324: }
325: $answer+=$topcount+$#whichfalse+1;
326: $dosplice=0;
327: } else {
328: if ($topcount>0) {
329: $answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
330: + $topcount;
331: }
332: }
333: #add the top items to the top, bottom items to the bottom
334: for (my $i=0;$i<=$#toplist;$i++) {
335: if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
336: }
337: for (my $i=0;$i<=$#bottomlist;$i++) {
338: if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
339: }
340: #if the true statement is randomized insert it into the list
341: if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
342: }
343: &Apache::lonxml::debug("Answer is $answer");
344: return ($answer,@whichfalse);
345: }
346:
347: sub displayfoils {
348: my ($target,$max,$randomize)=@_;
349: my $result;
350:
351: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
352: my $part=$Apache::inputtags::part;
353: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
354: my $status=$Apache::inputtags::status[-1];
355: if ( ($target ne 'tex') &&
356: (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) ) {
357: foreach my $name (@whichfoils) {
358: if ($target ne 'tex') {
359: $result.="<br />";
360: } else {
361: $result.='\item \vskip -2 mm ';
362: }
363: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
364: if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
365: } else {
366: $result.='Incorrect:';
367: }
368: if ($target ne 'tex') {
369: $result.=$Apache::response::foilgroup{$name.'.text'}."</input>\n";
370: } else {
371: $result.=$Apache::response::foilgroup{$name.'.text'};
372: }
373: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
374: if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
375: }
376: }
377: } else {
378: my @alphabet = ('A'..'Z');
379: my $i = 0;
380: my $temp=0;
381: my $id=$Apache::inputtags::response['-1'];
382: my $part=$Apache::inputtags::part;
383: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
384: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
385: foreach my $name (@whichfoils) {
386: if ($target ne 'tex') {
387: $result.="<br /><input type=\"radio\" name=\"HWVAL_$Apache::inputtags::response['-1']\" value=\"$temp\" ";
388: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
389: $result .= '>'.$Apache::response::foilgroup{$name.'.text'}."</input>\n";
390: } else {
391: if ($Apache::lonhomework::type eq 'exam') {
392: $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
393: $i++;
394: } else {
395: $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
396: }
397: }
398: $temp++;
399: }
400: }
401: if ($target ne 'tex') { $result.="<br />"; } else { $result.='\vskip 0 mm '; }
402: return $result;
403: }
404:
405: sub displayanswers {
406: my ($max,$randomize)=@_;
407: my ($answer,@whichopt) = &whichfoils($max,$randomize);
408: my $result=&Apache::response::answer_header('radiobuttonresponse');
409: foreach my $name (@whichopt) {
410: $result.=&Apache::response::answer_part('radiobuttonresponse',
411: $Apache::response::foilgroup{$name.'.value'})
412: }
413: $result.=&Apache::response::answer_footer('radiobuttonresponse');
414: return $result;
415: }
416:
417: sub start_conceptgroup {
418: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
419: $Apache::radiobuttonresponse::conceptgroup=1;
420: %Apache::response::conceptgroup=();
421: my $result;
422: if ($target eq 'edit') {
423: $result.=&Apache::edit::tag_start($target,$token);
424: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
425: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
426: } elsif ($target eq 'modified') {
427: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
428: $safeeval,'concept');
429: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
430: }
431: return $result;
432: }
433:
434: sub end_conceptgroup {
435: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
436: $Apache::radiobuttonresponse::conceptgroup=0;
437: my $result;
438: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
439: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
440: my @names = @{ $Apache::response::conceptgroup{'names'} };
441: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
442: my $name=$names[$pick];
443: push @{ $Apache::response::foilgroup{'names'} }, $name;
444: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
445: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
446: $Apache::response::foilgroup{"$name.location"} = $Apache::response::conceptgroup{"$name.location"};
447: my $concept = &Apache::lonxml::get_param('concept',$parstack,$safeeval);
448: $Apache::response::foilgroup{"$name.concept"} = $concept;
449: &Apache::lonxml::debug("Selecting $name in $concept");
450: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
451: push(@{ $Apache::hint::radiobutton{"$part_id.concepts"} },$concept);
452: $Apache::hint::radiobutton{"$part_id.concept.$concept"}=
453: $Apache::response::conceptgroup{'names'};
454: }
455: } elsif ($target eq 'edit') {
456: $result=&Apache::edit::end_table();
457: }
458: return $result;
459: }
460:
461: sub insert_conceptgroup {
462: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
463: return $result;
464: }
465:
466: sub start_foil {
467: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
468: my $result='';
469: if ($target eq 'web' || $target eq 'tex') {
470: &Apache::lonxml::startredirection;
471: } elsif ($target eq 'edit') {
472: $result=&Apache::edit::tag_start($target,$token);
473: $result.=&Apache::edit::text_arg('Name:','name',$token);
474: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
475: ['unused','true','false'],$token);
476: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
477: $safeeval,'-3');
478: if ($randomize ne 'no') {
479: $result.=&Apache::edit::select_arg('Location:','location',
480: ['random','top','bottom'],$token);
481: }
482: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
483: } elsif ($target eq 'modified') {
484: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
485: 'value','name','location');
486: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
487: }
488: return $result;
489: }
490:
491: sub end_foil {
492: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
493: my $text='';
494: if ($target eq 'web' || $target eq 'tex') { $text=&Apache::lonxml::endredirection; }
495: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
496: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
497: if ($value ne 'unused') {
498: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
499: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
500: if (!$name) { $name=$Apache::lonxml::curdepth; }
501: if ( $Apache::radiobuttonresponse::conceptgroup
502: && !&Apache::response::showallfoils() ) {
503: push @{ $Apache::response::conceptgroup{'names'} }, $name;
504: $Apache::response::conceptgroup{"$name.value"} = $value;
505: $Apache::response::conceptgroup{"$name.text"} = $text;
506: $Apache::response::conceptgroup{"$name.location"} = $location;
507: } else {
508: push @{ $Apache::response::foilgroup{'names'} }, $name;
509: $Apache::response::foilgroup{"$name.value"} = $value;
510: $Apache::response::foilgroup{"$name.text"} = $text;
511: $Apache::response::foilgroup{"$name.location"} = $location;
512: }
513: }
514: }
515: return '';
516: }
517:
518: sub insert_foil {
519: return '
520: <foil name="" value="unused">
521: <startouttext />
522: <endouttext />
523: </foil>';
524: }
525: 1;
526: __END__
527:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>