1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.153.6.2 2012/01/21 20:54:57 foxr Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22: #
23: # /home/httpd/html/adm/gpl.txt
24: #
25: # http://www.lon-capa.org/
26: #
27:
28: package Apache::radiobuttonresponse;
29: use strict;
30: use HTML::Entities();
31: use Apache::lonlocal;
32: use Apache::lonnet;
33: use Apache::response;
34:
35: my $default_bubbles_per_line = 10;
36:
37: BEGIN {
38: &Apache::lonxml::register( 'Apache::radiobuttonresponse',
39: ('radiobuttonresponse') );
40: }
41:
42: sub bubble_line_count {
43: my ( $numfoils, $bubbles_per_line ) = @_;
44: my $bubble_lines;
45: $bubble_lines = int( $numfoils / $bubbles_per_line );
46: if ( ( $numfoils % $bubbles_per_line ) != 0 ) {
47: $bubble_lines++;
48: }
49: return $bubble_lines;
50:
51: }
52:
53: sub start_radiobuttonresponse {
54: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
55: @_;
56: my $result;
57:
58: #when in a radiobutton response use these
59: &Apache::lonxml::register( 'Apache::radiobuttonresponse',
60: ( 'foilgroup', 'foil', 'conceptgroup' ) );
61: push( @Apache::lonxml::namespace, 'radiobuttonresponse' );
62: my $id = &Apache::response::start_response( $parstack, $safeeval );
63:
64: %Apache::hint::radiobutton = ();
65: undef(%Apache::response::foilnames);
66: if ( $target eq 'meta' ) {
67: $result = &Apache::response::meta_package_write('radiobuttonresponse');
68: }
69: elsif ( $target eq 'edit' ) {
70: $result .=
71: &Apache::edit::start_table($token)
72: . '<tr><td>'
73: . &Apache::lonxml::description($token)
74: . &Apache::loncommon::help_open_topic('Radio_Response_Problems')
75: . '</td>'
76: . '<td><span class="LC_nobreak">'
77: . &mt('Delete?') . ' '
78: . &Apache::edit::deletelist( $target, $token )
79: . '</span></td>'
80: . '<td> '
81: . &Apache::edit::end_row()
82: . &Apache::edit::start_spanning_row();
83: $result .= &Apache::edit::text_arg( 'Max Number Of Shown Foils:',
84: 'max', $token, '4' )
85: . ' ' x 3
86: . &Apache::edit::select_arg( 'Randomize Foil Order:',
87: 'randomize', [ 'yes', 'no' ], $token )
88: . ' ' x 3
89: . &Apache::edit::select_arg(
90: 'Display Direction:', 'direction',
91: [ 'vertical', 'horizontal' ], $token
92: )
93: . &Apache::edit::end_row()
94: . &Apache::edit::start_spanning_row() . "\n";
95: }
96: elsif ( $target eq 'modified' ) {
97: my $constructtag =
98: &Apache::edit::get_new_args( $token, $parstack, $safeeval, 'max',
99: 'randomize', 'direction' );
100: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
101: }
102: elsif ( $target eq 'tex' ) {
103: my $type =
104: &Apache::lonxml::get_param( 'TeXtype', $parstack, $safeeval, undef,
105: 0 );
106: if ( $type eq '1' ) {
107: $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
108: }
109: elsif ( $type eq 'A' ) {
110: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
111: }
112: elsif ( $type eq 'a' ) {
113: $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
114: }
115: elsif ( $type eq 'i' ) {
116: $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
117: }
118: else {
119: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
120: }
121: if ( $env{'form.pdfFormFields'} eq 'yes'
122: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
123: {
124: $result .= '\begin{itemize}';
125: }
126: else {
127: $result .= '\begin{enumerate}';
128: }
129: }
130: elsif ( $target eq 'analyze' ) {
131: my $part_id = "$Apache::inputtags::part.$id";
132: $Apache::lonhomework::analyze{"$part_id.type"} = 'radiobuttonresponse';
133: push( @{ $Apache::lonhomework::analyze{"parts"} }, $part_id );
134: }
135: return $result;
136: }
137:
138: sub end_radiobuttonresponse {
139: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
140: @_;
141: my $result;
142: if ( $target eq 'edit' ) { $result = &Apache::edit::end_table(); }
143: if ( $target eq 'tex' ) {
144: if ( $env{'form.pdfFormFields'} eq 'yes'
145: and $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
146: {
147: $result .= '\end{itemize}';
148: }
149: else {
150: $result .= '\end{enumerate}';
151: }
152: }
153: &Apache::response::end_response;
154: pop @Apache::lonxml::namespace;
155: &Apache::lonxml::deregister( 'Apache::radiobuttonresponse',
156: ( 'foilgroup', 'foil', 'conceptgroup' ) );
157: undef(%Apache::response::foilnames);
158: return $result;
159: }
160:
161: %Apache::response::foilgroup = ();
162:
163: sub start_foilgroup {
164: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
165: @_;
166: %Apache::response::foilgroup = ();
167: $Apache::radiobuttonresponse::conceptgroup = 0;
168: &Apache::response::pushrandomnumber( undef, $target );
169: return;
170: }
171:
172: sub storesurvey {
173: my ($style) = @_;
174: if ( !&Apache::response::submitted() ) { return ''; }
175: my $response = $env{ 'form.HWVAL_' . $Apache::inputtags::response['-1'] };
176: &Apache::lonxml::debug("Here I am!:$response:");
177: if ( $response !~ /[0-9]+/ ) { return ''; }
178: my $part = $Apache::inputtags::part;
179: my $id = $Apache::inputtags::response['-1'];
180: my @whichfoils = @{ $Apache::response::foilgroup{'names'} };
181: my %responsehash;
182: $responsehash{ $whichfoils[$response] } = $response;
183: my $responsestr = &Apache::lonnet::hash2str(%responsehash);
184: $Apache::lonhomework::results{"resource.$part.$id.submission"} =
185: $responsestr;
186: my %previous =
187: &Apache::response::check_for_previous( $responsestr, $part, $id );
188: my $ad;
189:
190: if ( $style eq 'anonsurvey' ) {
191: $ad = $Apache::lonhomework::results{"resource.$part.$id.awarddetail"} =
192: 'ANONYMOUS';
193: }
194: elsif ( $style eq 'anonsurveycred' ) {
195: $ad = $Apache::lonhomework::results{"resource.$part.$id.awarddetail"} =
196: 'ANONYMOUS_CREDIT';
197: }
198: elsif ( $style eq 'surveycred' ) {
199: $ad = $Apache::lonhomework::results{"resource.$part.$id.awarddetail"} =
200: 'SUBMITTED_CREDIT';
201: }
202: else {
203: $ad = $Apache::lonhomework::results{"resource.$part.$id.awarddetail"} =
204: 'SUBMITTED';
205: }
206: &Apache::response::handle_previous( \%previous, $ad );
207: &Apache::lonxml::debug("submitted a $response<br />\n");
208: return '';
209: }
210:
211: sub grade_response {
212: my ( $answer, $whichfoils, $bubbles_per_line ) = @_;
213:
214: if ( !&Apache::response::submitted() ) { return; }
215: my $response;
216:
217: if ( $env{'form.submitted'} eq 'scantron' ) {
218: $response =
219: &Apache::response::getresponse( 1, undef,
220: &bubble_line_count( scalar( @{$whichfoils} ), $bubbles_per_line ),
221: $bubbles_per_line );
222:
223: }
224: else {
225: $response = $env{ 'form.HWVAL_' . $Apache::inputtags::response['-1'] };
226: }
227:
228: if ( $response !~ /[0-9]+/ ) { return; }
229: my $part = $Apache::inputtags::part;
230: my $id = $Apache::inputtags::response['-1'];
231: my %responsehash;
232: $responsehash{ $whichfoils->[$response] } = $response;
233: my $responsestr = &Apache::lonnet::hash2str(%responsehash);
234: my %previous =
235: &Apache::response::check_for_previous( $responsestr, $part, $id );
236: $Apache::lonhomework::results{"resource.$part.$id.submission"} =
237: $responsestr;
238: &Apache::lonxml::debug("submitted a $response<br />\n");
239: my $ad;
240:
241: if ( $response == $answer ) {
242: $ad = 'EXACT_ANS';
243: }
244: else {
245: $ad = 'INCORRECT';
246: }
247: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"} = $ad;
248: &Apache::response::handle_previous( \%previous, $ad );
249: }
250:
251: sub end_foilgroup {
252: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
253: @_;
254:
255: my $result;
256: my $bubble_lines;
257: my $answer_count;
258: my $id = $Apache::inputtags::response['-1'];
259: my $part = $Apache::inputtags::part;
260: my $bubbles_per_line = &getbubblesnum( $part, $id );
261:
262: if ( $target eq 'grade'
263: || $target eq 'web'
264: || $target eq 'answer'
265: || $target eq 'tex'
266: || $target eq 'analyze' )
267: {
268: my $style = $Apache::lonhomework::type;
269: my $direction =
270: &Apache::lonxml::get_param( 'direction', $parstack, $safeeval, '-2' );
271: if (
272: (
273: ( $style eq 'survey' )
274: || ( $style eq 'surveycred' )
275: || ( $style eq 'anonsurvey' )
276: || ( $style eq 'anonsurveycred' )
277: )
278: && ( $target ne 'analyze' )
279: )
280: {
281: if ( $target eq 'web' || $target eq 'tex' ) {
282: $result = &displayallfoils( $direction, $target );
283: }
284: elsif ( $target eq 'answer' ) {
285: $result = &displayallanswers();
286: }
287: elsif ( $target eq 'grade' ) {
288: $result = &storesurvey($style);
289: }
290: $answer_count =
291: scalar( @{ $Apache::response::foilgroup{'names'} } );
292:
293: }
294: else {
295:
296: my $name;
297: my $max =
298: &Apache::lonxml::get_param( 'max', $parstack, $safeeval, '-2' );
299: my $randomize =
300: &Apache::lonxml::get_param( 'randomize', $parstack, $safeeval,
301: '-2' );
302: my ( $answer, @shown ) = &whichfoils( $max, $randomize );
303: $answer_count = scalar(@shown);
304:
305: if ( $target eq 'web' || $target eq 'tex' ) {
306: $result =
307: &displayfoils( $target, $answer, \@shown, $direction,
308: $bubbles_per_line );
309: }
310: elsif ( $target eq 'answer' ) {
311: $result =
312: &displayanswers( $answer, \@shown, $bubbles_per_line );
313: }
314: elsif ( $target eq 'grade' ) {
315: &grade_response( $answer, \@shown, $bubbles_per_line );
316: }
317: elsif ( $target eq 'analyze' ) {
318: my $bubble_lines =
319: &bubble_line_count( $answer_count, $bubbles_per_line );
320: &Apache::response::analyze_store_foilgroup( \@shown,
321: [ 'text', 'value', 'location' ] );
322: my $part_id = "$part.$id";
323: push(
324: @{ $Apache::lonhomework::analyze{"$part_id.options"} },
325: ( 'true', 'false' )
326: );
327:
328: }
329: }
330: $Apache::lonxml::post_evaluate = 0;
331: }
332: if ( $target eq 'web' ) {
333: &Apache::response::setup_prior_tries_hash( \&format_prior_answer,
334: [ \%Apache::response::foilgroup ] );
335: }
336: &Apache::response::poprandomnumber();
337: $bubble_lines = &bubble_line_count( $answer_count, $bubbles_per_line );
338: &Apache::lonxml::increment_counter( $bubble_lines, "$part.$id" );
339: if ( $target eq 'analyze' ) {
340: &Apache::lonhomework::set_bubble_lines();
341: }
342: return $result;
343: }
344:
345: sub getbubblesnum {
346: my ( $part, $id ) = @_;
347: my $bubbles_per_line;
348: my $default_numbubbles = $default_bubbles_per_line;
349: if ( ( $env{'form.bubbles_per_row'} =~ /^\d+$/ )
350: && ( $env{'form.bubbles_per_row'} > 0 ) )
351: {
352: $default_numbubbles = $env{'form.bubbles_per_row'};
353: }
354: $bubbles_per_line = &Apache::response::get_response_param( $part . "_$id",
355: 'numbubbles', $default_numbubbles );
356: return $bubbles_per_line;
357: }
358:
359: sub getfoilcounts {
360: my @names;
361: my $truecnt = 0;
362: my $falsecnt = 0;
363: my $name;
364: if ( $Apache::response::foilgroup{'names'} ) {
365: @names = @{ $Apache::response::foilgroup{'names'} };
366: }
367: foreach $name (@names) {
368: if ( $Apache::response::foilgroup{ $name . '.value' } eq 'true' ) {
369: $truecnt++;
370: }
371: elsif ( $Apache::response::foilgroup{ $name . '.value' } eq 'false' ) {
372: $falsecnt++;
373: }
374: }
375: return ( $truecnt, $falsecnt );
376: }
377:
378: sub format_prior_answer {
379: my ( $mode, $answer, $other_data ) = @_;
380: my $foil_data = $other_data->[0];
381: my %response = &Apache::lonnet::str2hash($answer);
382: my ($name) = keys(%response);
383: return
384: '<span class="LC_prior_radiobutton">'
385: . $foil_data->{ $name . '.text' }
386: . '</span>';
387:
388: }
389:
390: ##
391: sub displayallfoils {
392: my ( $direction, $target ) = @_;
393: my $result;
394: &Apache::lonxml::debug("survey style display");
395: my @names;
396: if ( $Apache::response::foilgroup{'names'} ) {
397: @names = @{ $Apache::response::foilgroup{'names'} };
398: }
399:
400: my $temp = 0;
401: my $i = 0;
402: my $id = $Apache::inputtags::response['-1'];
403: my $part = $Apache::inputtags::part;
404: my ( $lastresponse, $newvariation, $showanswer );
405: if (
406: (
407: (
408: $Apache::lonhomework::history{"resource.$part.type"} eq
409: 'randomizetry'
410: )
411: || ( $Apache::lonhomework::type eq 'randomizetry' )
412: )
413: && ( $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
414: )
415: {
416: if ( $env{ 'form.' . $part . '.rndseed' } ne
417: $Apache::lonhomework::history{"resource.$part.rndseed"} )
418: {
419: $newvariation = 1;
420: }
421: }
422: $showanswer = &Apache::response::show_answer();
423: unless (
424: (
425: (
426: $Apache::lonhomework::history{"resource.$part.type"} eq
427: 'anonsurvey'
428: )
429: || ( $Apache::lonhomework::history{"resource.$part.type"} eq
430: 'anonsurveycred' )
431: )
432: && ( defined( $env{'form.grade_symb'} ) )
433: || ( $newvariation && !$showanswer )
434: )
435: {
436: $lastresponse =
437: $Apache::lonhomework::history{"resource.$part.$id.submission"};
438: }
439: if ( $direction eq 'horizontal' ) { $result .= '<table><tr>'; }
440: my %lastresponse = &Apache::lonnet::str2hash($lastresponse);
441: if ($showanswer) {
442: foreach my $name (@names) {
443: if ( $Apache::response::foilgroup{ $name . '.value' } ne 'unused' )
444: {
445: if ( ( $direction eq 'horizontal' ) && ( $target ne 'tex' ) ) {
446: $result .= "<td>";
447: }
448: else {
449: if ( $target eq 'tex' ) {
450: $result .= '\item \vskip -2mm ';
451: }
452: else {
453: $result .= "<br />";
454: }
455: }
456: if ( defined( $lastresponse{$name} ) ) {
457: if ( $target eq 'tex' ) {
458: $result .= '}';
459: }
460: else {
461: $result .= '<b>';
462: }
463: }
464: $result .= $Apache::response::foilgroup{ $name . '.text' };
465: if ( defined( $lastresponse{$name} ) && ( $target ne 'tex' ) ) {
466: $result .= '</b>';
467: }
468: if ( ( $direction eq 'horizontal' ) && ( $target ne 'tex' ) ) {
469: $result .= "</td>";
470: }
471: }
472: }
473: }
474: else {
475: foreach my $name (@names) {
476: if ( $Apache::response::foilgroup{ $name . '.value' } ne 'unused' )
477: {
478: if ( $direction eq 'horizontal' ) {
479: $result .= "<td>";
480: }
481: else {
482: if ( $target eq 'tex' ) {
483: if ( $env{'form.pdfFormFields'} eq 'yes'
484: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
485: {
486: my $fieldname =
487: $env{'request.symb'}
488: . '&part_'
489: . $Apache::inputtags::part
490: . '&radiobuttonresponse'
491: . '&HWVAL_'
492: . $Apache::inputtags::response['-1'];
493: $result .= '\item[{'
494: . &Apache::lonxml::print_pdf_radiobutton(
495: $fieldname, $temp )
496: . '}]'
497: . $Apache::response::foilgroup{ $name . '.text' }
498: . "\n";
499: }
500: else {
501: $result .= '\item \vskip -2mm ';
502: }
503: }
504: else {
505: $result .= "<br />";
506: }
507: }
508: if ( $target eq 'tex' ) {
509: if ( $env{'form.pdfFormFields'} ne 'yes'
510: or $Apache::inputtags::status[-1] ne 'CAN_ANSWER' )
511: {
512: $result .=
513: '$\bigcirc$'
514: . $Apache::response::foilgroup{ $name . '.text' }
515: . '\\\\'; #' stupid emacs
516: }
517: $i++;
518: }
519: else {
520: $result .= '<label>';
521: $result .= "<input
522: onchange=\"javascript:setSubmittedPart('$part');\"
523: type=\"radio\"
524: name=\"HWVAL_$Apache::inputtags::response['-1']\"
525: value=\"$temp\"";
526:
527: if ( defined( $lastresponse{$name} ) ) {
528: $result .= ' checked="checked"';
529: }
530: $result .= ' />'
531: . $Apache::response::foilgroup{ $name . '.text' }
532: . '</label>';
533: }
534: $temp++;
535: if ( $target ne 'tex' ) {
536: if ( ( $direction eq 'horizontal' )
537: && ( $target ne 'tex' ) )
538: {
539: $result .= "</td>";
540: }
541: }
542: else {
543: $result .= '\vskip 0 mm ';
544: }
545: }
546: }
547: }
548:
549: if ( ( $direction eq 'horizontal' ) && ( $target ne 'tex' ) ) {
550: $result .= '</tr></table>';
551: }
552: return $result;
553: }
554:
555: sub whichfoils {
556: my ( $max, $randomize ) = @_;
557:
558: my @truelist;
559: my @falselist;
560: my @whichfalse = ();
561: my ( $truecnt, $falsecnt ) = &getfoilcounts();
562: my $count = 0;
563:
564: # we will add in 1 of the true statements
565: if ( $max > 0 && ( $falsecnt + 1 ) > $max ) { $count = $max }
566: else { $count = $falsecnt + 1; $max = $count; }
567: my $answer = int( &Math::Random::random_uniform() * ($count) );
568: &Apache::lonxml::debug("Count is $count, $answer is $answer");
569: my @names;
570: if ( $Apache::response::foilgroup{'names'} ) {
571: @names = @{ $Apache::response::foilgroup{'names'} };
572: }
573: if ( &Apache::response::showallfoils() ) {
574: @whichfalse = @names;
575: }
576: elsif ( $randomize eq 'no' ) {
577: &Apache::lonxml::debug("No randomization");
578: my $havetrue = 0;
579: foreach my $name (@names) {
580: if ( $Apache::response::foilgroup{ $name . '.value' } eq 'true' ) {
581: if ( !$havetrue ) {
582: push( @whichfalse, $name );
583: $havetrue++;
584: $answer = $#whichfalse;
585: }
586: }
587: elsif (
588: $Apache::response::foilgroup{ $name . '.value' } eq 'false' )
589: {
590: push( @whichfalse, $name );
591: }
592: elsif (
593: $Apache::response::foilgroup{ $name . '.value' } eq 'unused' )
594: {
595: }
596: else {
597: &Apache::lonxml::error(
598: &HTML::Entities::encode(
599: "No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",
600: '<>&"'
601: )
602: );
603: }
604: }
605: if ( ( !$havetrue )
606: && ( $Apache::lonhomework::type ne 'survey' )
607: && ( $Apache::lonhomework::type ne 'surveycred' )
608: && ( $Apache::lonhomework::type ne 'anonsurvey' )
609: && ( $Apache::lonhomework::type ne 'anonsurveycred' ) )
610: {
611: &Apache::lonxml::error(
612: &mt('There are no true statements available.') . '<br />' );
613: }
614: }
615: else {
616: my $current = 0;
617: &Apache::lonhomework::showhash(%Apache::response::foilgroup);
618: my ( %top, %bottom );
619:
620: #first find out where everyone wants to be
621: foreach my $name (@names) {
622: $current++;
623: if ( $Apache::response::foilgroup{ $name . '.value' } eq 'true' ) {
624: push( @truelist, $name );
625: if ( $Apache::response::foilgroup{ $name . '.location' } eq
626: 'top' )
627: {
628: $top{$name} = $current;
629: }
630: elsif ( $Apache::response::foilgroup{ $name . '.location' } eq
631: 'bottom' )
632: {
633: $bottom{$name} = $current;
634: }
635: }
636: elsif (
637: $Apache::response::foilgroup{ $name . '.value' } eq 'false' )
638: {
639: push( @falselist, $name );
640: if ( $Apache::response::foilgroup{ $name . '.location' } eq
641: 'top' )
642: {
643: $top{$name} = $current;
644: }
645: elsif ( $Apache::response::foilgroup{ $name . '.location' } eq
646: 'bottom' )
647: {
648: $bottom{$name} = $current;
649: }
650: }
651: elsif (
652: $Apache::response::foilgroup{ $name . '.value' } eq 'unused' )
653: {
654: }
655: else {
656: &Apache::lonxml::error(
657: &HTML::Entities::encode(
658: "No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",
659: '<>&"'
660: )
661: );
662: }
663: }
664:
665: #pick a true statement
666: my $notrue = 0;
667: if ( scalar(@truelist) == 0 ) { $notrue = 1; }
668: my $whichtrue =
669: int( &Math::Random::random_uniform() * ( $#truelist + 1 ) );
670: &Apache::lonxml::debug(
671: "Max is $max, From $#truelist elms, picking $whichtrue");
672: my ( @toplist, @bottomlist );
673: my $topcount = 0;
674: my $bottomcount = 0;
675:
676: # assign everyone to either toplist/bottomlist or whichfalse
677: # which false is randomized, toplist bottomlist are in order
678: while (( ( $#whichfalse + $topcount + $bottomcount ) < $max - 2 )
679: && ( $#falselist > -1 ) )
680: {
681: &Apache::lonxml::debug("Have $#whichfalse max is $max");
682: my $afalse =
683: int( &Math::Random::random_uniform() * ( $#falselist + 1 ) );
684: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
685: $afalse = splice( @falselist, $afalse, 1 );
686: &Apache::lonxml::debug("Picked $afalse");
687: &Apache::lonhomework::showhash( ( 'names' => \@names ) );
688: &Apache::lonhomework::showhash(%top);
689: if ( $top{$afalse} ) {
690: $toplist[ $top{$afalse} ] = $afalse;
691: $topcount++;
692: }
693: elsif ( $bottom{$afalse} ) {
694: $bottomlist[ $bottom{$afalse} ] = $afalse;
695: $bottomcount++;
696: }
697: else {
698: push( @whichfalse, $afalse );
699: }
700: }
701: &Apache::lonxml::debug("Answer wants $answer");
702: my $truename = $truelist[$whichtrue];
703: my $dosplice = 1;
704: if ( ($notrue)
705: && ( $Apache::lonhomework::type ne 'survey' )
706: && ( $Apache::lonhomework::type ne 'surveycred' )
707: && ( $Apache::lonhomework::type ne 'anonsurvey' )
708: && ( $Apache::lonhomework::type ne 'anonsurveycred' ) )
709: {
710: $dosplice = 0;
711: &Apache::lonxml::error(
712: &mt('There are no true statements available.') . '<br />' );
713: }
714:
715: #insert the true statement, keeping track of where it wants to be
716: if ( $Apache::response::foilgroup{ $truename . '.location' } eq 'top'
717: && $dosplice )
718: {
719: $toplist[ $top{$truename} ] = $truename;
720: $answer = -1;
721: foreach my $top ( reverse(@toplist) ) {
722: if ($top) { $answer++; }
723: if ( $top eq $truename ) { last; }
724: }
725: $dosplice = 0;
726: }
727: elsif (
728: $Apache::response::foilgroup{ $truename . '.location' } eq 'bottom'
729: && $dosplice )
730: {
731: $bottomlist[ $bottom{$truename} ] = $truename;
732: $answer = -1;
733: foreach my $bot (@bottomlist) {
734: if ($bot) { $answer++; }
735: if ( $bot eq $truename ) { last; }
736: }
737: $answer += $topcount + $#whichfalse + 1;
738: $dosplice = 0;
739: }
740: else {
741: if ( $topcount > 0 || $bottomcount > 0 ) {
742: my $inc = 1;
743: if ( ( $bottomcount > 0 )
744: && ( $Apache::lonhomework::type ne 'exam' ) )
745: {
746: $inc = 2;
747: }
748: $answer = int(
749: &Math::Random::random_uniform() * ( $#whichfalse + $inc ) )
750: + $topcount;
751: }
752: }
753: &Apache::lonxml::debug("Answer now wants $answer");
754:
755: #add the top items to the top, bottom items to the bottom
756: for ( my $i = 0 ; $i <= $#toplist ; $i++ ) {
757: if ( $toplist[$i] ) { unshift( @whichfalse, $toplist[$i] ) }
758: }
759: for ( my $i = 0 ; $i <= $#bottomlist ; $i++ ) {
760: if ( $bottomlist[$i] ) { push( @whichfalse, $bottomlist[$i] ) }
761: }
762:
763: #if the true statement is randomized insert it into the list
764: if ($dosplice) {
765: splice( @whichfalse, $answer, 0, $truelist[$whichtrue] );
766: }
767: }
768: &Apache::lonxml::debug("Answer is $answer");
769: return ( $answer, @whichfalse );
770: }
771: ##
772: # Generate the HTML for a single html foil.
773: # @param $part - The part for which the response is being generated.
774: # @param $fieldname - The basename of the radiobutton field
775: # @param $name - The foilname.
776: # @param $last_responses - Reference to a hash that holds the most recent
777: # responses.
778: # @param $value - radiobutton value.
779: #
780: # @return text
781: # @retval The generated html.
782: #
783: sub html_radiobutton {
784: my ($part, $fieldname, $name, $last_responses, $value) = @_;
785:
786: my $result='<label>';
787:
788: $result .= '<input type="radio"
789: onchange="javascript:setSubmittedPart(' . "'$part');\""
790: . 'name="HWVAL_' . $fieldname . '"'
791: . "value='$value'";
792:
793: if (defined($last_responses->{$name})) {
794: $result .= ' checked="checked" ';
795: }
796: $result .= ' />';
797: $result .= $Apache::response::foilgroup{$name . '.text'};
798: $result .= '</label>';
799:
800: return $result;
801:
802: }
803:
804: ##
805: # Display foils in html rendition:
806: #
807: # @param $whichfoils - Set of foils to display.
808: # @param $target - Rendition target...there are several html targets.
809: # @param $direction - 'horizontal' if layout is horizontal.
810: # @param $part - Part of the problem that's being displayed.
811: # @param $solved - Solution state of the problem.
812: # @param $show_answer- True if answers should be shown.
813: #
814: # @return string
815: # @retval generated html.
816: #
817: sub display_foils_html {
818: my ($whichfoils, $target, $direction, $part, $solved, $show_answer) = @_;
819: my $result;
820:
821: # if the answers get shown, we need to label each item as correct or
822: # incorrect.
823:
824: if ($show_answer) {
825: my $item_pretext = '<br />'; # html prior to each item
826: my $item_posttext = ''; # html after each item.
827: my $finalclose = ''; # html to close off the whole shebang
828:
829:
830: # Horizontal layout is a table with each foil in a cell
831:
832: if ($direction eq 'horizontal') {
833: $result = '<table><tr>';
834: $item_pretext = '<td>' . $item_pretext;
835: $item_posttext = '</td>';
836: $finalclose = '</tr></table>';
837: }
838:
839: foreach my $name (@{$whichfoils}) {
840:
841: # If the item gets further surrounded by tags, this
842: # holds the closures for those tages.
843:
844: my $item_closetag = '';
845:
846: $result .= $item_pretext;
847:
848: # Label each foil as correct or incorrect:
849:
850: if ($Apache::response::foilgroup{$name . '.value'} eq 'true') {
851: $result .= &mt('Correct:') . '<b>';
852: $item_closetag .= '</b>';
853:
854: } else {
855: $result .= &mt('Incorrect');
856: }
857:
858: # Web rendition encloses the
859: # item text in a label tag as well:
860:
861: if ($target eq 'web') {
862: $result .= '<label>';
863: $item_closetag = '</label>' . $item_closetag;
864: }
865: $result .= $Apache::response::foilgroup{$name . '.text'};
866: $result .= $item_closetag;
867: $result .= $item_posttext;
868: $result .= "\n"; # make the html a bit more readable.
869: }
870:
871: $result .= $finalclose;
872:
873: } else {
874: # Not showing the answers, we need to generate the HTML appropriate
875: # to allowing the student to respond.
876:
877:
878: }
879:
880: return $result;
881: }
882:
883:
884: ##
885:
886: sub displayfoils {
887: my ( $target, $answer, $whichfoils, $direction, $bubbles_per_line ) = @_;
888: my $result;
889:
890: my $part = $Apache::inputtags::part;
891: my $solved = $Apache::lonhomework::history{"resource.$part.solved"};
892: if ( ( $target ne 'tex' )
893: && &Apache::response::show_answer() )
894: {
895:
896: $result = &display_foils_html(
897: $whichfoils, $target, $direction, $part, $solved, 1);
898:
899: }
900: else {
901: my @alphabet = ( 'A' .. 'Z' );
902: my $i = 0;
903: my $bubble_number = 0;
904: my $line = 0;
905: my $temp = 0;
906: my $id = $Apache::inputtags::response['-1'];
907: my $part = $Apache::inputtags::part;
908: my ( $lastresponse, $newvariation );
909:
910: if (
911: (
912: (
913: $Apache::lonhomework::history{"resource.$part.type"} eq
914: 'randomizetry'
915: )
916: || ( $Apache::lonhomework::type eq 'randomizetry' )
917: )
918: && ( $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
919: )
920: {
921:
922: if ( $env{ 'form.' . $part . '.rndseed' } ne
923: $Apache::lonhomework::history{"resource.$part.rndseed"} )
924: {
925: $newvariation = 1;
926: }
927: }
928: unless ($newvariation) {
929: $lastresponse =
930: $Apache::lonhomework::history{"resource.$part.$id.submission"};
931: }
932: my %lastresponse = &Apache::lonnet::str2hash($lastresponse);
933: if ( $target ne 'tex' && $direction eq 'horizontal' ) {
934: $result .= "<table><tr>";
935: }
936: my $numlines;
937: if ( ( $target eq 'tex' ) && ( $Apache::lonhomework::type eq 'exam' ) )
938: {
939: my $numitems = scalar( @{$whichfoils} );
940: $numlines = int( $numitems / $bubbles_per_line );
941: if ( ( $numitems % $bubbles_per_line ) != 0 ) {
942: $numlines++;
943: }
944: if ( $numlines < 1 ) {
945: $numlines = 1;
946: }
947: if ( $numlines > 1 ) {
948: my $linetext;
949: for ( my $i = 0 ; $i < $numlines ; $i++ ) {
950: $linetext .= $Apache::lonxml::counter + $i . ', ';
951: }
952: $linetext =~ s/,\s$//;
953: $result .=
954: '\item[\small {\textbf{'
955: . $linetext . '}}]'
956: . ' {\footnotesize '
957: . &mt( '(Bubble once in [_1] lines)', $numlines )
958: . '} \hspace*{\fill} \\\\';
959: }
960: else {
961: $result .= '\item[\textbf{' . $Apache::lonxml::counter . '}.]';
962: }
963: }
964: my $item_pretext;
965: my $item_posttext;
966: if ( $target ne 'tex' ) {
967: if ( $direction eq 'horizontal' ) {
968: $item_pretext = '<td>';
969: $item_posttext = '</td>';
970: }
971: else {
972: $item_pretext = '<br/>';
973: }
974: }
975: foreach my $name ( @{$whichfoils} ) {
976:
977: if ( $target ne 'tex' ) {
978: $result .= $item_pretext;
979: $result .= &html_radiobutton(
980: $part, $Apache::inputtags::response[-1],
981: $name, $lastresponse, $temp
982: );
983: $result .= $item_posttext;
984: }
985: else {
986: if ( $Apache::lonhomework::type eq 'exam' ) {
987: if ( $bubble_number >= $bubbles_per_line ) {
988: $line++;
989: $i = 0;
990: $bubble_number = 0;
991: }
992: my $identifier;
993: if ( $numlines > 1 ) {
994: $identifier = $Apache::lonxml::counter + $line;
995: }
996: $result .=
997: '{\small \textbf{'
998: . $identifier
999: . $alphabet[$i]
1000: . '}}$\bigcirc$'
1001: . $Apache::response::foilgroup{ $name . '.text' }
1002: . '\\\\'; #' stupid emacs
1003: $i++;
1004: $bubble_number++;
1005: }
1006: else {
1007: if ( $env{'form.pdfFormFields'} eq 'yes'
1008: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER' )
1009: {
1010: my $fieldname =
1011: $env{'request.symb'}
1012: . '&part_'
1013: . $Apache::inputtags::part
1014: . '&radiobuttonresponse'
1015: . '&HWVAL_'
1016: . $Apache::inputtags::response['-1'];
1017: $result .= '\item[{'
1018: . &Apache::lonxml::print_pdf_radiobutton( $fieldname,
1019: $temp )
1020: . '}]'
1021: . $Apache::response::foilgroup{ $name . '.text' }
1022: . "\n";
1023: }
1024: else {
1025: $result .= '\vspace*{-2 mm}\item '
1026: . $Apache::response::foilgroup{ $name . '.text' };
1027: }
1028: }
1029: }
1030:
1031: $temp++;
1032: }
1033: if ( $target ne 'tex' && $direction eq 'horizontal' ) {
1034: $result .= "</tr></table>";
1035: }
1036: }
1037: if ( $target ne 'tex'
1038: && ( $direction ne 'horizontal' )
1039: ) {
1040: $result .= "<br />";
1041: }
1042: if ($target eq 'tex') {
1043: $result .= '\vskip 0 mm ';
1044: }
1045: return $result;
1046: }
1047:
1048: sub displayallanswers {
1049: my @names;
1050: if ( $Apache::response::foilgroup{'names'} ) {
1051: @names = @{ $Apache::response::foilgroup{'names'} };
1052: }
1053: my $result = &Apache::response::answer_header('radiobuttonresponse');
1054: foreach my $name (@names) {
1055: $result .=
1056: &Apache::response::answer_part( 'radiobuttonresponse',
1057: $Apache::response::foilgroup{ $name . '.value' } );
1058: }
1059: $result .= &Apache::response::answer_footer('radiobuttonresponse');
1060: return $result;
1061: }
1062:
1063: sub displayanswers {
1064: my ( $answer, $whichopt, $bubbles_per_line ) = @_;
1065: my $result;
1066:
1067: if ( $Apache::lonhomework::type eq 'exam' ) {
1068: my $line = int( $answer / $bubbles_per_line );
1069: my $correct = ( 'A' .. 'Z' )[ $answer % $bubbles_per_line ];
1070: $result .=
1071: &Apache::response::answer_header( 'radiobuttonresponse', $line );
1072: $result .=
1073: &Apache::response::answer_part( 'radiobuttonresponse', $correct );
1074: }
1075: else {
1076: $result .= &Apache::response::answer_header('radiobuttonresponse');
1077: }
1078: foreach my $name ( @{$whichopt} ) {
1079: $result .=
1080: &Apache::response::answer_part( 'radiobuttonresponse',
1081: $Apache::response::foilgroup{ $name . '.value' } );
1082: }
1083: $result .= &Apache::response::answer_footer('radiobuttonresponse');
1084: return $result;
1085: }
1086:
1087: sub start_conceptgroup {
1088: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
1089: @_;
1090: $Apache::radiobuttonresponse::conceptgroup = 1;
1091: %Apache::response::conceptgroup = ();
1092: my $result;
1093: if ( $target eq 'edit' ) {
1094: $result .= &Apache::edit::tag_start( $target, $token );
1095: $result .=
1096: &Apache::edit::text_arg( 'Concept:', 'concept', $token, '50' )
1097: . &Apache::edit::end_row()
1098: . &Apache::edit::start_spanning_row();
1099: }
1100: elsif ( $target eq 'modified' ) {
1101: my $constructtag =
1102: &Apache::edit::get_new_args( $token, $parstack, $safeeval,
1103: 'concept' );
1104: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1105: }
1106: return $result;
1107: }
1108:
1109: sub end_conceptgroup {
1110: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
1111: @_;
1112: $Apache::radiobuttonresponse::conceptgroup = 0;
1113: my $result;
1114: if ( $target eq 'web'
1115: || $target eq 'grade'
1116: || $target eq 'answer'
1117: || $target eq 'tex'
1118: || $target eq 'analyze' )
1119: {
1120: &Apache::response::pick_foil_for_concept( $target,
1121: [ 'value', 'text', 'location' ],
1122: \%Apache::hint::radiobutton, $parstack, $safeeval );
1123: }
1124: elsif ( $target eq 'edit' ) {
1125: $result = &Apache::edit::end_table();
1126: }
1127: return $result;
1128: }
1129:
1130: sub insert_conceptgroup {
1131: my $result =
1132: "\n\t\t<conceptgroup concept=\"\">"
1133: . &insert_foil()
1134: . "\n\t\t</conceptgroup>\n";
1135: return $result;
1136: }
1137:
1138: sub start_foil {
1139: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
1140: @_;
1141: my $result = '';
1142: if ( $target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1143: &Apache::lonxml::startredirection;
1144: if ( $target eq 'analyze' ) {
1145: &Apache::response::check_if_computed( $token, $parstack, $safeeval,
1146: 'value' );
1147: }
1148: }
1149: elsif ( $target eq 'edit' ) {
1150: $result = &Apache::edit::tag_start( $target, $token );
1151: $result .= &Apache::edit::text_arg( 'Name:', 'name', $token );
1152: $result .= &Apache::edit::select_or_text_arg(
1153: 'Correct Option:', 'value',
1154: [ 'unused', 'true', 'false' ], $token
1155: );
1156: my $randomize =
1157: &Apache::lonxml::get_param( 'randomize', $parstack, $safeeval, '-3' );
1158: if ( $randomize ne 'no' ) {
1159: $result .=
1160: &Apache::edit::select_arg( 'Location:', 'location',
1161: [ 'random', 'top', 'bottom' ], $token );
1162: }
1163: $result .=
1164: &Apache::edit::end_row() . &Apache::edit::start_spanning_row();
1165: }
1166: elsif ( $target eq 'modified' ) {
1167: my $constructtag =
1168: &Apache::edit::get_new_args( $token, $parstack, $safeeval, 'value',
1169: 'name', 'location' );
1170: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1171: }
1172: return $result;
1173: }
1174:
1175: sub end_foil {
1176: my ( $target, $token, $tagstack, $parstack, $parser, $safeeval, $style ) =
1177: @_;
1178: my $text = '';
1179: if ( $target eq 'web' || $target eq 'tex' || $target eq 'analyze' ) {
1180: $text = &Apache::lonxml::endredirection;
1181: }
1182: if ( $target eq 'web'
1183: || $target eq 'grade'
1184: || $target eq 'answer'
1185: || $target eq 'tex'
1186: || $target eq 'analyze' )
1187: {
1188: my $value = &Apache::lonxml::get_param( 'value', $parstack, $safeeval );
1189: if ( $value ne 'unused' ) {
1190: my $name =
1191: &Apache::lonxml::get_param( 'name', $parstack, $safeeval );
1192: if ( $name eq "" ) {
1193: &Apache::lonxml::warning(
1194: &mt(
1195: 'Foils without names exist. This can cause problems to malfunction.'
1196: )
1197: );
1198: $name = $Apache::lonxml::curdepth;
1199: }
1200: if ( defined( $Apache::response::foilnames{$name} ) ) {
1201: &Apache::lonxml::error(
1202: &mt(
1203: 'Foil name [_1] appears more than once. Foil names need to be unique.',
1204: '<b><tt>' . $name . '</tt></b>'
1205: )
1206: );
1207: }
1208: $Apache::response::foilnames{$name}++;
1209: my $location =
1210: &Apache::lonxml::get_param( 'location', $parstack, $safeeval );
1211: if ( $Apache::radiobuttonresponse::conceptgroup
1212: && !&Apache::response::showallfoils() )
1213: {
1214: push @{ $Apache::response::conceptgroup{'names'} }, $name;
1215: $Apache::response::conceptgroup{"$name.value"} = $value;
1216: $Apache::response::conceptgroup{"$name.text"} = $text;
1217: $Apache::response::conceptgroup{"$name.location"} = $location;
1218: }
1219: else {
1220: push @{ $Apache::response::foilgroup{'names'} }, $name;
1221: $Apache::response::foilgroup{"$name.value"} = $value;
1222: $Apache::response::foilgroup{"$name.text"} = $text;
1223: $Apache::response::foilgroup{"$name.location"} = $location;
1224: }
1225: }
1226: }
1227: return '';
1228: }
1229:
1230: sub insert_foil {
1231: return '
1232: <foil name="" value="unused">
1233: <startouttext />
1234: <endouttext />
1235: </foil>';
1236: }
1237:
1238: 1;
1239: __END__
1240:
1241:
1242:
1243: =head1 NAME
1244:
1245: Apache::radiobuttonresponse
1246:
1247: =head1 SYNOPSIS
1248:
1249: Handles multiple-choice style responses.
1250:
1251: This is part of the LearningOnline Network with CAPA project
1252: described at http://www.lon-capa.org.
1253:
1254: =head1 SUBROUTINES
1255:
1256: =over
1257:
1258: =item start_radiobuttonresponse()
1259:
1260: =item bubble_line_count()
1261:
1262: =item end_radiobuttonresponse()
1263:
1264: =item start_foilgroup()
1265:
1266: =item storesurvey()
1267:
1268: =item grade_response()
1269:
1270: =item end_foilgroup()
1271:
1272: =item getfoilcounts()
1273:
1274: =item format_prior_answer()
1275:
1276: =item displayallfoils()
1277:
1278: =item &whichfoils($max,$randomize)
1279:
1280: Randomizes the list of foils.
1281: Respects
1282: - each foils desire to be randomized
1283: - the existance of Concept groups of foils (select 1 foil from each)
1284: - and selects a single correct statement from all possilble true statments
1285: - and limits it to a toal of $max foils
1286:
1287: WARNING: this routine uses the random number generator, it should only
1288: be called once per target, otherwise it can cause randomness changes in
1289: homework problems.
1290:
1291: Arguments
1292: $max - maximum number of foils to select (including the true one)
1293: (so a max of 5 is: 1 true, 4 false)
1294:
1295: $randomize - whether to randomize the listing of foils, by default
1296: will randomize, only if randomize is 'no' will it not
1297:
1298: Returns
1299: $answer - location in the array of the correct answer
1300: @foils - array of foil names in to display order
1301:
1302: =item displayfoils()
1303:
1304: =item displayallanswers()
1305:
1306: =item displayanswers()
1307:
1308: =item start_conceptgroup()
1309:
1310: =item end_conceptgroup()
1311:
1312: =item insert_conceptgroup()
1313:
1314: =item start_foil()
1315:
1316: =item end_foil()
1317:
1318: =item insert_foil()
1319:
1320: =back
1321:
1322: =cut
1323:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>