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