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