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