Annotation of loncom/homework/radiobuttonresponse.pm, revision 1.125
1.22 albertel 1: # The LearningOnline Network with CAPA
2: # mutliple choice style responses
1.31 albertel 3: #
1.125 ! albertel 4: # $Id: radiobuttonresponse.pm,v 1.124 2007/07/17 20:44:07 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.115 foxr 34: use Apache::response;
1.1 albertel 35:
1.120 foxr 36: my $default_bubbles_per_line = 10;
1.121 foxr 37:
1.116 foxr 38:
1.36 harris41 39: BEGIN {
1.83 albertel 40: &Apache::lonxml::register('Apache::radiobuttonresponse',('radiobuttonresponse'));
1.1 albertel 41: }
42:
1.121 foxr 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:
1.1 albertel 55: sub start_radiobuttonresponse {
1.83 albertel 56: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
57: my $result;
1.115 foxr 58:
1.120 foxr 59:
60:
1.83 albertel 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);
1.120 foxr 65:
1.83 albertel 66: %Apache::hint::radiobutton=();
1.85 albertel 67: undef(%Apache::response::foilnames);
1.83 albertel 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).
1.103 albertel 83: &Apache::edit::select_arg('Display Direction','direction',
84: ['vertical','horizontal'],$token).
1.83 albertel 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',
1.103 albertel 90: 'randomize','direction');
1.83 albertel 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}.}';
1.88 albertel 103: } else {
104: $result .= ' \renewcommand{\labelenumi}{\Alph{enumi}.}';
1.83 albertel 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;
1.1 albertel 112: }
113:
114: sub end_radiobuttonresponse {
1.83 albertel 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'));
1.85 albertel 122: undef(%Apache::response::foilnames);
1.83 albertel 123: return $result;
1.1 albertel 124: }
125:
1.43 albertel 126: %Apache::response::foilgroup=();
1.1 albertel 127: sub start_foilgroup {
1.83 albertel 128: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
129: my $result;
130: %Apache::response::foilgroup=();
131: $Apache::radiobuttonresponse::conceptgroup=0;
1.89 albertel 132: &Apache::response::pushrandomnumber();
1.83 albertel 133: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
134: $result.='\item[\textbf{'.$Apache::lonxml::counter.'}.]';
135: }
136: return $result;
1.5 albertel 137: }
138:
1.15 albertel 139: sub storesurvey {
1.99 albertel 140: if ( !&Apache::response::submitted() ) { return ''; }
1.100 albertel 141: my $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83 albertel 142: &Apache::lonxml::debug("Here I am!:$response:");
143: if ( $response !~ /[0-9]+/) { return ''; }
1.96 albertel 144: my $part = $Apache::inputtags::part;
1.83 albertel 145: my $id = $Apache::inputtags::response['-1'];
146: my @whichfoils=@{ $Apache::response::foilgroup{'names'} };
147: my %responsehash;
148: $responsehash{$whichfoils[$response]}=$response;
1.96 albertel 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);
1.83 albertel 155: &Apache::lonxml::debug("submitted a $response<br />\n");
156: return '';
1.15 albertel 157: }
158:
1.120 foxr 159:
1.32 albertel 160: sub grade_response {
1.123 albertel 161: my ($answer, $whichfoils, $bubbles_per_line)=@_;
162:
1.99 albertel 163: if ( !&Apache::response::submitted() ) { return; }
1.83 albertel 164: my $response;
1.118 foxr 165:
1.100 albertel 166: if ($env{'form.submitted'} eq 'scantron') {
1.121 foxr 167: $response = &Apache::response::getresponse(1,undef,
1.123 albertel 168: &bubble_line_count(scalar(@{ $whichfoils}),
1.121 foxr 169: $bubbles_per_line),
170: $bubbles_per_line);
1.116 foxr 171:
1.83 albertel 172: } else {
1.100 albertel 173: $response = $env{'form.HWVAL_'.$Apache::inputtags::response['-1']};
1.83 albertel 174: }
1.120 foxr 175:
1.118 foxr 176:
1.83 albertel 177: if ( $response !~ /[0-9]+/) { return; }
178: my $part=$Apache::inputtags::part;
179: my $id = $Apache::inputtags::response['-1'];
180: my %responsehash;
1.123 albertel 181: $responsehash{$whichfoils->[$response]}=$response;
1.83 albertel 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);
1.32 albertel 196: }
197:
1.1 albertel 198: sub end_foilgroup {
1.83 albertel 199: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.29 albertel 200:
1.83 albertel 201: my $result;
1.121 foxr 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:
1.83 albertel 212: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
213: $target eq 'tex' || $target eq 'analyze') {
214: my $style = $Apache::lonhomework::type;
1.93 albertel 215: my $direction = &Apache::lonxml::get_param('direction',$parstack,
216: $safeeval,'-2');
1.83 albertel 217: if ( $style eq 'survey' && $target ne 'analyze') {
218: if ($target eq 'web' || $target eq 'tex') {
1.110 foxr 219: $result=&displayallfoils($direction, $target);
1.83 albertel 220: } elsif ( $target eq 'answer' ) {
221: $result=&displayallanswers();
222: } elsif ( $target eq 'grade' ) {
223: $result=&storesurvey();
224: }
1.121 foxr 225: $answer_count = scalar(@{$Apache::response::foilgroup{'names'}});
226:
1.83 albertel 227: } else {
1.121 foxr 228:
1.83 albertel 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');
1.123 albertel 234: my ($answer, @shown) = &whichfoils($max, $randomize);
1.121 foxr 235: $answer_count = scalar(@shown);
236:
1.83 albertel 237: if ($target eq 'web' || $target eq 'tex') {
1.121 foxr 238: $result=&displayfoils($target,
1.123 albertel 239: $answer, \@shown,
1.121 foxr 240: $direction,
241: $bubbles_per_line);
1.83 albertel 242: } elsif ($target eq 'answer' ) {
1.124 albertel 243: $result=&displayanswers($answer, \@shown, $bubbles_per_line);
1.83 albertel 244: } elsif ( $target eq 'grade') {
1.123 albertel 245: &grade_response($answer, \@shown, $bubbles_per_line);
1.83 albertel 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: }
1.81 albertel 253: }
1.111 albertel 254: $Apache::lonxml::post_evaluate=0;
1.83 albertel 255: }
1.114 albertel 256: if ($target eq 'web') {
257: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
258: [\%Apache::response::foilgroup]);
259: }
1.121 foxr 260: $bubble_lines = &bubble_line_count($answer_count, $bubbles_per_line);
1.89 albertel 261: &Apache::response::poprandomnumber();
1.120 foxr 262: &Apache::lonxml::increment_counter($bubble_lines);
1.83 albertel 263: return $result;
1.6 albertel 264: }
265:
266: sub getfoilcounts {
1.83 albertel 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'} };
1.6 albertel 273: }
1.83 albertel 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);
1.5 albertel 282: }
283:
1.114 albertel 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:
1.112 albertel 292: }
293:
1.15 albertel 294: sub displayallfoils {
1.110 foxr 295: my ($direction, $target)=@_;
1.83 albertel 296: my $result;
297: &Apache::lonxml::debug("survey style display");
1.106 albertel 298: my @names;
299: if ( $Apache::response::foilgroup{'names'} ) {
300: @names= @{ $Apache::response::foilgroup{'names'} };
301: }
1.120 foxr 302:
1.83 albertel 303: my $temp=0;
1.110 foxr 304: my $i =0;
1.83 albertel 305: my $id=$Apache::inputtags::response['-1'];
306: my $part=$Apache::inputtags::part;
307: my $lastresponse=
308: $Apache::lonhomework::history{"resource.$part.$id.submission"};
1.93 albertel 309: if ($direction eq 'horizontal') { $result.='<table><tr>'; }
1.83 albertel 310: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
311: if (&Apache::response::show_answer() ) {
312: foreach my $name (@names) {
313: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.110 foxr 314: if (($direction eq 'horizontal') && ($target ne 'tex')) {
1.93 albertel 315: $result.="<td>";
316: } else {
1.110 foxr 317: if ($target eq 'tex') {
318: $result .= '\item \vskip -2mm ';
319: } else {
320: $result.="<br />";
321: }
1.93 albertel 322: }
1.84 albertel 323: if (defined($lastresponse{$name})) {
1.110 foxr 324: if ($target eq 'tex') {
325: $result .= '}';
326: } else {
327: $result.='<b>';
328: }
1.83 albertel 329: }
330: $result .= $Apache::response::foilgroup{$name.'.text'};
1.110 foxr 331: if (defined($lastresponse{$name}) && ($target ne 'tex')) {
1.83 albertel 332: $result.='</b>';
333: }
1.110 foxr 334: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
1.83 albertel 335: }
1.45 albertel 336: }
1.83 albertel 337: } else {
338: foreach my $name (@names) {
339: if ($Apache::response::foilgroup{$name.'.value'} ne 'unused') {
1.93 albertel 340: if ($direction eq 'horizontal') {
341: $result.="<td>";
342: } else {
1.110 foxr 343: if ($target eq 'tex') {
344: $result .= '\item \vskip -2mm ';
345: } else {
346: $result.="<br />";
347: }
348: }
349: if ($target eq 'tex') {
350: $result .= '$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
351: $i++;
352: } else {
353: $result .= '<label>';
1.113 albertel 354: $result.="<input
355: onchange=\"javascript:setSubmittedPart('$part');\"
356: type=\"radio\"
357: name=\"HWVAL_$Apache::inputtags::response['-1']\"
358: value=\"$temp\" ";
1.110 foxr 359: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
360: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}.
361: '</label>';
1.93 albertel 362: }
1.83 albertel 363: $temp++;
1.110 foxr 364: if ($target ne 'tex') {
365: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.="</td>"; }
366: } else {
367: $result.='\vskip 0 mm ';
368: }
1.83 albertel 369: }
1.45 albertel 370: }
371: }
1.120 foxr 372:
1.110 foxr 373: if (($direction eq 'horizontal') && ($target ne 'tex')) { $result.='</tr></table>'; }
1.83 albertel 374: return $result;
1.15 albertel 375: }
376:
1.122 albertel 377: =pod
378:
379: =item &whichfoils($max,$randomize)
380:
381: Randomizes the list of foils.
382: Respects
383: - each foils desire to be randomized
384: - the existance of Concept groups of foils (select 1 foil from each)
385: - and selects a single correct statement from all possilble true statments
386: - and limits it to a toal of $max foils
387:
1.123 albertel 388: WARNING: this routine uses the random number generator, it should only
389: be called once per target, otherwise it can cause randomness changes in
390: homework problems.
391:
1.122 albertel 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:
1.28 albertel 405: sub whichfoils {
1.83 albertel 406: my ($max,$randomize)=@_;
1.28 albertel 407:
1.83 albertel 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
1.104 albertel 414: if ( $max>0 && ($falsecnt+1)>$max) { $count=$max } else { $count=$falsecnt+1; $max=$count; }
1.83 albertel 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 {
1.87 albertel 435: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83 albertel 436: }
437: }
1.97 albertel 438: if (!$havetrue && $Apache::lonhomework::type ne 'survey') {
439: &Apache::lonxml::error("There are no true statements available.<br />");
440: }
1.83 albertel 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 {
1.87 albertel 464: &Apache::lonxml::error(&HTML::Entities::encode("No valid value assigned ($Apache::response::foilgroup{$name.'.value'}) for foil $name in <foilgroup>",'<>&"'));
1.83 albertel 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; }
1.49 albertel 509: }
1.83 albertel 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; }
1.49 albertel 517: }
1.83 albertel 518: $answer+=$topcount+$#whichfalse+1;
519: $dosplice=0;
1.49 albertel 520: } else {
1.83 albertel 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]) }
1.49 albertel 530: }
1.83 albertel 531: for (my $i=0;$i<=$#bottomlist;$i++) {
532: if ($bottomlist[$i]) { push(@whichfalse,$bottomlist[$i]) }
1.49 albertel 533: }
1.83 albertel 534: #if the true statement is randomized insert it into the list
535: if ($dosplice) { splice(@whichfalse,$answer,0,$truelist[$whichtrue]); }
1.49 albertel 536: }
1.83 albertel 537: &Apache::lonxml::debug("Answer is $answer");
538: return ($answer,@whichfalse);
1.28 albertel 539: }
540:
541: sub displayfoils {
1.123 albertel 542: my ($target,$answer,$whichfoils,$direction, $bubbles_per_line)=@_;
1.83 albertel 543: my $result;
1.28 albertel 544:
1.22 albertel 545: my $part=$Apache::inputtags::part;
1.83 albertel 546: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
547: if ( ($target ne 'tex') &&
548: &Apache::response::show_answer() ) {
1.90 albertel 549: if ($direction eq 'horizontal') {
550: if ($target ne 'tex') {
551: $result.='<table><tr>';
552: }
553: }
1.123 albertel 554: foreach my $name (@{ $whichfoils }) {
1.90 albertel 555: if ($direction eq 'horizontal') {
556: if ($target ne 'tex') { $result.='<td>'; }
557: }
1.83 albertel 558: if ($target ne 'tex') {
559: $result.="<br />";
560: } else {
561: $result.='\item \vskip -2 mm ';
562: }
563: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
564: if ($target ne 'tex') { $result.='Correct:<b>'; } else { $result.='Correct: \textbf{';}
565: } else {
566: $result.='Incorrect:';
567: }
1.94 matthew 568: if ($target eq 'web') { $result.="<label>"; }
1.90 albertel 569: $result.=$Apache::response::foilgroup{$name.'.text'};
1.94 matthew 570: if ($target eq 'web') { $result.="</label>"; }
1.83 albertel 571: if ($Apache::response::foilgroup{$name.'.value'} eq 'true') {
572: if ($target ne 'tex') { $result.='</b>';} else {$result.='}';}
573: }
1.90 albertel 574: if ($direction eq 'horizontal') {
575: if ($target ne 'tex') { $result.='</td>'; }
576: }
577: }
578: if ($direction eq 'horizontal') {
579: if ($target ne 'tex') {
580: $result.='</tr></table>';
581: }
1.83 albertel 582: }
583: } else {
584: my @alphabet = ('A'..'Z');
585: my $i = 0;
1.116 foxr 586: my $bubble_number = 0;
1.124 albertel 587: my $line = 0;
1.83 albertel 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);
1.90 albertel 593: if ($target ne 'tex' && $direction eq 'horizontal') {
594: $result.="<table><tr>";
595: }
1.123 albertel 596: foreach my $name (@{ $whichfoils }) {
1.83 albertel 597: if ($target ne 'tex') {
1.90 albertel 598: if ($direction eq 'horizontal') {
599: $result.="<td>";
600: } else {
601: $result.="<br />";
602: }
603: }
604: if ($target ne 'tex') {
1.94 matthew 605: $result.= '<label>';
1.113 albertel 606: $result.=
607: "<input type=\"radio\"
608: onchange=\"javascript:setSubmittedPart('$part');\"
609: name=\"HWVAL_$Apache::inputtags::response['-1']\"
610: value=\"$temp\" ";
1.83 albertel 611: if (defined($lastresponse{$name})) { $result .= 'checked="on"'; }
1.108 albertel 612: $result .= ' />'.$Apache::response::foilgroup{$name.'.text'}."</label>";
1.83 albertel 613: } else {
614: if ($Apache::lonhomework::type eq 'exam') {
1.116 foxr 615: if($bubble_number >= $bubbles_per_line) {
1.124 albertel 616: $line++;
1.116 foxr 617: $i = 0;
618: $bubble_number = 0;
1.124 albertel 619: $result.='\item[\textbf{'.($Apache::lonxml::counter+$line).'}.]';
1.116 foxr 620: }
1.124 albertel 621: $result .= '{\small \textbf{'.$alphabet[$i].'}}$\bigcirc$'.$Apache::response::foilgroup{$name.'.text'}.'\\\\'; #' stupid emacs
622: $i++;
623: $bubble_number++;
1.83 albertel 624: } else {
625: $result .= '\vspace*{-2 mm}\item '.$Apache::response::foilgroup{$name.'.text'};
626: }
627: }
1.90 albertel 628: if ($target ne 'tex' && $direction eq 'horizontal') {
629: $result.="</td>";
630: }
1.83 albertel 631: $temp++;
632: }
1.90 albertel 633: if ($target ne 'tex' && $direction eq 'horizontal') {
634: $result.="</tr></table>";
635: }
1.83 albertel 636: }
1.92 albertel 637: if ($target ne 'tex') { if ($direction ne 'horizontal') { $result.="<br />";} } else { $result.='\vskip 0 mm '; }
1.83 albertel 638: return $result;
1.81 albertel 639: }
640:
641: sub displayallanswers {
1.106 albertel 642: my @names;
643: if ( $Apache::response::foilgroup{'names'} ) {
644: @names= @{ $Apache::response::foilgroup{'names'} };
645: }
1.81 albertel 646: my $result=&Apache::response::answer_header('radiobuttonresponse');
647: foreach my $name (@names) {
648: $result.=&Apache::response::answer_part('radiobuttonresponse',
649: $Apache::response::foilgroup{$name.'.value'});
650: }
651: $result.=&Apache::response::answer_footer('radiobuttonresponse');
652: return $result;
1.14 albertel 653: }
654:
1.28 albertel 655: sub displayanswers {
1.124 albertel 656: my ($answer, $whichopt, $bubbles_per_line)=@_;
657: my $result;
658:
1.105 albertel 659: if ($Apache::lonhomework::type eq 'exam') {
1.124 albertel 660: my $line = int($answer/$bubbles_per_line);
661: my $correct = ('A'..'Z')[$answer%$bubbles_per_line];
662: $result .= &Apache::response::answer_header('radiobuttonresponse',
663: $line);
664: $result .= &Apache::response::answer_part('radiobuttonresponse',
665: $correct);
666: } else {
667: $result .= &Apache::response::answer_header('radiobuttonresponse');
1.105 albertel 668: }
1.123 albertel 669: foreach my $name (@{ $whichopt }) {
1.83 albertel 670: $result.=&Apache::response::answer_part('radiobuttonresponse',
1.105 albertel 671: $Apache::response::foilgroup{$name.'.value'});
672: }
1.83 albertel 673: $result.=&Apache::response::answer_footer('radiobuttonresponse');
674: return $result;
1.28 albertel 675: }
676:
1.14 albertel 677: sub start_conceptgroup {
1.83 albertel 678: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
679: $Apache::radiobuttonresponse::conceptgroup=1;
680: %Apache::response::conceptgroup=();
681: my $result;
682: if ($target eq 'edit') {
683: $result.=&Apache::edit::tag_start($target,$token);
684: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
685: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
686: } elsif ($target eq 'modified') {
687: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
688: $safeeval,'concept');
689: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
690: }
691: return $result;
1.14 albertel 692: }
693:
694: sub end_conceptgroup {
1.83 albertel 695: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
696: $Apache::radiobuttonresponse::conceptgroup=0;
697: my $result;
698: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
699: $target eq 'tex' || $target eq 'analyze') {
700: &Apache::response::pick_foil_for_concept($target,
701: ['value','text','location'],
702: \%Apache::hint::radiobutton,
703: $parstack,$safeeval);
704: } elsif ($target eq 'edit') {
705: $result=&Apache::edit::end_table();
706: }
707: return $result;
1.26 albertel 708: }
709:
710: sub insert_conceptgroup {
1.83 albertel 711: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
712: return $result;
1.1 albertel 713: }
714:
715: sub start_foil {
1.83 albertel 716: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
717: my $result='';
718: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
719: &Apache::lonxml::startredirection;
1.95 albertel 720: if ($target eq 'analyze') {
721: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
722: }
1.83 albertel 723: } elsif ($target eq 'edit') {
724: $result=&Apache::edit::tag_start($target,$token);
725: $result.=&Apache::edit::text_arg('Name:','name',$token);
726: $result.=&Apache::edit::select_or_text_arg('Correct Option:','value',
727: ['unused','true','false'],
728: $token);
729: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
730: $safeeval,'-3');
731: if ($randomize ne 'no') {
732: $result.=&Apache::edit::select_arg('Location:','location',
733: ['random','top','bottom'],$token);
734: }
735: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
736: } elsif ($target eq 'modified') {
737: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
738: $safeeval,'value','name',
739: 'location');
740: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
741: }
742: return $result;
1.1 albertel 743: }
744:
745: sub end_foil {
1.83 albertel 746: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
747: my $text='';
748: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
749: $text=&Apache::lonxml::endredirection;
750: }
1.85 albertel 751: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer'
752: || $target eq 'tex' || $target eq 'analyze') {
1.83 albertel 753: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
754: if ($value ne 'unused') {
755: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.102 albertel 756: if ($name eq "") {
1.101 albertel 757: &Apache::lonxml::warning("Foils without names exist. This can cause problems to malfunction.");
1.98 albertel 758: $name=$Apache::lonxml::curdepth;
759: }
1.85 albertel 760: if (defined($Apache::response::foilnames{$name})) {
761: &Apache::lonxml::error(&mt("Foil name <b><tt>[_1]</tt></b> appears more than once. Foil names need to be unique.",$name));
762: }
1.86 albertel 763: $Apache::response::foilnames{$name}++;
1.85 albertel 764: my $location =&Apache::lonxml::get_param('location',$parstack,
765: $safeeval);
1.83 albertel 766: if ( $Apache::radiobuttonresponse::conceptgroup
767: && !&Apache::response::showallfoils() ) {
768: push @{ $Apache::response::conceptgroup{'names'} }, $name;
769: $Apache::response::conceptgroup{"$name.value"} = $value;
770: $Apache::response::conceptgroup{"$name.text"} = $text;
771: $Apache::response::conceptgroup{"$name.location"} = $location;
772: } else {
773: push @{ $Apache::response::foilgroup{'names'} }, $name;
774: $Apache::response::foilgroup{"$name.value"} = $value;
775: $Apache::response::foilgroup{"$name.text"} = $text;
776: $Apache::response::foilgroup{"$name.location"} = $location;
777: }
778: }
1.18 albertel 779: }
1.83 albertel 780: return '';
1.1 albertel 781: }
782:
1.27 albertel 783: sub insert_foil {
1.83 albertel 784: return '
1.27 albertel 785: <foil name="" value="unused">
786: <startouttext />
787: <endouttext />
788: </foil>';
789: }
1.1 albertel 790: 1;
791: __END__
792:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>