1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.122 2007/06/29 17:24:03 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 ($max,$randomize, $bubbles_per_line)=@_;
162: #keep the random numbers the same must always call this
163: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
164: if ( !&Apache::response::submitted() ) { return; }
165: my $response;
166:
167: if ($env{'form.submitted'} eq 'scantron') {
168: $response = &Apache::response::getresponse(1,undef,
169: &bubble_line_count(scalar(@whichfoils),
170: $bubbles_per_line),
171: $bubbles_per_line);
172:
173: } else {
174: $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
175: }
176:
177:
178: if ( $response !~ /[0-9]+/) { return; }
179: my $part=$Apache::inputtags::part;
180: my $id = $Apache::inputtags::response['-1'];
181: my %responsehash;
182: $responsehash{$whichfoils[$response]}=$response;
183: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
184: my %previous=&Apache::response::check_for_previous($responsestr,
185: $part,$id);
186: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
187: $responsestr;
188: &Apache::lonxml::debug("submitted a $response<br />\n");
189: my $ad;
190: if ($response == $answer) {
191: $ad='EXACT_ANS';
192: } else {
193: $ad='INCORRECT';
194: }
195: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
196: &Apache::response::handle_previous(\%previous,$ad);
197: }
198:
199: sub end_foilgroup {
200: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
201:
202: my $result;
203: my $bubble_lines;
204: my $bubbles_per_line;
205: my $answer_count;
206: my $id = $Apache::inputtags::response['-1'];
207: $bubbles_per_line =
208: &Apache::response::get_response_param($Apache::inputtags::part."_$id",
209: 'numbubbles',
210: $default_bubbles_per_line);
211:
212:
213: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
214: $target eq 'tex' || $target eq 'analyze') {
215: my $style = $Apache::lonhomework::type;
216: my $direction = &Apache::lonxml::get_param('direction',$parstack,
217: $safeeval,'-2');
218: if ( $style eq 'survey' && $target ne 'analyze') {
219: if ($target eq 'web' || $target eq 'tex') {
220: $result=&displayallfoils($direction, $target);
221: } elsif ( $target eq 'answer' ) {
222: $result=&displayallanswers();
223: } elsif ( $target eq 'grade' ) {
224: $result=&storesurvey();
225: }
226: $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
227:
228: } else {
229:
230: my $name;
231: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
232: '-2');
233: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
234: $safeeval,'-2');
235: my ($answer,@shown) = &whichfoils($max,$randomize);
236: $answer_count = scalar(@shown);
237:
238: if ($target eq 'web' || $target eq 'tex') {
239: $result=&displayfoils($target,
240: $max,
241: $randomize,
242: $direction,
243: $bubbles_per_line);
244: } elsif ($target eq 'answer' ) {
245: $result=&displayanswers($max,$randomize);
246: } elsif ( $target eq 'grade') {
247: &grade_response($max,$randomize,
248: $bubbles_per_line);
249: } elsif ( $target eq 'analyze') {
250: &Apache::response::analyze_store_foilgroup(\@shown,
251: ['text','value','location']);
252: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
253: push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
254: ('true','false'));
255: }
256: }
257: $Apache::lonxml::post_evaluate=0;
258: }
259: if ($target eq 'web') {
260: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
261: [\%Apache::response::foilgroup]);
262: }
263: $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
264: &Apache::response::poprandomnumber();
265: &Apache::lonxml::increment_counter($bubble_lines);
266: return $result;
267: }
268:
269: sub getfoilcounts {
270: my @names;
271: my $truecnt=0;
272: my $falsecnt=0;
273: my $name;
274: if ( $Apache::response::foilgroup{'names'} ) {
275: @names= @{ $Apache::response::foilgroup{'names'} };
276: }
277: foreach $name (@names) {
278: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
279: $truecnt++;
280: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
281: $falsecnt++;
282: }
283: }
284: return ($truecnt,$falsecnt);
285: }
286:
287: sub format_prior_answer {
288: my ($mode,$answer,$other_data) = @_;
289: my $foil_data = $other_data->[0];
290: my %response = &Apache::lonnet::str2hash($answer);
291: my ($name) = keys(%response);
292: return '<span class="LC_prior_radiobutton">'.
293: $foil_data->{$name.'.text'}.'</span>';
294:
295: }
296:
297: sub displayallfoils {
298: my ($direction, $target)=@_;
299: my $result;
300: &Apache::lonxml::debug("survey style display");
301: my @names;
302: &Apache::lonnet::loghthis("Display all foils");
303: if ( $Apache::response::foilgroup{'names'} ) {
304: @names= @{ $Apache::response::foilgroup{'names'} };
305: }
306:
307: my $temp=0;
308: my $i =0;
309: my $id=$Apache::inputtags::response['-1'];
310: my $part=$Apache::inputtags::part;
311: my $lastresponse=
312: $Apache::lonhomework::history{"resource.$part.$id.submission"};
313: if ($direction eq 'horizontal') { $result.='<table><tr>'; }
314: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
315: if (&Apache::response::show_answer() ) {
316: foreach my $name (@names) {
317: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
318: if (($direction eq 'horizontal') && ($target ne 'tex')) {
319: $result.="<td>";
320: } else {
321: if ($target eq 'tex') {
322: $result .= '\item \vskip -2mm ';
323: } else {
324: $result.="<br />";
325: }
326: }
327: if (defined($lastresponse{$name})) {
328: if ($target eq 'tex') {
329: $result .= '}';
330: } else {
331: $result.='<b>';
332: }
333: }
334: $result .= $Apache::response::foilgroup{$name.'.text'};
335: if (defined($lastresponse{$name}) && ($target ne 'tex')) {
336: $result.='</b>';
337: }
338: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
339: }
340: }
341: } else {
342: foreach my $name (@names) {
343: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
344: if ($direction eq 'horizontal') {
345: $result.="<td>";
346: } else {
347: if ($target eq 'tex') {
348: $result .= '\item \vskip -2mm ';
349: } else {
350: $result.="<br />";
351: }
352: }
353: if ($target eq 'tex') {
354: $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
355: $i++;
356: } else {
357: $result .= '<label>';
358: $result.="<input
359: onchange=\"javascript:setSubmittedPart('$part');\"
360: type=\"radio\"
361: name=\"HWVAL_$Apache::inputtags::response['-1']\"
362: value=\"$temp\" ";
363: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
364: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
365: '</label>';
366: }
367: $temp++;
368: if ($target ne 'tex') {
369: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
370: } else {
371: $result.='\vskip 0 mm ';
372: }
373: }
374: }
375: }
376:
377: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
378: return $result;
379: }
380:
381: =pod
382:
383: =item &whichfoils($max,$randomize)
384:
385: Randomizes the list of foils.
386: Respects
387: - each foils desire to be randomized
388: - the existance of Concept groups of foils (select 1 foil from each)
389: - and selects a single correct statement from all possilble true statments
390: - and limits it to a toal of $max foils
391:
392: Arguments
393: $max - maximum number of foils to select (including the true one)
394: (so a max of 5 is: 1 true, 4 false)
395:
396: $randomize - whether to randomize the listing of foils, by default
397: will randomize, only if randomize is 'no' will it not
398:
399: Returns
400: $answer - location in the array of the correct answer
401: @foils - array of foil names in to display order
402:
403: =cut
404:
405: sub whichfoils {
406: my ($max,$randomize)=@_;
407:
408: my @truelist;
409: my @falselist;
410: my @whichfalse =();
411: my ($truecnt,$falsecnt) = &getfoilcounts();
412: my $count=0;
413: # we will add in 1 of the true statements
414: if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
415: my $answer=int(&Math::Random::random_uniform() * ($count));
416: &Apache::lonxml::debug("Count is $count, $answer is $answer");
417: my @names;
418: if ( $Apache::response::foilgroup{'names'} ) {
419: @names= @{ $Apache::response::foilgroup{'names'} };
420: }
421: if (&Apache::response::showallfoils()) {
422: @whichfalse=@names;
423: } elsif ($randomize eq 'no') {
424: &Apache::lonxml::debug("No randomization");
425: my $havetrue=0;
426: foreach my $name (@names) {
427: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
428: if (!$havetrue ) {
429: push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
430: }
431: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
432: push (@whichfalse,$name);
433: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
434: } else {
435: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
436: }
437: }
438: if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
439: &Apache::lonxml::error("There are no true statements available.<br />");
440: }
441: } else {
442: my $current=0;
443: &Apache::lonhomework::showhash(%Apache::response::foilgroup);
444: my (%top,%bottom);
445: #first find out where everyone wants to be
446: foreach my $name (@names) {
447: $current++;
448: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
449: push (@truelist,$name);
450: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
451: $top{$name}=$current;
452: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
453: $bottom{$name}=$current;
454: }
455: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
456: push (@falselist,$name);
457: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
458: $top{$name}=$current;
459: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
460: $bottom{$name}=$current;
461: }
462: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
463: } else {
464: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
465: }
466: }
467: #pick a true statement
468: my $notrue=0;
469: if (scalar(@truelist) == 0) { $notrue=1; }
470: my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
471: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
472: my (@toplist, @bottomlist);
473: my $topcount=0;
474: my $bottomcount=0;
475: # assign everyone to either toplist/bottomlist or whichfalse
476: # which false is randomized, toplist bottomlist are in order
477: while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
478: &Apache::lonxml::debug("Have $#whichfalse max is $max");
479: my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
480: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
481: $afalse=splice(@falselist,$afalse,1);
482: &Apache::lonxml::debug("Picked $afalse");
483: &Apache::lonhomework::showhash(('names'=>\@names));
484: &Apache::lonhomework::showhash(%top);
485: if ($top{$afalse}) {
486: $toplist[$top{$afalse}]=$afalse;
487: $topcount++;
488: } elsif ($bottom{$afalse}) {
489: $bottomlist[$bottom{$afalse}]=$afalse;
490: $bottomcount++;
491: } else {
492: push (@whichfalse,$afalse);
493: }
494: }
495: &Apache::lonxml::debug("Answer wants $answer");
496: my $truename=$truelist[$whichtrue];
497: my $dosplice=1;
498: if ($notrue && $Apache::lonhomework::type ne 'survey') {
499: $dosplice=0;
500: &Apache::lonxml::error("There are no true statements available.<br />");
501: }
502: #insert the true statement, keeping track of where it wants to be
503: if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
504: $toplist[$top{$truename}]=$truename;
505: $answer=-1;
506: foreach my $top (reverse(@toplist)) {
507: if ($top) { $answer++;}
508: if ($top eq $truename) { last; }
509: }
510: $dosplice=0;
511: } elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
512: $bottomlist[$bottom{$truename}]=$truename;
513: $answer=-1;
514: foreach my $bot (@bottomlist) {
515: if ($bot) { $answer++;}
516: if ($bot eq $truename) { last; }
517: }
518: $answer+=$topcount+$#whichfalse+1;
519: $dosplice=0;
520: } else {
521: if ($topcount>0 || $bottomcount>0) {
522: $answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
523: + $topcount;
524: }
525: }
526: &Apache::lonxml::debug("Answer now wants $answer");
527: #add the top items to the top, bottom items to the bottom
528: for (my $i=0;$i<=$#toplist;$i++) {
529: if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
530: }
531: for (my $i=0;$i<=$#bottomlist;$i++) {
532: if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
533: }
534: #if the true statement is randomized insert it into the list
535: if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
536: }
537: &Apache::lonxml::debug("Answer is $answer");
538: return ($answer,@whichfalse);
539: }
540:
541: sub displayfoils {
542: my ($target,$max,$randomize,$direction, $bubbles_per_line)=@_;
543: my $result;
544:
545: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
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 ($max,$randomize)=@_;
656: my ($answer,@whichopt) = &whichfoils($max,$randomize);
657: my $result=&Apache::response::answer_header('radiobuttonresponse');
658: if ($Apache::lonhomework::type eq 'exam') {
659: my $correct = ('A'..'Z')[$answer];
660: $result.=&Apache::response::answer_part('radiobuttonresponse',
661: $correct);
662: }
663: foreach my $name (@whichopt) {
664: $result.=&Apache::response::answer_part('radiobuttonresponse',
665: $Apache::response::foilgroup{$name.'.value'});
666: }
667: $result.=&Apache::response::answer_footer('radiobuttonresponse');
668: return $result;
669: }
670:
671: sub start_conceptgroup {
672: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
673: $Apache::radiobuttonresponse::conceptgroup=1;
674: %Apache::response::conceptgroup=();
675: my $result;
676: if ($target eq 'edit') {
677: $result.=&Apache::edit::tag_start($target,$token);
678: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
679: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
680: } elsif ($target eq 'modified') {
681: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
682: $safeeval,'concept');
683: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
684: }
685: return $result;
686: }
687:
688: sub end_conceptgroup {
689: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
690: $Apache::radiobuttonresponse::conceptgroup=0;
691: my $result;
692: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
693: $target eq 'tex' || $target eq 'analyze') {
694: &Apache::response::pick_foil_for_concept($target,
695: ['value','text','location'],
696: \%Apache::hint::radiobutton,
697: $parstack,$safeeval);
698: } elsif ($target eq 'edit') {
699: $result=&Apache::edit::end_table();
700: }
701: return $result;
702: }
703:
704: sub insert_conceptgroup {
705: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
706: return $result;
707: }
708:
709: sub start_foil {
710: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
711: my $result='';
712: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
713: &Apache::lonxml::startredirection;
714: if ($target eq 'analyze') {
715: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
716: }
717: } elsif ($target eq 'edit') {
718: $result=&Apache::edit::tag_start($target,$token);
719: $result.=&Apache::edit::text_arg('Name:','name',$token);
720: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
721: ['unused','true','false'],
722: $token);
723: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
724: $safeeval,'-3');
725: if ($randomize ne 'no') {
726: $result.=&Apache::edit::select_arg('Location:','location',
727: ['random','top','bottom'],$token);
728: }
729: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
730: } elsif ($target eq 'modified') {
731: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
732: $safeeval,'value','name',
733: 'location');
734: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
735: }
736: return $result;
737: }
738:
739: sub end_foil {
740: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
741: my $text='';
742: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
743: $text=&Apache::lonxml::endredirection;
744: }
745: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
746: || $target eq 'tex' || $target eq 'analyze') {
747: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
748: if ($value ne 'unused') {
749: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
750: if ($name eq "") {
751: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
752: $name=$Apache::lonxml::curdepth;
753: }
754: if (defined($Apache::response::foilnames{$name})) {
755: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
756: }
757: $Apache::response::foilnames{$name}++;
758: my $location =&Apache::lonxml::get_param('location',$parstack,
759: $safeeval);
760: if ( $Apache::radiobuttonresponse::conceptgroup
761: && !&Apache::response::showallfoils() ) {
762: push @{ $Apache::response::conceptgroup{'names'} }, $name;
763: $Apache::response::conceptgroup{"$name.value"} = $value;
764: $Apache::response::conceptgroup{"$name.text"} = $text;
765: $Apache::response::conceptgroup{"$name.location"} = $location;
766: } else {
767: push @{ $Apache::response::foilgroup{'names'} }, $name;
768: $Apache::response::foilgroup{"$name.value"} = $value;
769: $Apache::response::foilgroup{"$name.text"} = $text;
770: $Apache::response::foilgroup{"$name.location"} = $location;
771: }
772: }
773: }
774: return '';
775: }
776:
777: sub insert_foil {
778: return '
779: <foil name="" value="unused">
780: <startouttext />
781: <endouttext />
782: </foil>';
783: }
784: 1;
785: __END__
786:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>