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