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