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