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