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