Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.113
1.22 albertel 1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
1.31 albertel 3: #
1.113 ! albertel 4: # $Id: radiobuttonresponse.pm,v 1.112 2007/03/01 02:31:20 albertel Exp $
1.31 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
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: #
1.1 albertel 28:
29: package Apache::radiobuttonresponse;
30: use strict;
1.42 albertel 31: use HTML::Entities();
1.85 albertel 32: use Apache::lonlocal;
1.100 albertel 33: use Apache::lonnet;
1.1 albertel 34:
1.36 harris41 35: BEGIN {
1.83 albertel 36: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1 albertel 37: }
38:
39: sub start_radiobuttonresponse {
1.83 albertel 40: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
41: my $result;
42: #when in a radiobutton response use these
43: &Apache::lonxml::register('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
44: push (@Apache::lonxml::namespace,'radiobuttonresponse');
45: my $id = &Apache::response::start_response($parstack,$safeeval);
46: %Apache::hint::radiobutton=();
1.85 albertel 47: undef(%Apache::response::foilnames);
1.83 albertel 48: if ($target eq 'meta') {
49: $result=&Apache::response::meta_package_write('radiobuttonresponse');
50: } elsif ($target eq 'edit' ) {
51: $result.=&Apache::edit::start_table($token).
52: '<tr><td>'.&Apache::lonxml::description($token).
53: &Apache::loncommon::help_open_topic('Radio_Response_Problems').
54: "</td><td>Delete:".
55: &Apache::edit::deletelist($target,$token)
56: ."</td><td> ".&Apache::edit::end_row()
57: .&Apache::edit::start_spanning_row();
58: $result.=
59: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',
60: $token,'4').
61: &Apache::edit::select_arg('Randomize Foil Order','randomize',
62: ['yes','no'],$token).
1.103 albertel 63: &Apache::edit::select_arg('Display Direction','direction',
64: ['vertical','horizontal'],$token).
1.83 albertel 65: &Apache::edit::end_row().
66: &Apache::edit::start_spanning_row()."\n";
67: } elsif ($target eq 'modified') {
68: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
69: $safeeval,'max',
1.103 albertel 70: 'randomize','direction');
1.83 albertel 71: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
72: } elsif ($target eq 'tex') {
73: my $type=&Apache::lonxml::get_param('TeXtype',$parstack,$safeeval,
74: undef,0);
75: if ($type eq '1') {
76: $result .= ' \renewcommand{\labelenumi}{\arabic{enumi}.}';
77: } elsif ($type eq 'A') {
78: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
79: } elsif ($type eq 'a') {
80: $result .= ' \renewcommand{\labelenumi}{\alph{enumi}.}';
81: } elsif ($type eq 'i') {
82: $result .= ' \renewcommand{\labelenumi}{\roman{enumi}.}';
1.88 albertel 83: } else {
84: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
1.83 albertel 85: }
86: $result .= '\begin{enumerate}';
87: } elsif ($target eq 'analyze') {
88: my $part_id="$Apache::inputtags::part.$id";
89: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
90: }
91: return $result;
1.1 albertel 92: }
93:
94: sub end_radiobuttonresponse {
1.83 albertel 95: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
96: my $result;
97: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
98: if ($target eq 'tex') { $result .= '\end{enumerate}'; }
99: &Apache::response::end_response;
100: pop @Apache::lonxml::namespace;
101: &Apache::lonxml::deregister('Apache::radiobuttonresponse',('foilgroup','foil','conceptgroup'));
1.85 albertel 102: undef(%Apache::response::foilnames);
1.83 albertel 103: return $result;
1.1 albertel 104: }
105:
1.43 albertel 106: %Apache::response::foilgroup=();
1.1 albertel 107: sub start_foilgroup {
1.83 albertel 108: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
109: my $result;
110: %Apache::response::foilgroup=();
111: $Apache::radiobuttonresponse::conceptgroup=0;
1.89 albertel 112: &Apache::response::pushrandomnumber();
1.83 albertel 113: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
114: $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
115: }
116: return $result;
1.5 albertel 117: }
118:
1.15 albertel 119: sub storesurvey {
1.99 albertel 120: if ( !&Apache::response::submitted() ) { return ''; }
1.100 albertel 121: my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83 albertel 122: &Apache::lonxml::debug("Here I am!:$response:");
123: if ( $response !~ /[0-9]+/) { return ''; }
1.96 albertel 124: my $part = $Apache::inputtags::part;
1.83 albertel 125: my $id = $Apache::inputtags::response['-1'];
126: my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
127: my %responsehash;
128: $responsehash{$whichfoils[$response]}=$response;
1.96 albertel 129: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
130: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
131: $responsestr;
132: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
133: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
134: &Apache::response::handle_previous(\%previous,$ad);
1.83 albertel 135: &Apache::lonxml::debug("submitted a $response<br />\n");
136: return '';
1.15 albertel 137: }
138:
1.32 albertel 139: sub grade_response {
1.83 albertel 140: my ($max,$randomize)=@_;
141: #keep the random numbers the same must always call this
142: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.99 albertel 143: if ( !&Apache::response::submitted() ) { return; }
1.83 albertel 144: my $response;
1.100 albertel 145: if ($env{'form.submitted'} eq 'scantron') {
1.83 albertel 146: $response=&Apache::response::getresponse();
147: } else {
1.100 albertel 148: $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83 albertel 149: }
150: if ( $response !~ /[0-9]+/) { return; }
151: my $part=$Apache::inputtags::part;
152: my $id = $Apache::inputtags::response['-1'];
153: my %responsehash;
154: $responsehash{$whichfoils[$response]}=$response;
155: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
156: my %previous=&Apache::response::check_for_previous($responsestr,
157: $part,$id);
158: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
159: $responsestr;
160: &Apache::lonxml::debug("submitted a $response<br />\n");
161: my $ad;
162: if ($response == $answer) {
163: $ad='EXACT_ANS';
164: } else {
165: $ad='INCORRECT';
166: }
167: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
168: &Apache::response::handle_previous(\%previous,$ad);
1.32 albertel 169: }
170:
1.1 albertel 171: sub end_foilgroup {
1.83 albertel 172: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29 albertel 173:
1.83 albertel 174: my $result;
175: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
176: $target eq 'tex' || $target eq 'analyze') {
177: my $style = $Apache::lonhomework::type;
1.93 albertel 178: my $direction = &Apache::lonxml::get_param('direction',$parstack,
179: $safeeval,'-2');
1.83 albertel 180: if ( $style eq 'survey' && $target ne 'analyze') {
181: if ($target eq 'web' || $target eq 'tex') {
1.110 foxr 182: $result=&displayallfoils($direction, $target);
1.112 albertel 183: if ($target eq 'web') {
184: &setup_prior_tries_hash();
185: }
1.83 albertel 186: } elsif ( $target eq 'answer' ) {
187: $result=&displayallanswers();
188: } elsif ( $target eq 'grade' ) {
189: $result=&storesurvey();
190: }
191: } else {
192: my $name;
193: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,
194: '-2');
195: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
196: $safeeval,'-2');
197: if ($target eq 'web' || $target eq 'tex') {
1.90 albertel 198: $result=&displayfoils($target,$max,$randomize,$direction);
1.112 albertel 199: if ($target eq 'web') {
200: &setup_prior_tries_hash();
201: }
1.83 albertel 202: } elsif ($target eq 'answer' ) {
203: $result=&displayanswers($max,$randomize);
204: } elsif ( $target eq 'grade') {
205: &grade_response($max,$randomize);
206: } elsif ( $target eq 'analyze') {
207: my @shown = &whichfoils($max,$randomize);
208: &Apache::response::analyze_store_foilgroup(\@shown,
209: ['text','value','location']);
210: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
211: push (@{ $Apache::lonhomework::analyze{"$part_id.options"} },
212: ('true','false'));
213: }
1.81 albertel 214: }
1.111 albertel 215: $Apache::lonxml::post_evaluate=0;
1.83 albertel 216: }
1.89 albertel 217: &Apache::response::poprandomnumber();
1.83 albertel 218: &Apache::lonxml::increment_counter();
219: return $result;
1.6 albertel 220: }
221:
222: sub getfoilcounts {
1.83 albertel 223: my @names;
224: my $truecnt=0;
225: my $falsecnt=0;
226: my $name;
227: if ( $Apache::response::foilgroup{'names'} ) {
228: @names= @{ $Apache::response::foilgroup{'names'} };
1.6 albertel 229: }
1.83 albertel 230: foreach $name (@names) {
231: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
232: $truecnt++;
233: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
234: $falsecnt++;
235: }
236: }
237: return ($truecnt,$falsecnt);
1.5 albertel 238: }
239:
1.112 albertel 240: sub setup_prior_tries_hash {
241: my $part=$Apache::inputtags::part;
242: my $id=$Apache::inputtags::response[-1];
243: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
244: my $key = "$i:resource.$part.$id.submission";
245: next if (!exists($Apache::lonhomework::history{"$key"}));
246: my %response =
247: &Apache::lonnet::str2hash($Apache::lonhomework::history{$key});
248: my ($name) = keys(%response);
249: $Apache::inputtags::submission_display{$key} =
250: $Apache::response::foilgroup{$name.'.text'}
251: }
252: }
253:
1.15 albertel 254: sub displayallfoils {
1.110 foxr 255: my ($direction, $target)=@_;
1.83 albertel 256: my $result;
257: &Apache::lonxml::debug("survey style display");
1.106 albertel 258: my @names;
259: if ( $Apache::response::foilgroup{'names'} ) {
260: @names= @{ $Apache::response::foilgroup{'names'} };
261: }
1.83 albertel 262: my $temp=0;
1.110 foxr 263: my $i =0;
1.83 albertel 264: my $id=$Apache::inputtags::response['-1'];
265: my $part=$Apache::inputtags::part;
266: my $lastresponse=
267: $Apache::lonhomework::history{"resource.$part.$id.submission"};
1.93 albertel 268: if ($direction eq 'horizontal') { $result.='<table><tr>'; }
1.83 albertel 269: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
270: if (&Apache::response::show_answer() ) {
271: foreach my $name (@names) {
272: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.110 foxr 273: if (($direction eq 'horizontal') && ($target ne 'tex')) {
1.93 albertel 274: $result.="<td>";
275: } else {
1.110 foxr 276: if ($target eq 'tex') {
277: $result .= '\item \vskip -2mm ';
278: } else {
279: $result.="<br />";
280: }
1.93 albertel 281: }
1.84 albertel 282: if (defined($lastresponse{$name})) {
1.110 foxr 283: if ($target eq 'tex') {
284: $result .= '}';
285: } else {
286: $result.='<b>';
287: }
1.83 albertel 288: }
289: $result .= $Apache::response::foilgroup{$name.'.text'};
1.110 foxr 290: if (defined($lastresponse{$name}) && ($target ne 'tex')) {
1.83 albertel 291: $result.='</b>';
292: }
1.110 foxr 293: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
1.83 albertel 294: }
1.45 albertel 295: }
1.83 albertel 296: } else {
297: foreach my $name (@names) {
298: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.93 albertel 299: if ($direction eq 'horizontal') {
300: $result.="<td>";
301: } else {
1.110 foxr 302: if ($target eq 'tex') {
303: $result .= '\item \vskip -2mm ';
304: } else {
305: $result.="<br />";
306: }
307: }
308: if ($target eq 'tex') {
309: $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
310: $i++;
311: } else {
312: $result .= '<label>';
1.113 ! albertel 313: $result.="<input
! 314: onchange=\"javascript:setSubmittedPart('$part');\"
! 315: type=\"radio\"
! 316: name=\"HWVAL_$Apache::inputtags::response['-1']\"
! 317: value=\"$temp\" ";
1.110 foxr 318: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
319: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
320: '</label>';
1.93 albertel 321: }
1.83 albertel 322: $temp++;
1.110 foxr 323: if ($target ne 'tex') {
324: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
325: } else {
326: $result.='\vskip 0 mm ';
327: }
1.83 albertel 328: }
1.45 albertel 329: }
330: }
1.110 foxr 331: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
1.83 albertel 332: return $result;
1.15 albertel 333: }
334:
1.28 albertel 335: sub whichfoils {
1.83 albertel 336: my ($max,$randomize)=@_;
1.28 albertel 337:
1.83 albertel 338: my @truelist;
339: my @falselist;
340: my @whichfalse =();
341: my ($truecnt,$falsecnt) = &getfoilcounts();
342: my $count=0;
343: # we will add in 1 of the true statements
1.104 albertel 344: if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83 albertel 345: my $answer=int(&Math::Random::random_uniform() * ($count));
346: &Apache::lonxml::debug("Count is $count, $answer is $answer");
347: my @names;
348: if ( $Apache::response::foilgroup{'names'} ) {
349: @names= @{ $Apache::response::foilgroup{'names'} };
350: }
351: if (&Apache::response::showallfoils()) {
352: @whichfalse=@names;
353: } elsif ($randomize eq 'no') {
354: &Apache::lonxml::debug("No randomization");
355: my $havetrue=0;
356: foreach my $name (@names) {
357: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
358: if (!$havetrue ) {
359: push(@whichfalse,$name); $havetrue++; $answer=$#whichfalse;
360: }
361: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
362: push (@whichfalse,$name);
363: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
364: } else {
1.87 albertel 365: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83 albertel 366: }
367: }
1.97 albertel 368: if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
369: &Apache::lonxml::error("There are no true statements available.<br />");
370: }
1.83 albertel 371: } else {
372: my $current=0;
373: &Apache::lonhomework::showhash(%Apache::response::foilgroup);
374: my (%top,%bottom);
375: #first find out where everyone wants to be
376: foreach my $name (@names) {
377: $current++;
378: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
379: push (@truelist,$name);
380: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
381: $top{$name}=$current;
382: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
383: $bottom{$name}=$current;
384: }
385: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'false') {
386: push (@falselist,$name);
387: if ($Apache::response::foilgroup{$name.'.location'} eq 'top') {
388: $top{$name}=$current;
389: } elsif ($Apache::response::foilgroup{$name.'.location'} eq 'bottom') {
390: $bottom{$name}=$current;
391: }
392: } elsif ($Apache::response::foilgroup{$name.'.value'} eq 'unused') {
393: } else {
1.87 albertel 394: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83 albertel 395: }
396: }
397: #pick a true statement
398: my $notrue=0;
399: if (scalar(@truelist) == 0) { $notrue=1; }
400: my $whichtrue = int(&Math::Random::random_uniform() * ($#truelist+1));
401: &Apache::lonxml::debug("Max is $max, From $#truelist elms, picking $whichtrue");
402: my (@toplist, @bottomlist);
403: my $topcount=0;
404: my $bottomcount=0;
405: # assign everyone to either toplist/bottomlist or whichfalse
406: # which false is randomized, toplist bottomlist are in order
407: while ((($#whichfalse+$topcount+$bottomcount) < $max-2) && ($#falselist > -1)) {
408: &Apache::lonxml::debug("Have $#whichfalse max is $max");
409: my $afalse=int(&Math::Random::random_uniform() * ($#falselist+1));
410: &Apache::lonxml::debug("From $#falselist elms, picking $afalse");
411: $afalse=splice(@falselist,$afalse,1);
412: &Apache::lonxml::debug("Picked $afalse");
413: &Apache::lonhomework::showhash(('names'=>\@names));
414: &Apache::lonhomework::showhash(%top);
415: if ($top{$afalse}) {
416: $toplist[$top{$afalse}]=$afalse;
417: $topcount++;
418: } elsif ($bottom{$afalse}) {
419: $bottomlist[$bottom{$afalse}]=$afalse;
420: $bottomcount++;
421: } else {
422: push (@whichfalse,$afalse);
423: }
424: }
425: &Apache::lonxml::debug("Answer wants $answer");
426: my $truename=$truelist[$whichtrue];
427: my $dosplice=1;
428: if ($notrue && $Apache::lonhomework::type ne 'survey') {
429: $dosplice=0;
430: &Apache::lonxml::error("There are no true statements available.<br />");
431: }
432: #insert the true statement, keeping track of where it wants to be
433: if ($Apache::response::foilgroup{$truename.'.location'} eq 'top' && $dosplice) {
434: $toplist[$top{$truename}]=$truename;
435: $answer=-1;
436: foreach my $top (reverse(@toplist)) {
437: if ($top) { $answer++;}
438: if ($top eq $truename) { last; }
1.49 albertel 439: }
1.83 albertel 440: $dosplice=0;
441: } elsif ($Apache::response::foilgroup{$truename.'.location'} eq 'bottom' && $dosplice) {
442: $bottomlist[$bottom{$truename}]=$truename;
443: $answer=-1;
444: foreach my $bot (@bottomlist) {
445: if ($bot) { $answer++;}
446: if ($bot eq $truename) { last; }
1.49 albertel 447: }
1.83 albertel 448: $answer+=$topcount+$#whichfalse+1;
449: $dosplice=0;
1.49 albertel 450: } else {
1.83 albertel 451: if ($topcount>0 || $bottomcount>0) {
452: $answer = int(&Math::Random::random_uniform() * ($#whichfalse+1))
453: + $topcount;
454: }
455: }
456: &Apache::lonxml::debug("Answer now wants $answer");
457: #add the top items to the top, bottom items to the bottom
458: for (my $i=0;$i<=$#toplist;$i++) {
459: if ($toplist[$i]) { unshift(@whichfalse,$toplist[$i]) }
1.49 albertel 460: }
1.83 albertel 461: for (my $i=0;$i<=$#bottomlist;$i++) {
462: if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49 albertel 463: }
1.83 albertel 464: #if the true statement is randomized insert it into the list
465: if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49 albertel 466: }
1.83 albertel 467: &Apache::lonxml::debug("Answer is $answer");
468: return ($answer,@whichfalse);
1.28 albertel 469: }
470:
471: sub displayfoils {
1.90 albertel 472: my ($target,$max,$randomize,$direction)=@_;
1.83 albertel 473: my $result;
1.28 albertel 474:
1.83 albertel 475: my ($answer,@whichfoils)=&whichfoils($max,$randomize);
1.22 albertel 476: my $part=$Apache::inputtags::part;
1.83 albertel 477: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
478: if ( ($target ne 'tex') &&
479: &Apache::response::show_answer() ) {
1.90 albertel 480: if ($direction eq 'horizontal') {
481: if ($target ne 'tex') {
482: $result.='<table><tr>';
483: }
484: }
1.83 albertel 485: foreach my $name (@whichfoils) {
1.90 albertel 486: if ($direction eq 'horizontal') {
487: if ($target ne 'tex') { $result.='<td>'; }
488: }
1.83 albertel 489: if ($target ne 'tex') {
490: $result.="<br />";
491: } else {
492: $result.='\item \vskip -2 mm ';
493: }
494: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
495: if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
496: } else {
497: $result.='Incorrect:';
498: }
1.94 matthew 499: if ($target eq 'web') { $result.="<label>"; }
1.90 albertel 500: $result.=$Apache::response::foilgroup{$name.'.text'};
1.94 matthew 501: if ($target eq 'web') { $result.="</label>"; }
1.83 albertel 502: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
503: if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
504: }
1.90 albertel 505: if ($direction eq 'horizontal') {
506: if ($target ne 'tex') { $result.='</td>'; }
507: }
508: }
509: if ($direction eq 'horizontal') {
510: if ($target ne 'tex') {
511: $result.='</tr></table>';
512: }
1.83 albertel 513: }
514: } else {
515: my @alphabet = ('A'..'Z');
516: my $i = 0;
517: my $temp=0;
518: my $id=$Apache::inputtags::response['-1'];
519: my $part=$Apache::inputtags::part;
520: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
521: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
1.90 albertel 522: if ($target ne 'tex' && $direction eq 'horizontal') {
523: $result.="<table><tr>";
524: }
1.83 albertel 525: foreach my $name (@whichfoils) {
526: if ($target ne 'tex') {
1.90 albertel 527: if ($direction eq 'horizontal') {
528: $result.="<td>";
529: } else {
530: $result.="<br />";
531: }
532: }
533: if ($target ne 'tex') {
1.94 matthew 534: $result.= '<label>';
1.113 ! albertel 535: $result.=
! 536: "<input type=\"radio\"
! 537: onchange=\"javascript:setSubmittedPart('$part');\"
! 538: name=\"HWVAL_$Apache::inputtags::response['-1']\"
! 539: value=\"$temp\" ";
1.83 albertel 540: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108 albertel 541: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83 albertel 542: } else {
543: if ($Apache::lonhomework::type eq 'exam') {
544: $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
545: $i++;
546: } else {
547: $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
548: }
549: }
1.90 albertel 550: if ($target ne 'tex' && $direction eq 'horizontal') {
551: $result.="</td>";
552: }
1.83 albertel 553: $temp++;
554: }
1.90 albertel 555: if ($target ne 'tex' && $direction eq 'horizontal') {
556: $result.="</tr></table>";
557: }
1.83 albertel 558: }
1.92 albertel 559: if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83 albertel 560: return $result;
1.81 albertel 561: }
562:
563: sub displayallanswers {
1.106 albertel 564: my @names;
565: if ( $Apache::response::foilgroup{'names'} ) {
566: @names= @{ $Apache::response::foilgroup{'names'} };
567: }
1.81 albertel 568:
569: my $result=&Apache::response::answer_header('radiobuttonresponse');
570: foreach my $name (@names) {
571: $result.=&Apache::response::answer_part('radiobuttonresponse',
572: $Apache::response::foilgroup{$name.'.value'});
573: }
574: $result.=&Apache::response::answer_footer('radiobuttonresponse');
575: return $result;
1.14 albertel 576: }
577:
1.28 albertel 578: sub displayanswers {
1.83 albertel 579: my ($max,$randomize)=@_;
580: my ($answer,@whichopt) = &whichfoils($max,$randomize);
581: my $result=&Apache::response::answer_header('radiobuttonresponse');
1.105 albertel 582: if ($Apache::lonhomework::type eq 'exam') {
583: my $correct = ('A'..'Z')[$answer];
584: $result.=&Apache::response::answer_part('radiobuttonresponse',
585: $correct);
586: }
1.83 albertel 587: foreach my $name (@whichopt) {
588: $result.=&Apache::response::answer_part('radiobuttonresponse',
1.105 albertel 589: $Apache::response::foilgroup{$name.'.value'});
590: }
1.83 albertel 591: $result.=&Apache::response::answer_footer('radiobuttonresponse');
592: return $result;
1.28 albertel 593: }
594:
1.14 albertel 595: sub start_conceptgroup {
1.83 albertel 596: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
597: $Apache::radiobuttonresponse::conceptgroup=1;
598: %Apache::response::conceptgroup=();
599: my $result;
600: if ($target eq 'edit') {
601: $result.=&Apache::edit::tag_start($target,$token);
602: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
603: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
604: } elsif ($target eq 'modified') {
605: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
606: $safeeval,'concept');
607: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
608: }
609: return $result;
1.14 albertel 610: }
611:
612: sub end_conceptgroup {
1.83 albertel 613: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
614: $Apache::radiobuttonresponse::conceptgroup=0;
615: my $result;
616: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
617: $target eq 'tex' || $target eq 'analyze') {
618: &Apache::response::pick_foil_for_concept($target,
619: ['value','text','location'],
620: \%Apache::hint::radiobutton,
621: $parstack,$safeeval);
622: } elsif ($target eq 'edit') {
623: $result=&Apache::edit::end_table();
624: }
625: return $result;
1.26 albertel 626: }
627:
628: sub insert_conceptgroup {
1.83 albertel 629: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
630: return $result;
1.1 albertel 631: }
632:
633: sub start_foil {
1.83 albertel 634: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
635: my $result='';
636: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
637: &Apache::lonxml::startredirection;
1.95 albertel 638: if ($target eq 'analyze') {
639: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
640: }
1.83 albertel 641: } elsif ($target eq 'edit') {
642: $result=&Apache::edit::tag_start($target,$token);
643: $result.=&Apache::edit::text_arg('Name:','name',$token);
644: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
645: ['unused','true','false'],
646: $token);
647: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
648: $safeeval,'-3');
649: if ($randomize ne 'no') {
650: $result.=&Apache::edit::select_arg('Location:','location',
651: ['random','top','bottom'],$token);
652: }
653: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
654: } elsif ($target eq 'modified') {
655: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
656: $safeeval,'value','name',
657: 'location');
658: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
659: }
660: return $result;
1.1 albertel 661: }
662:
663: sub end_foil {
1.83 albertel 664: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
665: my $text='';
666: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
667: $text=&Apache::lonxml::endredirection;
668: }
1.85 albertel 669: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
670: || $target eq 'tex' || $target eq 'analyze') {
1.83 albertel 671: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
672: if ($value ne 'unused') {
673: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102 albertel 674: if ($name eq "") {
1.101 albertel 675: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.98 albertel 676: $name=$Apache::lonxml::curdepth;
677: }
1.85 albertel 678: if (defined($Apache::response::foilnames{$name})) {
679: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
680: }
1.86 albertel 681: $Apache::response::foilnames{$name}++;
1.85 albertel 682: my $location =&Apache::lonxml::get_param('location',$parstack,
683: $safeeval);
1.83 albertel 684: if ( $Apache::radiobuttonresponse::conceptgroup
685: && !&Apache::response::showallfoils() ) {
686: push @{ $Apache::response::conceptgroup{'names'} }, $name;
687: $Apache::response::conceptgroup{"$name.value"} = $value;
688: $Apache::response::conceptgroup{"$name.text"} = $text;
689: $Apache::response::conceptgroup{"$name.location"} = $location;
690: } else {
691: push @{ $Apache::response::foilgroup{'names'} }, $name;
692: $Apache::response::foilgroup{"$name.value"} = $value;
693: $Apache::response::foilgroup{"$name.text"} = $text;
694: $Apache::response::foilgroup{"$name.location"} = $location;
695: }
696: }
1.18 albertel 697: }
1.83 albertel 698: return '';
1.1 albertel 699: }
700:
1.27 albertel 701: sub insert_foil {
1.83 albertel 702: return '
1.27 albertel 703: <foil name="" value="unused">
704: <startouttext />
705: <endouttext />
706: </foil>';
707: }
1.1 albertel 708: 1;
709: __END__
710:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>