1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
3: #
4: # $Id: radiobuttonresponse.pm,v 1.124 2007/07/17 20:44:07 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, $bubbles_per_line);
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 $line = 0;
589: my $temp=0;
590: my $id=$Apache::inputtags::response['-1'];
591: my $part=$Apache::inputtags::part;
592: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
593: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
594: if ($target ne 'tex' && $direction eq 'horizontal') {
595: $result.="<table><tr>";
596: }
597: foreach my $name (@{ $whichfoils }) {
598: if ($target ne 'tex') {
599: if ($direction eq 'horizontal') {
600: $result.="<td>";
601: } else {
602: $result.="<br />";
603: }
604: }
605: if ($target ne 'tex') {
606: $result.= '<label>';
607: $result.=
608: "<input type=\"radio\"
609: onchange=\"javascript:setSubmittedPart('$part');\"
610: name=\"HWVAL_$Apache::inputtags::response['-1']\"
611: value=\"$temp\" ";
612: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
613: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
614: } else {
615: if ($Apache::lonhomework::type eq 'exam') {
616: if($bubble_number >= $bubbles_per_line) {
617: $line++;
618: $i = 0;
619: $bubble_number = 0;
620: $result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
621: }
622: $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
623: $i++;
624: $bubble_number++;
625: } else {
626: $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
627: }
628: }
629: if ($target ne 'tex' && $direction eq 'horizontal') {
630: $result.="</td>";
631: }
632: $temp++;
633: }
634: if ($target ne 'tex' && $direction eq 'horizontal') {
635: $result.="</tr></table>";
636: }
637: }
638: if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
639: return $result;
640: }
641:
642: sub displayallanswers {
643: my @names;
644: if ( $Apache::response::foilgroup{'names'} ) {
645: @names= @{ $Apache::response::foilgroup{'names'} };
646: }
647: my $result=&Apache::response::answer_header('radiobuttonresponse');
648: foreach my $name (@names) {
649: $result.=&Apache::response::answer_part('radiobuttonresponse',
650: $Apache::response::foilgroup{$name.'.value'});
651: }
652: $result.=&Apache::response::answer_footer('radiobuttonresponse');
653: return $result;
654: }
655:
656: sub displayanswers {
657: my ($answer, $whichopt, $bubbles_per_line)=@_;
658: my $result;
659:
660: if ($Apache::lonhomework::type eq 'exam') {
661: my $line = int($answer/$bubbles_per_line);
662: my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
663: $result .= &Apache::response::answer_header('radiobuttonresponse',
664: $line);
665: $result .= &Apache::response::answer_part('radiobuttonresponse',
666: $correct);
667: } else {
668: $result .= &Apache::response::answer_header('radiobuttonresponse');
669: }
670: foreach my $name (@{ $whichopt }) {
671: $result.=&Apache::response::answer_part('radiobuttonresponse',
672: $Apache::response::foilgroup{$name.'.value'});
673: }
674: $result.=&Apache::response::answer_footer('radiobuttonresponse');
675: return $result;
676: }
677:
678: sub start_conceptgroup {
679: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
680: $Apache::radiobuttonresponse::conceptgroup=1;
681: %Apache::response::conceptgroup=();
682: my $result;
683: if ($target eq 'edit') {
684: $result.=&Apache::edit::tag_start($target,$token);
685: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
686: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
687: } elsif ($target eq 'modified') {
688: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
689: $safeeval,'concept');
690: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
691: }
692: return $result;
693: }
694:
695: sub end_conceptgroup {
696: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
697: $Apache::radiobuttonresponse::conceptgroup=0;
698: my $result;
699: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
700: $target eq 'tex' || $target eq 'analyze') {
701: &Apache::response::pick_foil_for_concept($target,
702: ['value','text','location'],
703: \%Apache::hint::radiobutton,
704: $parstack,$safeeval);
705: } elsif ($target eq 'edit') {
706: $result=&Apache::edit::end_table();
707: }
708: return $result;
709: }
710:
711: sub insert_conceptgroup {
712: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
713: return $result;
714: }
715:
716: sub start_foil {
717: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
718: my $result='';
719: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
720: &Apache::lonxml::startredirection;
721: if ($target eq 'analyze') {
722: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
723: }
724: } elsif ($target eq 'edit') {
725: $result=&Apache::edit::tag_start($target,$token);
726: $result.=&Apache::edit::text_arg('Name:','name',$token);
727: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
728: ['unused','true','false'],
729: $token);
730: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
731: $safeeval,'-3');
732: if ($randomize ne 'no') {
733: $result.=&Apache::edit::select_arg('Location:','location',
734: ['random','top','bottom'],$token);
735: }
736: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
737: } elsif ($target eq 'modified') {
738: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
739: $safeeval,'value','name',
740: 'location');
741: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
742: }
743: return $result;
744: }
745:
746: sub end_foil {
747: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
748: my $text='';
749: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
750: $text=&Apache::lonxml::endredirection;
751: }
752: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
753: || $target eq 'tex' || $target eq 'analyze') {
754: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
755: if ($value ne 'unused') {
756: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
757: if ($name eq "") {
758: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
759: $name=$Apache::lonxml::curdepth;
760: }
761: if (defined($Apache::response::foilnames{$name})) {
762: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
763: }
764: $Apache::response::foilnames{$name}++;
765: my $location =&Apache::lonxml::get_param('location',$parstack,
766: $safeeval);
767: if ( $Apache::radiobuttonresponse::conceptgroup
768: && !&Apache::response::showallfoils() ) {
769: push @{ $Apache::response::conceptgroup{'names'} }, $name;
770: $Apache::response::conceptgroup{"$name.value"} = $value;
771: $Apache::response::conceptgroup{"$name.text"} = $text;
772: $Apache::response::conceptgroup{"$name.location"} = $location;
773: } else {
774: push @{ $Apache::response::foilgroup{'names'} }, $name;
775: $Apache::response::foilgroup{"$name.value"} = $value;
776: $Apache::response::foilgroup{"$name.text"} = $text;
777: $Apache::response::foilgroup{"$name.location"} = $location;
778: }
779: }
780: }
781: return '';
782: }
783:
784: sub insert_foil {
785: return '
786: <foil name="" value="unused">
787: <startouttext />
788: <endouttext />
789: </foil>';
790: }
791: 1;
792: __END__
793:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>