1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.142 2009/03/18 13:46:26 bisitz 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# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28:
29:
30: package Apache::radiobuttonresponse;
31: use strict;
32: use HTML::Entities();
33: use Apache::lonlocal;
34: use Apache::lonnet;
35: use Apache::response;
36:
37: my $default_bubbles_per_line = 10;
38:
39:
40: BEGIN {
41: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
42: }
43:
44: sub bubble_line_count {
45: my ($numfoils, $bubbles_per_line) = @_;
46: my $bubble_lines;
47: $bubble_lines = int($numfoils / $bubbles_per_line);
48: if (($numfoils % $bubbles_per_line) != 0) {
49: $bubble_lines++;
50: }
51: return $bubble_lines;
52:
53: }
54:
55:
56: sub start_radiobuttonresponse {
57: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
58: my $result;
59:
60: #when in a radiobutton response use these
61: &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
62: push (@Apache::lonxml::namespace,'radiobuttonresponse');
63: my $id = &Apache::response::start_response($parstack,$safeeval);
64:
65: %Apache::hint::radiobutton=();
66: undef(%Apache::response::foilnames);
67: if ($target eq 'meta') {
68: $result=&Apache::response::meta_package_write('radiobuttonresponse');
69: } elsif ($target eq 'edit' ) {
70: $result.=&Apache::edit::start_table($token)
71: .'<tr><td>'.&Apache::lonxml::description($token)
72: .&Apache::loncommon::help_open_topic('Radio_Response_Problems')
73: .'</td>'
74: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
75: .&Apache::edit::deletelist($target,$token)
76: .'</span></td>'
77: .'<td> '.&Apache::edit::end_row()
78: .&Apache::edit::start_spanning_row();
79: $result.=
80: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
81: $token,'4').' 'x 3 .
82: &Apache::edit::select_arg('Randomize Foil Order:','randomize',
83: ['yes','no'],$token).' 'x 3 .
84: &Apache::edit::select_arg('Display Direction:','direction',
85: ['vertical','horizontal'],$token).
86: &Apache::edit::end_row().
87: &Apache::edit::start_spanning_row()."\n";
88: } elsif ($target eq 'modified') {
89: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
90: $safeeval,'max',
91: 'randomize','direction');
92: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
93: } elsif ($target eq 'tex') {
94: my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
95: undef,0);
96: if ($type eq '1') {
97: $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
98: } elsif ($type eq 'A') {
99: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
100: } elsif ($type eq 'a') {
101: $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
102: } elsif ($type eq 'i') {
103: $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
104: } else {
105: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
106: }
107: if($env{'form.pdfFormFields'} eq 'yes') {
108: $result .= &Apache::lonxml::print_pdf_hiddenfield('meta', $env{'user.name'}, $env{'user.domain'});
109: $result .= "\n\\\\\n\\\\\n";
110: } else {
111: $result .= '\begin{enumerate}';
112: }
113: } elsif ($target eq 'analyze') {
114: my $part_id="$Apache::inputtags::part.$id";
115: $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
116: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
117: }
118: return $result;
119: }
120:
121: sub end_radiobuttonresponse {
122: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
123: my $result;
124: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
125: if ($target eq 'tex' and $env{'form.pdfFormFields'} ne 'yes') {
126: $result .= '\end{enumerate}';
127: }
128: &Apache::response::end_response;
129: pop @Apache::lonxml::namespace;
130: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
131: undef(%Apache::response::foilnames);
132: return $result;
133: }
134:
135: %Apache::response::foilgroup=();
136: sub start_foilgroup {
137: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
138: my $result;
139: %Apache::response::foilgroup=();
140: $Apache::radiobuttonresponse::conceptgroup=0;
141: &Apache::response::pushrandomnumber();
142: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
143: $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
144: }
145: return $result;
146: }
147:
148: sub storesurvey {
149: if ( !&Apache::response::submitted() ) { return ''; }
150: my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
151: &Apache::lonxml::debug("Here I am!:$response:");
152: if ( $response !~ /[0-9]+/) { return ''; }
153: my $part = $Apache::inputtags::part;
154: my $id = $Apache::inputtags::response['-1'];
155: my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
156: my %responsehash;
157: $responsehash{$whichfoils[$response]}=$response;
158: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
159: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
160: $responsestr;
161: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
162: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
163: &Apache::response::handle_previous(\%previous,$ad);
164: &Apache::lonxml::debug("submitted a $response<br />\n");
165: return '';
166: }
167:
168:
169: sub grade_response {
170: my ($answer, $whichfoils, $bubbles_per_line)=@_;
171:
172: if ( !&Apache::response::submitted() ) { return; }
173: my $response;
174:
175: if ($env{'form.submitted'} eq 'scantron') {
176: $response = &Apache::response::getresponse(1,undef,
177: &bubble_line_count(scalar(@{ $whichfoils}),
178: $bubbles_per_line),
179: $bubbles_per_line);
180:
181: } else {
182: $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
183: }
184:
185:
186: if ( $response !~ /[0-9]+/) { return; }
187: my $part=$Apache::inputtags::part;
188: my $id = $Apache::inputtags::response['-1'];
189: my %responsehash;
190: $responsehash{$whichfoils->[$response]}=$response;
191: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
192: my %previous=&Apache::response::check_for_previous($responsestr,
193: $part,$id);
194: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
195: $responsestr;
196: &Apache::lonxml::debug("submitted a $response<br />\n");
197: my $ad;
198: if ($response == $answer) {
199: $ad='EXACT_ANS';
200: } else {
201: $ad='INCORRECT';
202: }
203: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
204: &Apache::response::handle_previous(\%previous,$ad);
205: }
206:
207: sub end_foilgroup {
208: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
209:
210: my $result;
211: my $bubble_lines;
212: my $bubbles_per_line;
213: my $answer_count;
214: my $id = $Apache::inputtags::response['-1'];
215: my $part = $Apache::inputtags::part;
216: $bubbles_per_line =
217: &Apache::response::get_response_param($Apache::inputtags::part."_$id",
218: 'numbubbles',
219: $default_bubbles_per_line);
220:
221:
222: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
223: $target eq 'tex' || $target eq 'analyze') {
224: my $style = $Apache::lonhomework::type;
225: my $direction = &Apache::lonxml::get_param('direction',$parstack,
226: $safeeval,'-2');
227: if ( $style eq 'survey' && $target ne 'analyze') {
228: if ($target eq 'web' || $target eq 'tex') {
229: $result=&displayallfoils($direction, $target);
230: } elsif ( $target eq 'answer' ) {
231: $result=&displayallanswers();
232: } elsif ( $target eq 'grade' ) {
233: $result=&storesurvey();
234: }
235: $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
236:
237: } else {
238:
239: my $name;
240: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
241: '-2');
242: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
243: $safeeval,'-2');
244: my ($answer, @shown) = &whichfoils($max, $randomize);
245: $answer_count = scalar(@shown);
246:
247: if ($target eq 'web' || $target eq 'tex') {
248: $result=&displayfoils($target,
249: $answer, \@shown,
250: $direction,
251: $bubbles_per_line);
252: } elsif ($target eq 'answer' ) {
253: $result=&displayanswers($answer, \@shown, $bubbles_per_line);
254: } elsif ( $target eq 'grade') {
255: &grade_response($answer, \@shown, $bubbles_per_line);
256: } elsif ( $target eq 'analyze') {
257: my $bubble_lines = &bubble_line_count($answer_count,
258: $bubbles_per_line);
259: &Apache::response::analyze_store_foilgroup(\@shown,
260: ['text','value','location']);
261: my $part_id="$part.$id";
262: push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
263: ('true','false'));
264:
265: }
266: }
267: $Apache::lonxml::post_evaluate=0;
268: }
269: if ($target eq 'web') {
270: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
271: [\%Apache::response::foilgroup]);
272: }
273: &Apache::response::poprandomnumber();
274: $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
275: &Apache::lonxml::increment_counter($bubble_lines,
276: "$part.$id");
277: if ($target eq 'analyze') {
278: &Apache::lonhomework::set_bubble_lines();
279: }
280: return $result;
281: }
282:
283: sub getfoilcounts {
284: my @names;
285: my $truecnt=0;
286: my $falsecnt=0;
287: my $name;
288: if ( $Apache::response::foilgroup{'names'} ) {
289: @names= @{ $Apache::response::foilgroup{'names'} };
290: }
291: foreach $name (@names) {
292: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
293: $truecnt++;
294: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
295: $falsecnt++;
296: }
297: }
298: return ($truecnt,$falsecnt);
299: }
300:
301: sub format_prior_answer {
302: my ($mode,$answer,$other_data) = @_;
303: my $foil_data = $other_data->[0];
304: my %response = &Apache::lonnet::str2hash($answer);
305: my ($name) = keys(%response);
306: return '<span class="LC_prior_radiobutton">'.
307: $foil_data->{$name.'.text'}.'</span>';
308:
309: }
310:
311: sub displayallfoils {
312: my ($direction, $target)=@_;
313: my $result;
314: &Apache::lonxml::debug("survey style display");
315: my @names;
316: if ( $Apache::response::foilgroup{'names'} ) {
317: @names= @{ $Apache::response::foilgroup{'names'} };
318: }
319:
320: my $temp=0;
321: my $i =0;
322: my $id=$Apache::inputtags::response['-1'];
323: my $part=$Apache::inputtags::part;
324: my $lastresponse=
325: $Apache::lonhomework::history{"resource.$part.$id.submission"};
326: if ($direction eq 'horizontal') { $result.='<table><tr>'; }
327: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
328: if (&Apache::response::show_answer() ) {
329: foreach my $name (@names) {
330: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
331: if (($direction eq 'horizontal') && ($target ne 'tex')) {
332: $result.="<td>";
333: } else {
334: if ($target eq 'tex') {
335: $result .= '\item \vskip -2mm ';
336: } else {
337: $result.="<br />";
338: }
339: }
340: if (defined($lastresponse{$name})) {
341: if ($target eq 'tex') {
342: $result .= '}';
343: } else {
344: $result.='<b>';
345: }
346: }
347: $result .= $Apache::response::foilgroup{$name.'.text'};
348: if (defined($lastresponse{$name}) && ($target ne 'tex')) {
349: $result.='</b>';
350: }
351: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
352: }
353: }
354: } else {
355: foreach my $name (@names) {
356: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
357: if ($direction eq 'horizontal') {
358: $result.="<td>";
359: } else {
360: if ($target eq 'tex') {
361: if($env{'form.pdfFormFields'} eq 'yes') {
362: my $fieldname = $env{'request.symb'}.
363: '&part_'. $Apache::inputtags::part.
364: '&radiobuttonresponse'.
365: '&HWVAL_' . $Apache::inputtags::response['-1'];
366: my $value = $temp;
367: my $text = $Apache::response::foilgroup{$name.'.text'};
368: $result .= &Apache::lonxml::print_pdf_radiobutton($fieldname,
369: $value,
370: $text)."\n";
371: } else {
372: $result .= '\item \vskip -2mm ';
373: }
374: } else {
375: $result.="<br />";
376: }
377: }
378: if ($target eq 'tex') {
379: if($env{'form.pdfFormFields'} ne 'yes') {
380: $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
381: }
382: $i++;
383: } else {
384: $result .= '<label>';
385: $result.="<input
386: onchange=\"javascript:setSubmittedPart('$part');\"
387: type=\"radio\"
388: name=\"HWVAL_$Apache::inputtags::response['-1']\"
389: value=\"$temp\"";
390: if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
391: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
392: '</label>';
393: }
394: $temp++;
395: if ($target ne 'tex') {
396: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
397: } else {
398: $result.='\vskip 0 mm ';
399: }
400: }
401: }
402: }
403:
404: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
405: return $result;
406: }
407:
408:
409: sub whichfoils {
410: my ($max,$randomize)=@_;
411:
412: my @truelist;
413: my @falselist;
414: my @whichfalse =();
415: my ($truecnt,$falsecnt) = &getfoilcounts();
416: my $count=0;
417: # we will add in 1 of the true statements
418: if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
419: my $answer=int(&Math::Random::random_uniform() * ($count));
420: &Apache::lonxml::debug("Count is $count, $answer is $answer");
421: my @names;
422: if ( $Apache::response::foilgroup{'names'} ) {
423: @names= @{ $Apache::response::foilgroup{'names'} };
424: }
425: if (&Apache::response::showallfoils()) {
426: @whichfalse=@names;
427: } elsif ($randomize eq 'no') {
428: &Apache::lonxml::debug("No randomization");
429: my $havetrue=0;
430: foreach my $name (@names) {
431: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
432: if (!$havetrue ) {
433: push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
434: }
435: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
436: push (@whichfalse,$name);
437: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
438: } else {
439: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
440: }
441: }
442: if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
443: &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
444: }
445: } else {
446: my $current=0;
447: &Apache::lonhomework::showhash(%Apache::response::foilgroup);
448: my (%top,%bottom);
449: #first find out where everyone wants to be
450: foreach my $name (@names) {
451: $current++;
452: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
453: push (@truelist,$name);
454: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
455: $top{$name}=$current;
456: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
457: $bottom{$name}=$current;
458: }
459: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
460: push (@falselist,$name);
461: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
462: $top{$name}=$current;
463: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
464: $bottom{$name}=$current;
465: }
466: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
467: } else {
468: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
469: }
470: }
471: #pick a true statement
472: my $notrue=0;
473: if (scalar(@truelist) == 0) { $notrue=1; }
474: my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
475: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
476: my (@toplist, @bottomlist);
477: my $topcount=0;
478: my $bottomcount=0;
479: # assign everyone to either toplist/bottomlist or whichfalse
480: # which false is randomized, toplist bottomlist are in order
481: while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
482: &Apache::lonxml::debug("Have $#whichfalse max is $max");
483: my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
484: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
485: $afalse=splice(@falselist,$afalse,1);
486: &Apache::lonxml::debug("Picked $afalse");
487: &Apache::lonhomework::showhash(('names'=>\@names));
488: &Apache::lonhomework::showhash(%top);
489: if ($top{$afalse}) {
490: $toplist[$top{$afalse}]=$afalse;
491: $topcount++;
492: } elsif ($bottom{$afalse}) {
493: $bottomlist[$bottom{$afalse}]=$afalse;
494: $bottomcount++;
495: } else {
496: push (@whichfalse,$afalse);
497: }
498: }
499: &Apache::lonxml::debug("Answer wants $answer");
500: my $truename=$truelist[$whichtrue];
501: my $dosplice=1;
502: if ($notrue && $Apache::lonhomework::type ne 'survey') {
503: $dosplice=0;
504: &Apache::lonxml::error(&mt('There are no true statements available.').'<br />');
505: }
506: #insert the true statement, keeping track of where it wants to be
507: if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
508: $toplist[$top{$truename}]=$truename;
509: $answer=-1;
510: foreach my $top (reverse(@toplist)) {
511: if ($top) { $answer++;}
512: if ($top eq $truename) { last; }
513: }
514: $dosplice=0;
515: } elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
516: $bottomlist[$bottom{$truename}]=$truename;
517: $answer=-1;
518: foreach my $bot (@bottomlist) {
519: if ($bot) { $answer++;}
520: if ($bot eq $truename) { last; }
521: }
522: $answer+=$topcount+$#whichfalse+1;
523: $dosplice=0;
524: } else {
525: if ($topcount>0 || $bottomcount>0) {
526: $answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
527: + $topcount;
528: }
529: }
530: &Apache::lonxml::debug("Answer now wants $answer");
531: #add the top items to the top, bottom items to the bottom
532: for (my $i=0;$i<=$#toplist;$i++) {
533: if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
534: }
535: for (my $i=0;$i<=$#bottomlist;$i++) {
536: if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
537: }
538: #if the true statement is randomized insert it into the list
539: if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
540: }
541: &Apache::lonxml::debug("Answer is $answer");
542: return ($answer,@whichfalse);
543: }
544:
545: sub displayfoils {
546: my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
547: my $result;
548:
549: my $part=$Apache::inputtags::part;
550: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
551: if ( ($target ne 'tex') &&
552: &Apache::response::show_answer() ) {
553: if ($direction eq 'horizontal') {
554: if ($target ne 'tex') {
555: $result.='<table><tr>';
556: }
557: }
558: foreach my $name (@{ $whichfoils }) {
559: if ($direction eq 'horizontal') {
560: if ($target ne 'tex') { $result.='<td>'; }
561: }
562: if ($target ne 'tex') {
563: $result.="<br />";
564: } else {
565: $result.='\item \vskip -2 mm ';
566: }
567: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
568: if ($target ne 'tex') {
569: $result.=&mt('Correct:').'<b>';
570: } else {
571: $result.=&mt('Correct:').' \textbf{';
572: }
573: } else {
574: $result.=&mt('Incorrect:');
575: }
576: if ($target eq 'web') { $result.="<label>"; }
577: $result.=$Apache::response::foilgroup{$name.'.text'};
578: if ($target eq 'web') { $result.="</label>"; }
579: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
580: if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
581: }
582: if ($direction eq 'horizontal') {
583: if ($target ne 'tex') { $result.='</td>'; }
584: }
585: }
586: if ($direction eq 'horizontal') {
587: if ($target ne 'tex') {
588: $result.='</tr></table>';
589: }
590: }
591: } else {
592: my @alphabet = ('A'..'Z');
593: my $i = 0;
594: my $bubble_number = 0;
595: my $line = 0;
596: my $temp=0;
597: my $id=$Apache::inputtags::response['-1'];
598: my $part=$Apache::inputtags::part;
599: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
600: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
601: if ($target ne 'tex' && $direction eq 'horizontal') {
602: $result.="<table><tr>";
603: }
604: foreach my $name (@{ $whichfoils }) {
605: if ($target ne 'tex') {
606: if ($direction eq 'horizontal') {
607: $result.="<td>";
608: } else {
609: $result.="<br />";
610: }
611: }
612: if ($target ne 'tex') {
613: $result.= '<label>';
614: $result.=
615: "<input type=\"radio\"
616: onchange=\"javascript:setSubmittedPart('$part');\"
617: name=\"HWVAL_$Apache::inputtags::response['-1']\"
618: value=\"$temp\"";
619: if (defined($lastresponse{$name})) { $result .= ' checked="checked"'; }
620: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
621: } else {
622: if ($Apache::lonhomework::type eq 'exam') {
623: if($bubble_number >= $bubbles_per_line) {
624: $line++;
625: $i = 0;
626: $bubble_number = 0;
627: $result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
628: }
629: $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
630: $i++;
631: $bubble_number++;
632: } else {
633: if($env{'form.pdfFormFields'} eq 'yes') {
634:
635: my $fieldname = $env{'request.symb'}.
636: '&part_'. $Apache::inputtags::part.
637: '&radiobuttonresponse'.
638: '&HWVAL_' . $Apache::inputtags::response['-1'];
639: my $value = $temp;
640: my $text = $Apache::response::foilgroup{$name.'.text'};
641: $result .= &Apache::lonxml::print_pdf_radiobutton($fieldname, $value, $text).'\newline'."\n";
642: } else {
643: $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
644: }
645: }
646: }
647: if ($target ne 'tex' && $direction eq 'horizontal') {
648: $result.="</td>";
649: }
650: $temp++;
651: }
652: if ($target ne 'tex' && $direction eq 'horizontal') {
653: $result.="</tr></table>";
654: }
655: }
656: if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
657: return $result;
658: }
659:
660: sub displayallanswers {
661: my @names;
662: if ( $Apache::response::foilgroup{'names'} ) {
663: @names= @{ $Apache::response::foilgroup{'names'} };
664: }
665: my $result=&Apache::response::answer_header('radiobuttonresponse');
666: foreach my $name (@names) {
667: $result.=&Apache::response::answer_part('radiobuttonresponse',
668: $Apache::response::foilgroup{$name.'.value'});
669: }
670: $result.=&Apache::response::answer_footer('radiobuttonresponse');
671: return $result;
672: }
673:
674: sub displayanswers {
675: my ($answer, $whichopt, $bubbles_per_line)=@_;
676: my $result;
677:
678: if ($Apache::lonhomework::type eq 'exam') {
679: my $line = int($answer/$bubbles_per_line);
680: my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
681: $result .= &Apache::response::answer_header('radiobuttonresponse',
682: $line);
683: $result .= &Apache::response::answer_part('radiobuttonresponse',
684: $correct);
685: } else {
686: $result .= &Apache::response::answer_header('radiobuttonresponse');
687: }
688: foreach my $name (@{ $whichopt }) {
689: $result.=&Apache::response::answer_part('radiobuttonresponse',
690: $Apache::response::foilgroup{$name.'.value'});
691: }
692: $result.=&Apache::response::answer_footer('radiobuttonresponse');
693: return $result;
694: }
695:
696: sub start_conceptgroup {
697: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
698: $Apache::radiobuttonresponse::conceptgroup=1;
699: %Apache::response::conceptgroup=();
700: my $result;
701: if ($target eq 'edit') {
702: $result.=&Apache::edit::tag_start($target,$token);
703: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
704: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
705: } elsif ($target eq 'modified') {
706: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
707: $safeeval,'concept');
708: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
709: }
710: return $result;
711: }
712:
713: sub end_conceptgroup {
714: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
715: $Apache::radiobuttonresponse::conceptgroup=0;
716: my $result;
717: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
718: $target eq 'tex' || $target eq 'analyze') {
719: &Apache::response::pick_foil_for_concept($target,
720: ['value','text','location'],
721: \%Apache::hint::radiobutton,
722: $parstack,$safeeval);
723: } elsif ($target eq 'edit') {
724: $result=&Apache::edit::end_table();
725: }
726: return $result;
727: }
728:
729: sub insert_conceptgroup {
730: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
731: return $result;
732: }
733:
734: sub start_foil {
735: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
736: my $result='';
737: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
738: &Apache::lonxml::startredirection;
739: if ($target eq 'analyze') {
740: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
741: }
742: } elsif ($target eq 'edit') {
743: $result=&Apache::edit::tag_start($target,$token);
744: $result.=&Apache::edit::text_arg('Name:','name',$token);
745: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
746: ['unused','true','false'],
747: $token);
748: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
749: $safeeval,'-3');
750: if ($randomize ne 'no') {
751: $result.=&Apache::edit::select_arg('Location:','location',
752: ['random','top','bottom'],$token);
753: }
754: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
755: } elsif ($target eq 'modified') {
756: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
757: $safeeval,'value','name',
758: 'location');
759: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
760: }
761: return $result;
762: }
763:
764: sub end_foil {
765: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
766: my $text='';
767: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
768: $text=&Apache::lonxml::endredirection;
769: }
770: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
771: || $target eq 'tex' || $target eq 'analyze') {
772: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
773: if ($value ne 'unused') {
774: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
775: if ($name eq "") {
776: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
777: $name=$Apache::lonxml::curdepth;
778: }
779: if (defined($Apache::response::foilnames{$name})) {
780: &Apache::lonxml::error(&mt('Foil name [_1] appears more than once. Foil names need to be unique.','<b><tt>'.$name.'</tt></b>'));
781: }
782: $Apache::response::foilnames{$name}++;
783: my $location =&Apache::lonxml::get_param('location',$parstack,
784: $safeeval);
785: if ( $Apache::radiobuttonresponse::conceptgroup
786: && !&Apache::response::showallfoils() ) {
787: push @{ $Apache::response::conceptgroup{'names'} }, $name;
788: $Apache::response::conceptgroup{"$name.value"} = $value;
789: $Apache::response::conceptgroup{"$name.text"} = $text;
790: $Apache::response::conceptgroup{"$name.location"} = $location;
791: } else {
792: push @{ $Apache::response::foilgroup{'names'} }, $name;
793: $Apache::response::foilgroup{"$name.value"} = $value;
794: $Apache::response::foilgroup{"$name.text"} = $text;
795: $Apache::response::foilgroup{"$name.location"} = $location;
796: }
797: }
798: }
799: return '';
800: }
801:
802: sub insert_foil {
803: return '
804: <foil name="" value="unused">
805: <startouttext />
806: <endouttext />
807: </foil>';
808: }
809: 1;
810: __END__
811:
812:
813:
814: =head1 NAME
815:
816: Apache::radiobuttonresponse
817:
818: =head1 SYNOPSIS
819:
820: Handles multiple-choice style responses.
821:
822: This is part of the LearningOnline Network with CAPA project
823: described at http://www.lon-capa.org.
824:
825: =head1 SUBROUTINES
826:
827: =over
828:
829: =item start_radiobuttonresponse()
830:
831: =item bubble_line_count()
832:
833: =item end_radiobuttonresponse()
834:
835: =item start_foilgroup()
836:
837: =item storesurvey()
838:
839: =item grade_response()
840:
841: =item end_foilgroup()
842:
843: =item getfoilcounts()
844:
845: =item format_prior_answer()
846:
847: =item displayallfoils()
848:
849: =item &whichfoils($max,$randomize)
850:
851: Randomizes the list of foils.
852: Respects
853: - each foils desire to be randomized
854: - the existance of Concept groups of foils (select 1 foil from each)
855: - and selects a single correct statement from all possilble true statments
856: - and limits it to a toal of $max foils
857:
858: WARNING: this routine uses the random number generator, it should only
859: be called once per target, otherwise it can cause randomness changes in
860: homework problems.
861:
862: Arguments
863: $max - maximum number of foils to select (including the true one)
864: (so a max of 5 is: 1 true, 4 false)
865:
866: $randomize - whether to randomize the listing of foils, by default
867: will randomize, only if randomize is 'no' will it not
868:
869: Returns
870: $answer - location in the array of the correct answer
871: @foils - array of foil names in to display order
872:
873: =item displayfoils()
874:
875: =item displayallanswers()
876:
877: =item displayanswers()
878:
879: =item start_conceptgroup()
880:
881: =item end_conceptgroup()
882:
883: =item insert_conceptgroup()
884:
885: =item start_foil()
886:
887: =item end_foil()
888:
889: =item insert_foil()
890:
891: =back
892:
893: =cut
894:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>