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