File:
[LON-CAPA] /
loncom /
homework /
matchresponse.pm
Revision
1.83:
download - view:
text,
annotated -
select for diffs
Fri Oct 7 22:39:59 2011 UTC (12 years, 11 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Bug 2802.
Move determination of lenient vs non-lenient to subroutine to facilitate reuse
.
- Support lenient parameter for matchresponse as well as optionresponse.
- Use response::get_param_response() to retrieve lenient parm.
1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
4: # $Id: matchresponse.pm,v 1.83 2011/10/07 22:39:59 raeburn 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::matchresponse;
30: use strict;
31: use HTML::Entities();
32: use Math::Random();
33: use Apache::optionresponse();
34: use Apache::lonlocal;
35: use Apache::lonnet;
36: use Apache::lonxml;
37:
38: BEGIN {
39: &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
40: }
41:
42: sub start_matchresponse {
43: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
44: my $result;
45: #when in a matchresponse use these
46: &Apache::lonxml::register('Apache::matchresponse',
47: ('foilgroup','foil','conceptgroup','item',
48: 'itemgroup'));
49: push (@Apache::lonxml::namespace,'matchresponse');
50: my $id = &Apache::response::start_response($parstack,$safeeval);
51: %Apache::hint::match=();
52: undef(%Apache::response::foilnames);
53: if ($target eq 'meta') {
54: $result=&Apache::response::meta_package_write('matchresponse');
55: } elsif ($target eq 'edit' ) {
56: $result.=&Apache::edit::start_table($token).
57: '<tr><td>'.&Apache::lonxml::description($token).'</td>'
58: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
59: .&Apache::edit::deletelist($target,$token)
60: .'</span></td>'
61: .'<td> '.&Apache::edit::end_row()
62: .&Apache::edit::start_spanning_row();
63:
64: $result.=
65: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
66: &Apache::edit::select_arg('Randomize Foil Order:','randomize',
67: ['yes','no'],$token).
68: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
69: } elsif ($target eq 'modified') {
70: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
71: $safeeval,'max',
72: 'randomize');
73: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
74: } elsif ($target eq 'analyze') {
75: my $part_id="$Apache::inputtags::part.$id";
76: $Apache::lonhomework::analyze{"$part_id.type"} = 'matchresponse';
77: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
78: push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
79: 1);
80: }
81: return $result;
82: }
83:
84: sub end_matchresponse {
85: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
86: my $result;
87: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
88: &Apache::response::end_response;
89: pop @Apache::lonxml::namespace;
90: &Apache::lonxml::deregister('Apache::matchresponse',
91: ('foilgroup','foil','conceptgroup'));
92: undef(%Apache::response::foilnames);
93: return $result;
94: }
95:
96: %Apache::response::itemgroup=();
97: sub start_itemgroup {
98: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
99: my $result;
100: %Apache::response::itemgroup=();
101: %Apache::matchresponse::itemtable=();
102:
103: if ($target eq 'edit') {
104: $result=&Apache::edit::tag_start($target,$token);
105: $result.=&Apache::edit::select_arg('Randomize Order:','randomize',
106: ['yes','no'],$token).' 'x 3;
107: $result.=&Apache::edit::select_arg('Items Display Location:',
108: 'location',
109: ['top','bottom','left','right'],
110: $token).' 'x 3;
111: $result.=&Apache::edit::select_arg('Items Display Direction:',
112: 'direction',
113: ['vertical','horizontal'],
114: $token);
115: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
116: } elsif ($target eq 'modified') {
117: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
118: $safeeval,'randomize',
119: 'location','direction');
120: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
121: } elsif ($target eq 'web' or $target eq 'tex') {
122: $Apache::matchresponse::itemtable{'location'}=
123: &Apache::lonxml::get_param('location',$parstack,$safeeval);
124: $Apache::matchresponse::TeXitemgroupwidth=&Apache::lonxml::get_param('TeXitemgroupwidth',$parstack,$safeeval,undef,0);
125: }
126: return $result;
127: }
128:
129: sub end_itemgroup {
130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
131: my $result;
132:
133: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
134: if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
135: my @names=@{ $Apache::response::itemgroup{'names'} };
136: my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
137: if ($randomize ne 'no' ) {
138: @names=&Apache::response::whichorder($#names+1,$randomize,0,
139: \%Apache::response::itemgroup);
140: }
141: if ($target eq 'analyze') {
142: my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
143: push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
144: }
145: my %letter_name_map;
146: my %name_letter_map;
147: my @alphabet=('A'..'Z');
148: my $i=0;
149: foreach my $name (@names) {
150: $letter_name_map{$alphabet[$i]}=$name;
151: $name_letter_map{$name}=$alphabet[$i];
152: $i++;
153: }
154: $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
155: $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
156: my $direction=&Apache::lonxml::get_param('direction',$parstack,$safeeval);
157: if ($target eq 'web') {
158:
159: my $table='<br /><table>'; # extra space to match what latex does.
160: my $i=0;
161: if ($direction eq 'horizontal') { $table .='<tr>';}
162: foreach my $name (@names) {
163: if ($direction ne 'horizontal') { $table.='<tr>'; }
164: $table.='<td>'.$alphabet[$i].'</td><td>'.
165: $Apache::response::itemgroup{$name.'.text'}.'</td>';
166: if ($direction ne 'horizontal') { $table.='</tr>'; }
167: $i++;
168: }
169: if ($direction eq 'horizontal') { $table .='</tr>';}
170: $table.='</table>';
171: $Apache::matchresponse::itemtable{'display'}=$table;
172: $Apache::lonxml::post_evaluate=0;
173: } elsif ($target eq 'tex') {
174: my $table=' \begin{description}\setlength{\leftmargin}{2em}\setlength{\labelwidth}{1em}\setlength{\itemsep}{0.5pt plus1pt minus2pt}\setlength{\listparindent}{0em} ';
175: my $i=0;
176: my ($numrows,$bubbles_per_row);
177: if ($Apache::lonhomework::type eq 'exam') {
178: ($numrows,$bubbles_per_row) =
179: &Apache::optionresponse::getnumrows(scalar(@names));
180: } else {
181: $numrows = 1;
182: }
183: foreach my $name (@names) {
184: # $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
185: my $item;
186: if (($numrows > 1) && ($bubbles_per_row > 0)) {
187: my $num = 1+int($i/$bubbles_per_row);
188: my $idx = int($i % $bubbles_per_row);
189: if ($idx == 0) {
190: if ($num == 1) {
191: $table .= '\item[\footnotesize {'.&mt('(first line)').'}]';
192: } else {
193: $table .= '\item[\footnotesize {'.&mt('(next line)').'}]';
194: }
195: }
196: $item = $alphabet[$idx];
197: } else {
198: $item = $alphabet[$i];
199: }
200: $table.='\item['.$item.'] '.
201: $Apache::response::itemgroup{$name.'.text'};
202: $i++;
203: }
204: $table.=' \end{description} \strut ';
205: if ($Apache::lonhomework::type eq 'exam') {$table.='\vskip -13 mm \strut ';}
206: $Apache::matchresponse::itemtable{'display'}=$table;
207: $Apache::lonxml::post_evaluate=0;
208: }
209: return $result;
210: }
211:
212: sub start_item {
213: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
214: my $result='';
215: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
216: &Apache::lonxml::startredirection;
217: } elsif ($target eq 'edit') {
218: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
219: $safeeval,'-2');
220: $result=&Apache::edit::tag_start($target,$token,"Item");
221: $result.=&Apache::edit::text_arg('Name:','name',$token);
222: if ($randomize ne 'no') {
223: $result.=' 'x 3 .
224: &Apache::edit::select_arg('Location:','location',
225: ['random','top','bottom'],
226: $token);
227: }
228: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
229: } elsif ($target eq 'modified') {
230: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
231: $safeeval,'name',
232: 'location');
233: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
234: }
235: return $result;
236: }
237:
238: sub end_item {
239: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
240: my $text ='';
241: my $result = '';
242: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
243: $text=&Apache::lonxml::endredirection;
244: }
245: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
246: $target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
247: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
248: my $location=&Apache::lonxml::get_param('location',$parstack,
249: $safeeval);
250: &Apache::lonxml::debug("Got a name of :$name:");
251: if ($name eq "") { $name=$Apache::lonxml::curdepth; }
252: &Apache::lonxml::debug("Using a name of :$name:");
253: push @{ $Apache::response::itemgroup{'names'} }, $name;
254: $Apache::response::itemgroup{"$name.text"} = $text;
255: $Apache::response::itemgroup{"$name.location"} = $location;
256: }
257: if ($target eq 'edit') {
258: $result.= &Apache::edit::tag_end($target,$token,'');
259: }
260: return $result;
261: }
262:
263: sub insert_item {
264: return '
265: <item name="">
266: <startouttext />
267: <endouttext />
268: </item>';
269: }
270:
271: %Apache::response::foilgroup=();
272: sub start_foilgroup {
273: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
274: my $result;
275: %Apache::response::foilgroup=();
276: $Apache::matchresponse::conceptgroup=0;
277: &Apache::response::pushrandomnumber(undef,$target);
278: if ($target eq 'edit') {
279: $result.=&Apache::edit::start_table($token)
280: .'<tr><td>'.&mt('Collection Of Foils').'</td>'
281: .'<td><span class="LC_nobreak">'.&mt('Delete?')
282: .&Apache::edit::deletelist($target,$token)
283: .'</span></td>'
284: .'<td> '.&Apache::edit::end_row()
285: .&Apache::edit::start_spanning_row()."\n";
286: }
287: return $result;
288: }
289:
290: sub end_foilgroup {
291: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
292: my $result;
293: my $part = $Apache::inputtags::part;
294: my $response_id = $Apache::inputtags::response[-1];
295:
296: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
297: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
298: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
299: $safeeval,'-2');
300: if ($target eq 'web' || $target eq 'tex') {
301: $result=&displayfoils($target,$max,$randomize);
302: $Apache::lonxml::post_evaluate=0;
303: } elsif ($target eq 'answer' ) {
304: $result=&displayanswers($max,$randomize);
305: } elsif ( $target eq 'grade') {
306: &grade_response($max,$randomize);
307: } elsif ( $target eq 'analyze') {
308: my @shown=&whichfoils($max,$randomize);
309: &Apache::response::analyze_store_foilgroup(\@shown,
310: ['text','value','location']);
311: #FIXME need to store options in some way
312: }
313: my ($numrows,$bubbles_per_row);
314: if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
315: my $numitems;
316: if (ref($Apache::response::itemgroup{'names'}) eq 'ARRAY') {
317: $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
318: ($numrows,$bubbles_per_row) =
319: &Apache::optionresponse::getnumrows($numitems);
320: }
321: }
322: if ($numrows < 1) {
323: $numrows = 1;
324: }
325: my $increment = &getfoilcounts($max) * $numrows;
326: &Apache::lonxml::increment_counter($increment,"$part.$response_id");
327: if ($target eq 'analyze') {
328: &Apache::lonhomework::set_bubble_lines();
329: }
330:
331: } elsif ($target eq 'edit') {
332: $result=&Apache::edit::end_table();
333: }
334: &Apache::response::poprandomnumber();
335: return $result;
336: }
337:
338: sub whichfoils {
339: my ($max,$randomize)=@_;
340: return &Apache::response::whichorder(&getfoilcounts($max),
341: $randomize,
342: &Apache::response::showallfoils(),
343: \%Apache::response::foilgroup);
344: }
345:
346: sub displayanswers {
347: my ($max,$randomize,@opt)=@_;
348: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
349: my @names = @{ $Apache::response::foilgroup{'names'} };
350: my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
351: $randomize,
352: &Apache::response::showallfoils(),
353: \%Apache::response::foilgroup);
354: my %name_letter_map;
355: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
356: %name_letter_map=
357: %{ $Apache::response::itemgroup{'name_letter_map'} };
358: }
359: my $result;
360: if ($Apache::lonhomework::type eq 'exam') {
361: my $i=0;
362: foreach my $name (@whichfoils) {
363: $result.=&Apache::response::answer_header('matchresponse',$i++);
364: my $value_name=$Apache::response::foilgroup{$name.'.value'};
365: my $letter=$name_letter_map{$value_name};
366: $result.=&Apache::response::answer_part('matchresponse',$letter);
367: $result.=&Apache::response::answer_footer('matchresponse');
368: }
369: } else {
370: $result=&Apache::response::answer_header('matchresponse');
371: foreach my $name (@whichfoils) {
372: my $value_name=$Apache::response::foilgroup{$name.'.value'};
373: my $letter=$name_letter_map{$value_name};
374: $result.=&Apache::response::answer_part('matchresponse',$letter);
375: }
376: $result.=&Apache::response::answer_footer('matchresponse');
377: }
378: return $result;
379: }
380:
381:
382: sub grade_response {
383: my ($max,$randomize)=@_;
384: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
385: $randomize,
386: &Apache::response::showallfoils(),
387: \%Apache::response::foilgroup);
388: if (!&Apache::response::submitted()) { return; }
389: my %responsehash;
390: my %grade;
391: my %answerhash;
392: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
393: my %letter_name_map;
394: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
395: %letter_name_map=
396: %{ $Apache::response::itemgroup{'letter_name_map'} };
397: }
398: my @items;
399: my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
400: my ($numrows,$bubbles_per_row);
401: if ($Apache::lonhomework::scantronmode) {
402: my $numitems = scalar(@{ $Apache::response::itemgroup{'names'} });
403: ($numrows,$bubbles_per_row) =
404: &Apache::optionresponse::getnumrows($numitems);
405: }
406: if ($numrows < 1) {
407: $numrows = 1;
408: }
409: my @alphabet=('A'..'Z');
410: my %nums_from_letters;
411: for (my $i=0; $i<@alphabet; $i++) {
412: $nums_from_letters{$alphabet[$i]} = $i;
413: }
414: foreach my $name (@whichfoils) {
415: my $response;
416: if ($numrows > 1) {
417: my $num = $temp;
418: my $totalnum;
419: for (my $i=0; $i<$numrows; $i++) {
420: my $item = &Apache::response::getresponse($num,'letter');
421: if ($item =~ /^\w$/) {
422: $totalnum = $i*$bubbles_per_row + $nums_from_letters{$item};
423: }
424: $num ++;
425: }
426: if ($totalnum =~ /^\d+$/) {
427: $response = $alphabet[$totalnum];
428: }
429: $temp += $numrows;
430: } else {
431: $response = &Apache::response::getresponse($temp,'letter');
432: $temp ++;
433: }
434: push(@items,$response);
435: my $responsename = $letter_name_map{$response};
436: my $value=$Apache::response::foilgroup{$name.'.value'};
437: if ($Apache::lonhomework::type eq 'randomizetry') {
438: $answerhash{$name} = $value;
439: }
440: if ( $response =~ /[^\s]/) {
441: $responsehash{$name}=$responsename;
442: &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
443: if ($value eq $responsename) {
444: $grade{$name}='1'; $right++;
445: } else {
446: $grade{$name}='0'; $wrong++;
447: }
448: } else {
449: $ignored++;
450: }
451: }
452: my $part=$Apache::inputtags::part;
453: my $nonlenient=&Apache::optionresponse::is_nonlenient($part);
454: my $id = $Apache::inputtags::response['-1'];
455: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
456: my $itemstr =&Apache::lonnet::array2str(@items);
457: my $gradestr =&Apache::lonnet::hash2str(%grade);
458: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
459: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
460: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
461: $responsestr;
462: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
463: $itemstr;
464: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
465: $gradestr;
466: if ($Apache::lonhomework::type eq 'randomizetry') {
467: $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
468: }
469: if (($Apache::lonhomework::type eq 'survey') ||
470: ($Apache::lonhomework::type eq 'surveycred') ||
471: ($Apache::lonhomework::type eq 'anonsurvey') ||
472: ($Apache::lonhomework::type eq 'anonsurveycred')) {
473: if ($ignored == 0) {
474: my $ad;
475: if ($Apache::lonhomework::type eq 'anonsurvey') {
476: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
477: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
478: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
479: } elsif ($Apache::lonhomework::type eq 'surveycred') {
480: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
481: } else {
482: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
483: }
484: &Apache::response::handle_previous(\%previous,$ad);
485: } elsif ($wrong==0 && $right==0) {
486: } else {
487: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
488: &Apache::response::handle_previous(\%previous,$ad);
489: }
490: } elsif ($nonlenient) {
491: my $ad;
492: if ($wrong==0 && $ignored==0) {
493: $ad='EXACT_ANS';
494: } elsif ($wrong==0 && $right==0) {
495: #nothing submitted
496: } else {
497: if ($ignored==0) {
498: $ad='INCORRECT';
499: } else {
500: $ad='MISSING_ANSWER';
501: }
502: }
503: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
504: &Apache::response::handle_previous(\%previous,$ad);
505: } else {
506: my $ad;
507: if ($wrong==0 && $right==0) {
508: #nothing submitted only assign a score if we
509: #need to override a previous grade
510: if (defined($Apache::lonhomework::history{"resource.$part.$id.awarddetail"})) {
511: $ad='ASSIGNED_SCORE';
512: }
513: } else {
514: $ad='ASSIGNED_SCORE';
515: }
516: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
517: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
518: $right/(scalar(@whichfoils));
519: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
520: scalar(@whichfoils);
521: }
522: }
523:
524: sub format_prior_answer {
525: my ($mode,$answer,$other_data) = @_;
526: my %answer =&Apache::lonnet::str2hash($answer);
527: my $foil_order =$other_data->[0];
528: my %grading =&Apache::lonnet::str2hash($other_data->[1]);
529: my @items =&Apache::lonnet::str2array($other_data->[2]);
530: my $output;
531:
532: foreach my $name (@{ $foil_order }) {
533: my $item=shift(@items);
534: $output .= '<tr><td>'.$item.'</td></tr>';
535: }
536: return if (!defined($output));
537:
538: $output = '<table class="LC_prior_match">'.$output.'</table>';
539: return $output;
540: }
541:
542: sub itemdisplay {
543: my ($location)=@_;
544: if ($location eq 'top' &&
545: !defined($Apache::matchresponse::itemtable{'location'})) {
546: return $Apache::matchresponse::itemtable{'display'};
547: }
548: if ($Apache::matchresponse::itemtable{'location'} eq $location) {
549: return $Apache::matchresponse::itemtable{'display'};
550: }
551: return undef;
552: }
553:
554: sub displayfoils {
555: my ($target,$max,$randomize)=@_;
556: my ($tabsize, $lefttabsize, $righttabsize);
557:
558: my $result;
559: my $question;
560: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
561: $randomize,
562: &Apache::response::showallfoils(),
563: \%Apache::response::foilgroup);
564: my $part=$Apache::inputtags::part;
565: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
566: my %letter_name_map;
567: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
568: %letter_name_map=
569: %{ $Apache::response::itemgroup{'letter_name_map'} };
570: }
571: my %name_letter_map;
572: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
573: %name_letter_map=
574: %{ $Apache::response::itemgroup{'name_letter_map'} };
575: }
576: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
577: foreach my $name (@whichfoils) {
578: my $text=$Apache::response::foilgroup{$name.'.text'};
579: my $value=$Apache::response::foilgroup{$name.'.value'};
580: my $letter=$name_letter_map{$value};
581: if ($target eq 'tex') {
582: $question.=' \\\\ '.$letter.':'.$text;
583: } else {
584: $question.='<br /><b>'.$letter.':</b> '.$text;
585: }
586: }
587: } else {
588: my $i = 0;
589: my $temp=1;
590: my $id=$Apache::inputtags::response[-1];
591: my $part=$Apache::inputtags::part;
592: my ($lastresponse,$newvariation);
593: if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
594: ($Apache::lonhomework::type eq 'randomizetry')) &&
595: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
596: if ($env{'form.'.$part.'.rndseed'} ne
597: $Apache::lonhomework::history{"resource.$part.rndseed"}) {
598: $newvariation = 1;
599: }
600: }
601: unless ((($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurvey') || ($Apache::lonhomework::history{"resource.$part.type"} eq 'anonsurveycred') && (defined($env{'form.grade_symb'}))) || $newvariation) {
602: $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
603: }
604: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
605: my @alphabet=('A'..'Z');
606: my @used_letters=sort(keys(%letter_name_map));
607: my $internal_counter=$Apache::lonxml::counter;
608: my ($numrows,$bubbles_per_row);
609: if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
610: ($numrows,$bubbles_per_row) =
611: &Apache::optionresponse::getnumrows(scalar(@used_letters));
612: } else {
613: $numrows = 1;
614: }
615: foreach my $name (@whichfoils) {
616: my $lastopt=$lastresponse{$name};
617: my $last_letter=$name_letter_map{$lastopt};
618: my $optionlist = '';
619: if ($target ne 'tex') {
620: $optionlist="<option></option>\n";
621: } else {
622: if ($Apache::lonhomework::type ne 'exam') {
623: if($env{'form.pdfFormFields'} eq 'yes'
624: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
625: my $fieldname = $env{'request.symb'} . '&part_'. $Apache::inputtags::part .'&matchresponse'. '&HWVAL_' . $Apache::inputtags::response['-1'] . ':' . $temp . '&submit_' . $Apache::inputtags::part . '&';
626: $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
627: } else {
628:
629: $optionlist='\framebox[10 mm][s]{\tiny\strut}';
630: }
631: }
632: }
633: my $option;
634: foreach $option (@used_letters) {
635: if ($option eq $last_letter) {
636: if ($target ne 'tex') {
637: $optionlist.="<option selected=\"selected\">$option</option>\n";
638: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
639: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
640: && $Apache::lonhomework::type ne 'exam') {
641: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
642: }
643: } else {
644: if ($target ne 'tex') {
645: $optionlist.="<option>$option</option>\n";
646: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
647: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
648: && $Apache::lonhomework::type ne 'exam') {
649: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
650: }
651: }
652: }
653: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
654: $optionlist='<select onchange="javascript:setSubmittedPart(\''.
655: $part.'\');" name="HWVAL_'.
656: $Apache::inputtags::response[-1].':'.$temp.'">'.
657: $optionlist."</select>\n";
658: }
659: my $text=$Apache::response::foilgroup{$name.'.text'};
660: #
661: # Factor out the tex computations of the left/right 1/2 minipage
662: # widths for left or right positioning. This allows us
663: # to, if necessary constrain the bubble widths:
664: #
665: if ($target eq 'tex' &&
666: (&itemdisplay('left') || &itemdisplay('right'))) {
667: $tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
668: ($lefttabsize,$righttabsize)=(0,0);
669: if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
670: $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
671: $lefttabsize=$tabsize*$1/100;
672: $righttabsize=0.95*($tabsize-$lefttabsize);
673: } else {
674: $tabsize=~/(\d+\.?\d*)/;
675: $lefttabsize=$1/2.1;
676: $righttabsize=0.95*($1-$lefttabsize);
677: }
678: }
679: if ($target ne 'tex') {
680: if ($Apache::lonhomework::type ne 'exam') {
681: $question.="<br />\n".$optionlist.$text;
682: } else {
683: $question.="<br />\n".$text;
684: }
685: if ($Apache::lonhomework::type eq 'exam') {
686: my @blank;
687: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
688: }
689: } else {
690: # Note that if left or right positioned, we must
691: # confine the bubbles to righttabsize:
692: #
693: if ($Apache::lonhomework::type eq 'exam') {
694: $question.=' '.$optionlist.$text."\n";
695: my @emptyItems = ();
696: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
697: $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
698: .'\item \hskip -3mm ';
699: if ($numrows == 1) {
700: $question .= '\textbf{'.$internal_counter.'}';
701: } else {
702: my $linetext;
703: for (my $i=0; $i<$numrows; $i++) {
704: $linetext .= $internal_counter+$i.', ';
705: }
706: $linetext =~ s/,\s$//;
707: $question .= '\small {\textbf{'.$linetext.'}} '.
708: '\hskip 2 mm {\footnotesize '.
709: &mt('(Bubble once in [_1] lines)',$numrows).
710: '} \vskip 3 mm';
711: }
712: my $max_width;
713: if (&itemdisplay('left') || &itemdisplay('right')) {
714: $max_width = $righttabsize;
715: }
716: $question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems,'',$max_width,$numrows,$bubbles_per_row,$internal_counter);
717: $question .= '\end{list} \vskip -8 mm \strut ';
718: $internal_counter += $numrows;
719: } else {
720: if($env{'form.pdfFormFields'} eq 'yes'
721: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
722: $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
723: } else {
724: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
725: }
726: }
727: }
728: $temp++;
729: }
730: }
731: if ($result=&itemdisplay('top')) {
732: $result.=$question;
733: } elsif ($result=&itemdisplay('bottom')) {
734: $result=$question.$result;
735: } elsif ($result=&itemdisplay('right')) {
736: if ($target ne 'tex') {
737: #remove the first <br />
738: $question=~s|<br />||;
739: $result='<table><tr><td valign="top">'.$question.
740: '</td><td valign="top">'.$result.'</td></tr></table>';
741: } else {
742: $lefttabsize.=' mm ';
743: $righttabsize.=' mm ';
744: $result='\setlength{\tabcolsep}{1 mm}\begin{tabular}{p{'.$righttabsize.'}p{'.$lefttabsize.'}}\begin{minipage}{'.$righttabsize.'}'.$question.'\end{minipage}&\begin{minipage}{'.$lefttabsize.'}'.$result.'\end{minipage}\end{tabular}';
745: }
746: } elsif ($result=&itemdisplay('left')) {
747: if ($target ne 'tex') {
748: #remove the first <br />
749: $question=~s|<br />||;
750: $result='<table><tr><td valign="top">'.$result.
751: '</td><td valign="top">'.$question.'</td></tr></table>';
752: } else {
753: $lefttabsize.=' mm ';
754: $righttabsize.=' mm ';
755: $result='\setlength{\tabcolsep}{1 mm}\begin{tabular}{p{'.$lefttabsize.'}p{'.$righttabsize.'}}\begin{minipage}{'.$lefttabsize.'}'.$result.'\end{minipage}&\begin{minipage}{'.$righttabsize.'}'.$question.'\end{minipage}\end{tabular}';
756: }
757: }
758: if ($target eq 'web') {
759: my $questiontype;
760: if ($Apache::lonhomework::type eq 'randomizetry') {
761: $questiontype = $Apache::lonhomework::type;
762: }
763: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
764: [\@whichfoils,
765: 'submissiongrading',
766: 'submissionitems'],
767: $questiontype);
768: }
769: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
770: return $result;
771: }
772:
773: sub getfoilcounts {
774: my ($max)=@_;
775: # +1 since instructors will count from 1
776: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
777: if (&Apache::response::showallfoils()) { $max=$count; }
778: if ($count>$max) { $count=$max }
779: &Apache::lonxml::debug("Count is $count from $max");
780: return $count;
781: }
782:
783:
784: sub start_conceptgroup {
785: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
786: $Apache::matchresponse::conceptgroup=1;
787: %Apache::response::conceptgroup=();
788: my $result;
789: if ($target eq 'edit') {
790: $result.=&Apache::edit::tag_start($target,$token,
791: "Concept Grouped Foils");
792: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
793: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
794: }
795: if ($target eq 'modified') {
796: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
797: $safeeval,'concept');
798: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
799: }
800: return $result;
801: }
802:
803: sub end_conceptgroup {
804: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
805: $Apache::matchresponse::conceptgroup=0;
806: my $result='';
807: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
808: $target eq 'tex' || $target eq 'analyze') {
809: &Apache::response::pick_foil_for_concept($target,
810: ['value','text','location'],
811: \%Apache::hint::match,
812: $parstack,$safeeval);
813: } elsif ($target eq 'edit') {
814: $result=&Apache::edit::end_table();
815: }
816: return $result;
817: }
818:
819: sub insert_conceptgroup {
820: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
821: return $result;
822: }
823:
824: sub start_foil {
825: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
826: my $result='';
827: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
828: &Apache::lonxml::startredirection;
829: if ($target eq 'analyze') {
830: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
831: }
832: } elsif ($target eq 'edit') {
833: $result=&Apache::edit::tag_start($target,$token,"Foil");
834: my $level='-2';
835: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
836: $result.=&Apache::edit::text_arg('Name:','name',$token);
837: my @names;
838: if (defined(@{ $Apache::response::itemgroup{'names'} })) {
839: @names=@{ $Apache::response::itemgroup{'names'} };
840: }
841: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
842: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
843: $safeeval,'-3');
844: if ($randomize ne 'no') {
845: $result.=&Apache::edit::select_arg('Location:','location',
846: ['random','top','bottom'],$token);
847: }
848: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
849: } elsif ($target eq 'modified') {
850: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
851: $safeeval,'value',
852: 'name','location');
853: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
854: }
855: return $result;
856: }
857:
858: sub end_foil {
859: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
860: my $text ='';
861: my $result = '';
862: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
863: $text=&Apache::lonxml::endredirection;
864: }
865:
866: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
867: $target eq 'tex' || $target eq 'analyze') {
868: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
869: $text='\vskip 5mm $\triangleright$ '.$text;
870: }
871: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
872: if ($value ne 'unused') {
873: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
874: &Apache::lonxml::debug("Got a name of :$name:");
875: if (!$name) {
876: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
877: $name=$Apache::lonxml::curdepth;
878: }
879: &Apache::lonxml::debug("Using a name of :$name:");
880: if (defined($Apache::response::foilnames{$name})) {
881: &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
882: }
883: $Apache::response::foilnames{$name}++;
884: my $location =&Apache::lonxml::get_param('location',$parstack,
885: $safeeval);
886: if ( $Apache::matchresponse::conceptgroup
887: && !&Apache::response::showallfoils() ) {
888:
889: push @{ $Apache::response::conceptgroup{'names'} }, $name;
890: $Apache::response::conceptgroup{"$name.value"} = $value;
891: $Apache::response::conceptgroup{"$name.text"} = $text;
892: $Apache::response::conceptgroup{"$name.location"} = $location;
893: } else {
894:
895: push @{ $Apache::response::foilgroup{'names'} }, $name;
896: $Apache::response::foilgroup{"$name.value"} = $value;
897: $Apache::response::foilgroup{"$name.text"} = $text;
898: $Apache::response::foilgroup{"$name.location"} = $location;
899: }
900: }
901: }
902: if ($target eq 'edit') {
903: $result.= &Apache::edit::tag_end($target,$token,'');
904: }
905: return $result;
906: }
907:
908: sub insert_foil {
909: return '
910: <foil name="" value="unused">
911: <startouttext />
912: <endouttext />
913: </foil>';
914: }
915:
916: 1;
917: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>