Annotation of loncom/homework/matchresponse.pm, revision 1.83
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
1.83 ! raeburn 4: # $Id: matchresponse.pm,v 1.82 2011/09/13 21:42:58 raeburn Exp $
1.1 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: #
28:
29: package Apache::matchresponse;
30: use strict;
31: use HTML::Entities();
32: use Math::Random();
1.37 albertel 33: use Apache::optionresponse();
34: use Apache::lonlocal;
1.52 albertel 35: use Apache::lonnet;
1.71 onken 36: use Apache::lonxml;
1.6 sakharuk 37:
1.1 albertel 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=();
1.37 albertel 52: undef(%Apache::response::foilnames);
1.1 albertel 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).
1.73 raeburn 57: '<tr><td>'.&Apache::lonxml::description($token).'</td>'
1.72 bisitz 58: .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
59: .&Apache::edit::deletelist($target,$token)
60: .'</span></td>'
1.73 raeburn 61: .'<td> '.&Apache::edit::end_row()
1.72 bisitz 62: .&Apache::edit::start_spanning_row();
1.1 albertel 63:
64: $result.=
65: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
1.72 bisitz 66: &Apache::edit::select_arg('Randomize Foil Order:','randomize',
1.1 albertel 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); }
1.28 albertel 74: } elsif ($target eq 'analyze') {
75: my $part_id="$Apache::inputtags::part.$id";
1.69 raeburn 76: $Apache::lonhomework::analyze{"$part_id.type"} = 'matchresponse';
1.28 albertel 77: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.68 raeburn 78: push (@{ $Apache::lonhomework::analyze{"$part_id.bubble_lines"} },
1.65 foxr 79: 1);
1.1 albertel 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'));
1.37 albertel 92: undef(%Apache::response::foilnames);
1.1 albertel 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=();
1.4 albertel 101: %Apache::matchresponse::itemtable=();
1.15 albertel 102:
1.1 albertel 103: if ($target eq 'edit') {
104: $result=&Apache::edit::tag_start($target,$token);
105: $result.=&Apache::edit::select_arg('Randomize Order:','randomize',
1.73 raeburn 106: ['yes','no'],$token).' 'x 3;
1.4 albertel 107: $result.=&Apache::edit::select_arg('Items Display Location:',
108: 'location',
109: ['top','bottom','left','right'],
1.73 raeburn 110: $token).' 'x 3;
111: $result.=&Apache::edit::select_arg('Items Display Direction:',
1.51 albertel 112: 'direction',
113: ['vertical','horizontal'],
114: $token);
1.1 albertel 115: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.4 albertel 116: } elsif ($target eq 'modified') {
117: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
118: $safeeval,'randomize',
1.51 albertel 119: 'location','direction');
1.4 albertel 120: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.21 sakharuk 121: } elsif ($target eq 'web' or $target eq 'tex') {
1.4 albertel 122: $Apache::matchresponse::itemtable{'location'}=
123: &Apache::lonxml::get_param('location',$parstack,$safeeval);
1.43 sakharuk 124: $Apache::matchresponse::TeXitemgroupwidth=&Apache::lonxml::get_param('TeXitemgroupwidth',$parstack,$safeeval,undef,0);
1.1 albertel 125: }
126: return $result;
127: }
128:
129: sub end_itemgroup {
130: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
131: my $result;
132:
1.17 albertel 133: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
1.1 albertel 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);
1.2 albertel 137: if ($randomize ne 'no' ) {
1.18 albertel 138: @names=&Apache::response::whichorder($#names+1,$randomize,0,
139: \%Apache::response::itemgroup);
1.1 albertel 140: }
1.30 albertel 141: if ($target eq 'analyze') {
142: my $partid="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
143: push (@{ $Apache::lonhomework::analyze{"$partid.items"} }, @names);
144: }
1.1 albertel 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: }
1.15 albertel 154: $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
1.1 albertel 155: $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
1.51 albertel 156: my $direction=&Apache::lonxml::get_param('direction',$parstack,$safeeval);
1.1 albertel 157: if ($target eq 'web') {
1.58 foxr 158:
159: my $table='<br /><table>'; # extra space to match what latex does.
1.1 albertel 160: my $i=0;
1.51 albertel 161: if ($direction eq 'horizontal') { $table .='<tr>';}
1.1 albertel 162: foreach my $name (@names) {
1.51 albertel 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>'; }
1.1 albertel 167: $i++;
168: }
1.51 albertel 169: if ($direction eq 'horizontal') { $table .='</tr>';}
1.4 albertel 170: $table.='</table>';
171: $Apache::matchresponse::itemtable{'display'}=$table;
1.60 albertel 172: $Apache::lonxml::post_evaluate=0;
1.5 sakharuk 173: } elsif ($target eq 'tex') {
1.58 foxr 174: my $table=' \begin{description}\setlength{\leftmargin}{2em}\setlength{\labelwidth}{1em}\setlength{\itemsep}{0.5pt plus1pt minus2pt}\setlength{\listparindent}{0em} ';
1.5 sakharuk 175: my $i=0;
1.82 raeburn 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: }
1.5 sakharuk 183: foreach my $name (@names) {
1.59 foxr 184: # $Apache::response::itemgroup{$name.'.text'}=~s/\$\$/\$/g;
1.82 raeburn 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.'] '.
1.58 foxr 201: $Apache::response::itemgroup{$name.'.text'};
1.5 sakharuk 202: $i++;
203: }
1.58 foxr 204: $table.=' \end{description} \strut ';
1.77 foxr 205: if ($Apache::lonhomework::type eq 'exam') {$table.='\vskip -13 mm \strut ';}
1.5 sakharuk 206: $Apache::matchresponse::itemtable{'display'}=$table;
1.60 albertel 207: $Apache::lonxml::post_evaluate=0;
1.17 albertel 208: }
1.1 albertel 209: return $result;
210: }
211:
212: sub start_item {
213: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
214: my $result='';
1.30 albertel 215: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 216: &Apache::lonxml::startredirection;
217: } elsif ($target eq 'edit') {
1.3 albertel 218: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
219: $safeeval,'-2');
1.1 albertel 220: $result=&Apache::edit::tag_start($target,$token,"Item");
221: $result.=&Apache::edit::text_arg('Name:','name',$token);
1.3 albertel 222: if ($randomize ne 'no') {
1.73 raeburn 223: $result.=' 'x 3 .
224: &Apache::edit::select_arg('Location:','location',
1.3 albertel 225: ['random','top','bottom'],
226: $token);
227: }
228: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.1 albertel 229: } elsif ($target eq 'modified') {
230: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.3 albertel 231: $safeeval,'name',
232: 'location');
1.1 albertel 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 = '';
1.30 albertel 242: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 243: $text=&Apache::lonxml::endredirection;
244: }
245: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.30 albertel 246: $target eq 'edit' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 247: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.3 albertel 248: my $location=&Apache::lonxml::get_param('location',$parstack,
249: $safeeval);
1.1 albertel 250: &Apache::lonxml::debug("Got a name of :$name:");
1.55 albertel 251: if ($name eq "") { $name=$Apache::lonxml::curdepth; }
1.1 albertel 252: &Apache::lonxml::debug("Using a name of :$name:");
253: push @{ $Apache::response::itemgroup{'names'} }, $name;
254: $Apache::response::itemgroup{"$name.text"} = $text;
1.3 albertel 255: $Apache::response::itemgroup{"$name.location"} = $location;
1.1 albertel 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;
1.81 raeburn 277: &Apache::response::pushrandomnumber(undef,$target);
1.1 albertel 278: if ($target eq 'edit') {
279: $result.=&Apache::edit::start_table($token)
1.73 raeburn 280: .'<tr><td>'.&mt('Collection Of Foils').'</td>'
1.72 bisitz 281: .'<td><span class="LC_nobreak">'.&mt('Delete?')
1.1 albertel 282: .&Apache::edit::deletelist($target,$token)
1.72 bisitz 283: .'</span></td>'
1.73 raeburn 284: .'<td> '.&Apache::edit::end_row()
1.1 albertel 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;
1.67 foxr 293: my $part = $Apache::inputtags::part;
1.68 raeburn 294: my $response_id = $Apache::inputtags::response[-1];
1.67 foxr 295:
1.28 albertel 296: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 297: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
298: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
299: $safeeval,'-2');
1.5 sakharuk 300: if ($target eq 'web' || $target eq 'tex') {
1.1 albertel 301: $result=&displayfoils($target,$max,$randomize);
1.60 albertel 302: $Apache::lonxml::post_evaluate=0;
1.1 albertel 303: } elsif ($target eq 'answer' ) {
304: $result=&displayanswers($max,$randomize);
305: } elsif ( $target eq 'grade') {
306: &grade_response($max,$randomize);
1.28 albertel 307: } elsif ( $target eq 'analyze') {
308: my @shown=&whichfoils($max,$randomize);
309: &Apache::response::analyze_store_foilgroup(\@shown,
1.29 albertel 310: ['text','value','location']);
1.28 albertel 311: #FIXME need to store options in some way
1.1 albertel 312: }
1.82 raeburn 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");
1.66 foxr 327: if ($target eq 'analyze') {
328: &Apache::lonhomework::set_bubble_lines();
329: }
330:
1.1 albertel 331: } elsif ($target eq 'edit') {
332: $result=&Apache::edit::end_table();
333: }
1.41 albertel 334: &Apache::response::poprandomnumber();
1.1 albertel 335: return $result;
1.29 albertel 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);
1.1 albertel 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'} };
1.18 albertel 350: my @whichfoils = &Apache::response::whichorder(&getfoilcounts($max),
351: $randomize,
352: &Apache::response::showallfoils(),
353: \%Apache::response::foilgroup);
1.1 albertel 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: }
1.56 albertel 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');
1.1 albertel 377: }
378: return $result;
379: }
380:
381:
382: sub grade_response {
383: my ($max,$randomize)=@_;
1.18 albertel 384: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
385: $randomize,
386: &Apache::response::showallfoils(),
387: \%Apache::response::foilgroup);
1.50 albertel 388: if (!&Apache::response::submitted()) { return; }
1.1 albertel 389: my %responsehash;
390: my %grade;
1.81 raeburn 391: my %answerhash;
1.33 albertel 392: my ($temp,$right,$wrong,$ignored)=(1,0,0,0);
1.1 albertel 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: }
1.30 albertel 398: my @items;
1.82 raeburn 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: }
1.1 albertel 414: foreach my $name (@whichfoils) {
1.82 raeburn 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: }
1.30 albertel 434: push(@items,$response);
1.1 albertel 435: my $responsename = $letter_name_map{$response};
436: my $value=$Apache::response::foilgroup{$name.'.value'};
1.81 raeburn 437: if ($Apache::lonhomework::type eq 'randomizetry') {
438: $answerhash{$name} = $value;
439: }
1.1 albertel 440: if ( $response =~ /[^\s]/) {
1.31 albertel 441: $responsehash{$name}=$responsename;
1.33 albertel 442: &Apache::lonxml::debug("submitted a $response($responsename) for $value<br />\n");
1.1 albertel 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;
1.83 ! raeburn 453: my $nonlenient=&Apache::optionresponse::is_nonlenient($part);
1.1 albertel 454: my $id = $Apache::inputtags::response['-1'];
455: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.30 albertel 456: my $itemstr =&Apache::lonnet::array2str(@items);
1.1 albertel 457: my $gradestr =&Apache::lonnet::hash2str(%grade);
1.30 albertel 458: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1 albertel 459: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
460: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
461: $responsestr;
1.30 albertel 462: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
463: $itemstr;
1.1 albertel 464: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
465: $gradestr;
1.81 raeburn 466: if ($Apache::lonhomework::type eq 'randomizetry') {
467: $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
468: }
1.79 raeburn 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')) {
1.47 albertel 473: if ($ignored == 0) {
1.79 raeburn 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: }
1.47 albertel 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: }
1.83 ! raeburn 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);
1.34 albertel 505: } else {
1.83 ! raeburn 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);
1.34 albertel 521: }
1.1 albertel 522: }
523:
1.64 albertel 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:
1.4 albertel 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: }
1.64 albertel 553:
1.1 albertel 554: sub displayfoils {
555: my ($target,$max,$randomize)=@_;
1.62 foxr 556: my ($tabsize, $lefttabsize, $righttabsize);
557:
1.1 albertel 558: my $result;
1.4 albertel 559: my $question;
1.18 albertel 560: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
561: $randomize,
562: &Apache::response::showallfoils(),
563: \%Apache::response::foilgroup);
1.1 albertel 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: }
1.25 albertel 576: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 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};
1.5 sakharuk 581: if ($target eq 'tex') {
582: $question.=' \\\\ '.$letter.':'.$text;
583: } else {
1.53 albertel 584: $question.='<br /><b>'.$letter.':</b> '.$text;
1.5 sakharuk 585: }
1.1 albertel 586: }
587: } else {
588: my $i = 0;
1.36 albertel 589: my $temp=1;
1.1 albertel 590: my $id=$Apache::inputtags::response[-1];
591: my $part=$Apache::inputtags::part;
1.81 raeburn 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) {
1.79 raeburn 602: $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
603: }
1.9 sakharuk 604: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
605: my @alphabet=('A'..'Z');
1.15 albertel 606: my @used_letters=sort(keys(%letter_name_map));
1.20 sakharuk 607: my $internal_counter=$Apache::lonxml::counter;
1.82 raeburn 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: }
1.1 albertel 615: foreach my $name (@whichfoils) {
616: my $lastopt=$lastresponse{$name};
617: my $last_letter=$name_letter_map{$lastopt};
1.5 sakharuk 618: my $optionlist = '';
1.7 sakharuk 619: if ($target ne 'tex') {
620: $optionlist="<option></option>\n";
621: } else {
622: if ($Apache::lonhomework::type ne 'exam') {
1.76 onken 623: if($env{'form.pdfFormFields'} eq 'yes'
624: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
1.75 raeburn 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 {
1.77 foxr 628:
1.75 raeburn 629: $optionlist='\framebox[10 mm][s]{\tiny\strut}';
630: }
1.74 raeburn 631: }
1.7 sakharuk 632: }
1.1 albertel 633: my $option;
1.15 albertel 634: foreach $option (@used_letters) {
1.1 albertel 635: if ($option eq $last_letter) {
1.71 onken 636: if ($target ne 'tex') {
1.81 raeburn 637: $optionlist.="<option selected=\"selected\">$option</option>\n";
1.76 onken 638: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.80 onken 639: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
640: && $Apache::lonhomework::type ne 'exam') {
1.75 raeburn 641: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
1.71 onken 642: }
1.1 albertel 643: } else {
1.71 onken 644: if ($target ne 'tex') {
645: $optionlist.="<option>$option</option>\n";
1.76 onken 646: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.80 onken 647: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
648: && $Apache::lonhomework::type ne 'exam') {
1.75 raeburn 649: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
1.71 onken 650: }
1.1 albertel 651: }
652: }
1.19 sakharuk 653: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.61 albertel 654: $optionlist='<select onchange="javascript:setSubmittedPart(\''.
655: $part.'\');" name="HWVAL_'.
1.5 sakharuk 656: $Apache::inputtags::response[-1].':'.$temp.'">'.
657: $optionlist."</select>\n";
658: }
1.1 albertel 659: my $text=$Apache::response::foilgroup{$name.'.text'};
1.62 foxr 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: }
1.5 sakharuk 679: if ($target ne 'tex') {
1.19 sakharuk 680: if ($Apache::lonhomework::type ne 'exam') {
1.45 albertel 681: $question.="<br />\n".$optionlist.$text;
1.19 sakharuk 682: } else {
1.45 albertel 683: $question.="<br />\n".$text;
1.19 sakharuk 684: }
1.6 sakharuk 685: if ($Apache::lonhomework::type eq 'exam') {
1.42 albertel 686: my @blank;
687: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
1.6 sakharuk 688: }
689: } else {
1.62 foxr 690: # Note that if left or right positioned, we must
691: # confine the bubbles to righttabsize:
692: #
1.80 onken 693: if ($Apache::lonhomework::type eq 'exam') {
1.6 sakharuk 694: $question.=' '.$optionlist.$text."\n";
1.13 sakharuk 695: my @emptyItems = ();
1.16 albertel 696: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.62 foxr 697: $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
1.82 raeburn 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;
1.62 foxr 713: if (&itemdisplay('left') || &itemdisplay('right')) {
1.82 raeburn 714: $max_width = $righttabsize;
715: }
716: $question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems,'',$max_width,$numrows,$bubbles_per_row,$internal_counter);
1.62 foxr 717: $question .= '\end{list} \vskip -8 mm \strut ';
1.82 raeburn 718: $internal_counter += $numrows;
1.6 sakharuk 719: } else {
1.76 onken 720: if($env{'form.pdfFormFields'} eq 'yes'
721: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
1.75 raeburn 722: $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
723: } else {
724: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
725: }
1.6 sakharuk 726: }
1.5 sakharuk 727: }
1.1 albertel 728: $temp++;
729: }
1.4 albertel 730: }
731: if ($result=&itemdisplay('top')) {
732: $result.=$question;
733: } elsif ($result=&itemdisplay('bottom')) {
734: $result=$question.$result;
735: } elsif ($result=&itemdisplay('right')) {
1.32 sakharuk 736: if ($target ne 'tex') {
1.45 albertel 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>';
1.32 sakharuk 741: } else {
1.43 sakharuk 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}';
1.32 sakharuk 745: }
1.4 albertel 746: } elsif ($result=&itemdisplay('left')) {
1.32 sakharuk 747: if ($target ne 'tex') {
1.45 albertel 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>';
1.32 sakharuk 752: } else {
1.43 sakharuk 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}';
1.32 sakharuk 756: }
1.1 albertel 757: }
1.64 albertel 758: if ($target eq 'web') {
1.81 raeburn 759: my $questiontype;
760: if ($Apache::lonhomework::type eq 'randomizetry') {
761: $questiontype = $Apache::lonhomework::type;
762: }
1.64 albertel 763: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
764: [\@whichfoils,
765: 'submissiongrading',
1.81 raeburn 766: 'submissionitems'],
767: $questiontype);
1.64 albertel 768: }
1.5 sakharuk 769: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1 albertel 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='';
1.28 albertel 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);
1.1 albertel 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='';
1.28 albertel 827: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 828: &Apache::lonxml::startredirection;
1.46 albertel 829: if ($target eq 'analyze') {
830: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
831: }
1.1 albertel 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 = '';
1.28 albertel 862: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 863: $text=&Apache::lonxml::endredirection;
864: }
1.48 albertel 865:
1.28 albertel 866: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
867: $target eq 'tex' || $target eq 'analyze') {
1.48 albertel 868: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
869: $text='\vskip 5mm $\triangleright$ '.$text;
870: }
1.1 albertel 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:");
1.49 albertel 875: if (!$name) {
1.70 bisitz 876: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.49 albertel 877: $name=$Apache::lonxml::curdepth;
878: }
1.1 albertel 879: &Apache::lonxml::debug("Using a name of :$name:");
1.37 albertel 880: if (defined($Apache::response::foilnames{$name})) {
1.70 bisitz 881: &Apache::lonxml::error(&mt("Foil name [_1] appears more than once. Foil names need to be unique.",'<b><tt>'.$name.'</tt></b>'));
1.37 albertel 882: }
1.38 albertel 883: $Apache::response::foilnames{$name}++;
1.37 albertel 884: my $location =&Apache::lonxml::get_param('location',$parstack,
885: $safeeval);
1.1 albertel 886: if ( $Apache::matchresponse::conceptgroup
887: && !&Apache::response::showallfoils() ) {
1.77 foxr 888:
1.1 albertel 889: push @{ $Apache::response::conceptgroup{'names'} }, $name;
890: $Apache::response::conceptgroup{"$name.value"} = $value;
1.48 albertel 891: $Apache::response::conceptgroup{"$name.text"} = $text;
1.1 albertel 892: $Apache::response::conceptgroup{"$name.location"} = $location;
893: } else {
1.77 foxr 894:
1.1 albertel 895: push @{ $Apache::response::foilgroup{'names'} }, $name;
896: $Apache::response::foilgroup{"$name.value"} = $value;
1.48 albertel 897: $Apache::response::foilgroup{"$name.text"} = $text;
1.1 albertel 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: }
1.82 raeburn 915:
1.1 albertel 916: 1;
917: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>