Annotation of loncom/homework/matchresponse.pm, revision 1.82
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
1.82 ! raeburn 4: # $Id: matchresponse.pm,v 1.81 2010/12/19 00:54:20 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;
453: my $id = $Apache::inputtags::response['-1'];
454: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
1.30 albertel 455: my $itemstr =&Apache::lonnet::array2str(@items);
1.1 albertel 456: my $gradestr =&Apache::lonnet::hash2str(%grade);
1.30 albertel 457: my %previous=&Apache::response::check_for_previous($responsestr,$part,$id);
1.1 albertel 458: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
459: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
460: $responsestr;
1.30 albertel 461: $Apache::lonhomework::results{"resource.$part.$id.submissionitems"}=
462: $itemstr;
1.1 albertel 463: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
464: $gradestr;
1.81 raeburn 465: if ($Apache::lonhomework::type eq 'randomizetry') {
466: $Apache::lonhomework::results{"resource.$part.$id.foilorder"} = &Apache::lonnet::array2str(@whichfoils);
467: }
1.79 raeburn 468: if (($Apache::lonhomework::type eq 'survey') ||
469: ($Apache::lonhomework::type eq 'surveycred') ||
470: ($Apache::lonhomework::type eq 'anonsurvey') ||
471: ($Apache::lonhomework::type eq 'anonsurveycred')) {
1.47 albertel 472: if ($ignored == 0) {
1.79 raeburn 473: my $ad;
474: if ($Apache::lonhomework::type eq 'anonsurvey') {
475: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS';
476: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
477: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='ANONYMOUS_CREDIT';
478: } elsif ($Apache::lonhomework::type eq 'surveycred') {
479: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED_CREDIT';
480: } else {
481: $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='SUBMITTED';
482: }
1.47 albertel 483: &Apache::response::handle_previous(\%previous,$ad);
484: } elsif ($wrong==0 && $right==0) {
485: } else {
486: my $ad=$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}='MISSING_ANSWER';
487: &Apache::response::handle_previous(\%previous,$ad);
488: }
489: } elsif (!$Apache::lonhomework::scantronmode) {
1.34 albertel 490: my $ad;
491: if ($wrong==0 && $ignored==0) {
492: $ad='EXACT_ANS';
493: } elsif ($wrong==0 && $right==0) {
494: #nothing submitted
495: } else {
496: if ($ignored==0) {
497: $ad='INCORRECT';
498: } else {
499: $ad='MISSING_ANSWER';
500: }
501: }
502: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
503: &Apache::response::handle_previous(\%previous,$ad);
504: } else {
505: my $ad;
506: if ($wrong==0 && $right==0) {
507: #nothing submitted
508: } else {
509: $ad='ASSIGNED_SCORE';
510: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=
511: $ad;
512: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
1.35 albertel 513: $right/(scalar(@whichfoils));
1.34 albertel 514: $Apache::lonhomework::results{"resource.$part.$id.numfoils"}=
1.35 albertel 515: scalar(@whichfoils);
1.34 albertel 516: }
517: }
1.1 albertel 518: }
519:
1.64 albertel 520: sub format_prior_answer {
521: my ($mode,$answer,$other_data) = @_;
522: my %answer =&Apache::lonnet::str2hash($answer);
523: my $foil_order =$other_data->[0];
524: my %grading =&Apache::lonnet::str2hash($other_data->[1]);
525: my @items =&Apache::lonnet::str2array($other_data->[2]);
526: my $output;
527:
528: foreach my $name (@{ $foil_order }) {
529: my $item=shift(@items);
530: $output .= '<tr><td>'.$item.'</td></tr>';
531: }
532: return if (!defined($output));
533:
534: $output = '<table class="LC_prior_match">'.$output.'</table>';
535: return $output;
536: }
537:
1.4 albertel 538: sub itemdisplay {
539: my ($location)=@_;
540: if ($location eq 'top' &&
541: !defined($Apache::matchresponse::itemtable{'location'})) {
542: return $Apache::matchresponse::itemtable{'display'};
543: }
544: if ($Apache::matchresponse::itemtable{'location'} eq $location) {
545: return $Apache::matchresponse::itemtable{'display'};
546: }
547: return undef;
548: }
1.64 albertel 549:
1.1 albertel 550: sub displayfoils {
551: my ($target,$max,$randomize)=@_;
1.62 foxr 552: my ($tabsize, $lefttabsize, $righttabsize);
553:
1.1 albertel 554: my $result;
1.4 albertel 555: my $question;
1.18 albertel 556: my (@whichfoils)=&Apache::response::whichorder(&getfoilcounts($max),
557: $randomize,
558: &Apache::response::showallfoils(),
559: \%Apache::response::foilgroup);
1.1 albertel 560: my $part=$Apache::inputtags::part;
561: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
562: my %letter_name_map;
563: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
564: %letter_name_map=
565: %{ $Apache::response::itemgroup{'letter_name_map'} };
566: }
567: my %name_letter_map;
568: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
569: %name_letter_map=
570: %{ $Apache::response::itemgroup{'name_letter_map'} };
571: }
1.25 albertel 572: if ( &Apache::response::show_answer() && ($target ne 'tex')) {
1.1 albertel 573: foreach my $name (@whichfoils) {
574: my $text=$Apache::response::foilgroup{$name.'.text'};
575: my $value=$Apache::response::foilgroup{$name.'.value'};
576: my $letter=$name_letter_map{$value};
1.5 sakharuk 577: if ($target eq 'tex') {
578: $question.=' \\\\ '.$letter.':'.$text;
579: } else {
1.53 albertel 580: $question.='<br /><b>'.$letter.':</b> '.$text;
1.5 sakharuk 581: }
1.1 albertel 582: }
583: } else {
584: my $i = 0;
1.36 albertel 585: my $temp=1;
1.1 albertel 586: my $id=$Apache::inputtags::response[-1];
587: my $part=$Apache::inputtags::part;
1.81 raeburn 588: my ($lastresponse,$newvariation);
589: if ((($Apache::lonhomework::history{"resource.$part.type"} eq 'randomizetry') ||
590: ($Apache::lonhomework::type eq 'randomizetry')) &&
591: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
592: if ($env{'form.'.$part.'.rndseed'} ne
593: $Apache::lonhomework::history{"resource.$part.rndseed"}) {
594: $newvariation = 1;
595: }
596: }
597: 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 598: $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
599: }
1.9 sakharuk 600: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
601: my @alphabet=('A'..'Z');
1.15 albertel 602: my @used_letters=sort(keys(%letter_name_map));
1.20 sakharuk 603: my $internal_counter=$Apache::lonxml::counter;
1.82 ! raeburn 604: my ($numrows,$bubbles_per_row);
! 605: if (($target eq 'tex') && ($Apache::lonhomework::type eq 'exam')) {
! 606: ($numrows,$bubbles_per_row) =
! 607: &Apache::optionresponse::getnumrows(scalar(@used_letters));
! 608: } else {
! 609: $numrows = 1;
! 610: }
1.1 albertel 611: foreach my $name (@whichfoils) {
612: my $lastopt=$lastresponse{$name};
613: my $last_letter=$name_letter_map{$lastopt};
1.5 sakharuk 614: my $optionlist = '';
1.7 sakharuk 615: if ($target ne 'tex') {
616: $optionlist="<option></option>\n";
617: } else {
618: if ($Apache::lonhomework::type ne 'exam') {
1.76 onken 619: if($env{'form.pdfFormFields'} eq 'yes'
620: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
1.75 raeburn 621: my $fieldname = $env{'request.symb'} . '&part_'. $Apache::inputtags::part .'&matchresponse'. '&HWVAL_' . $Apache::inputtags::response['-1'] . ':' . $temp . '&submit_' . $Apache::inputtags::part . '&';
622: $optionlist = &Apache::lonxml::print_pdf_start_combobox($fieldname);
623: } else {
1.77 foxr 624:
1.75 raeburn 625: $optionlist='\framebox[10 mm][s]{\tiny\strut}';
626: }
1.74 raeburn 627: }
1.7 sakharuk 628: }
1.1 albertel 629: my $option;
1.15 albertel 630: foreach $option (@used_letters) {
1.1 albertel 631: if ($option eq $last_letter) {
1.71 onken 632: if ($target ne 'tex') {
1.81 raeburn 633: $optionlist.="<option selected=\"selected\">$option</option>\n";
1.76 onken 634: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.80 onken 635: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
636: && $Apache::lonhomework::type ne 'exam') {
1.75 raeburn 637: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
1.71 onken 638: }
1.1 albertel 639: } else {
1.71 onken 640: if ($target ne 'tex') {
641: $optionlist.="<option>$option</option>\n";
1.76 onken 642: } elsif ($target eq 'tex' && $env{'form.pdfFormFields'} eq 'yes'
1.80 onken 643: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER'
644: && $Apache::lonhomework::type ne 'exam') {
1.75 raeburn 645: $optionlist .= &Apache::lonxml::print_pdf_add_combobox_option($option);
1.71 onken 646: }
1.1 albertel 647: }
648: }
1.19 sakharuk 649: if ($target ne 'tex' && $Apache::lonhomework::type ne 'exam') {
1.61 albertel 650: $optionlist='<select onchange="javascript:setSubmittedPart(\''.
651: $part.'\');" name="HWVAL_'.
1.5 sakharuk 652: $Apache::inputtags::response[-1].':'.$temp.'">'.
653: $optionlist."</select>\n";
654: }
1.1 albertel 655: my $text=$Apache::response::foilgroup{$name.'.text'};
1.62 foxr 656: #
657: # Factor out the tex computations of the left/right 1/2 minipage
658: # widths for left or right positioning. This allows us
659: # to, if necessary constrain the bubble widths:
660: #
661: if ($target eq 'tex' &&
662: (&itemdisplay('left') || &itemdisplay('right'))) {
663: $tabsize=&Apache::londefdef::recalc($env{'form.textwidth'});
664: ($lefttabsize,$righttabsize)=(0,0);
665: if ($Apache::matchresponse::TeXitemgroupwidth ne '') {
666: $Apache::matchresponse::TeXitemgroupwidth=~/(\d*.?\d*)/;
667: $lefttabsize=$tabsize*$1/100;
668: $righttabsize=0.95*($tabsize-$lefttabsize);
669: } else {
670: $tabsize=~/(\d+\.?\d*)/;
671: $lefttabsize=$1/2.1;
672: $righttabsize=0.95*($1-$lefttabsize);
673: }
674: }
1.5 sakharuk 675: if ($target ne 'tex') {
1.19 sakharuk 676: if ($Apache::lonhomework::type ne 'exam') {
1.45 albertel 677: $question.="<br />\n".$optionlist.$text;
1.19 sakharuk 678: } else {
1.45 albertel 679: $question.="<br />\n".$text;
1.19 sakharuk 680: }
1.6 sakharuk 681: if ($Apache::lonhomework::type eq 'exam') {
1.42 albertel 682: my @blank;
683: $question.=&Apache::optionresponse::webbubbles(\@used_letters,\@blank,$temp,$last_letter);
1.6 sakharuk 684: }
685: } else {
1.62 foxr 686: # Note that if left or right positioned, we must
687: # confine the bubbles to righttabsize:
688: #
1.80 onken 689: if ($Apache::lonhomework::type eq 'exam') {
1.6 sakharuk 690: $question.=' '.$optionlist.$text."\n";
1.13 sakharuk 691: my @emptyItems = ();
1.16 albertel 692: for (my $i=0;$i<=$#used_letters;$i++) {push @emptyItems, ' ';}
1.62 foxr 693: $question.='\vskip -1 mm\noindent\begin{list}{}{\setlength{\listparindent}{0mm}\setlength{\leftmargin}{2mm}}'
1.82 ! raeburn 694: .'\item \hskip -3mm ';
! 695: if ($numrows == 1) {
! 696: $question .= '\textbf{'.$internal_counter.'}';
! 697: } else {
! 698: my $linetext;
! 699: for (my $i=0; $i<$numrows; $i++) {
! 700: $linetext .= $internal_counter+$i.', ';
! 701: }
! 702: $linetext =~ s/,\s$//;
! 703: $question .= '\small {\textbf{'.$linetext.'}} '.
! 704: '\hskip 2 mm {\footnotesize '.
! 705: &mt('(Bubble once in [_1] lines)',$numrows).
! 706: '} \vskip 3 mm';
! 707: }
! 708: my $max_width;
1.62 foxr 709: if (&itemdisplay('left') || &itemdisplay('right')) {
1.82 ! raeburn 710: $max_width = $righttabsize;
! 711: }
! 712: $question .= '\vskip -4 mm' . &Apache::optionresponse::bubbles(\@used_letters,\@emptyItems,'',$max_width,$numrows,$bubbles_per_row,$internal_counter);
1.62 foxr 713: $question .= '\end{list} \vskip -8 mm \strut ';
1.82 ! raeburn 714: $internal_counter += $numrows;
1.6 sakharuk 715: } else {
1.76 onken 716: if($env{'form.pdfFormFields'} eq 'yes'
717: && $Apache::inputtags::status['-1'] eq 'CAN_ANSWER') {
1.75 raeburn 718: $question .= " $optionlist ". &Apache::lonxml::print_pdf_end_combobox($text).'\strut';
719: } else {
720: $question.=' '.$optionlist.$text.'\strut\\\\\strut '."\n";
721: }
1.6 sakharuk 722: }
1.5 sakharuk 723: }
1.1 albertel 724: $temp++;
725: }
1.4 albertel 726: }
727: if ($result=&itemdisplay('top')) {
728: $result.=$question;
729: } elsif ($result=&itemdisplay('bottom')) {
730: $result=$question.$result;
731: } elsif ($result=&itemdisplay('right')) {
1.32 sakharuk 732: if ($target ne 'tex') {
1.45 albertel 733: #remove the first <br />
734: $question=~s|<br />||;
735: $result='<table><tr><td valign="top">'.$question.
736: '</td><td valign="top">'.$result.'</td></tr></table>';
1.32 sakharuk 737: } else {
1.43 sakharuk 738: $lefttabsize.=' mm ';
739: $righttabsize.=' mm ';
740: $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 741: }
1.4 albertel 742: } elsif ($result=&itemdisplay('left')) {
1.32 sakharuk 743: if ($target ne 'tex') {
1.45 albertel 744: #remove the first <br />
745: $question=~s|<br />||;
746: $result='<table><tr><td valign="top">'.$result.
747: '</td><td valign="top">'.$question.'</td></tr></table>';
1.32 sakharuk 748: } else {
1.43 sakharuk 749: $lefttabsize.=' mm ';
750: $righttabsize.=' mm ';
751: $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 752: }
1.1 albertel 753: }
1.64 albertel 754: if ($target eq 'web') {
1.81 raeburn 755: my $questiontype;
756: if ($Apache::lonhomework::type eq 'randomizetry') {
757: $questiontype = $Apache::lonhomework::type;
758: }
1.64 albertel 759: &Apache::response::setup_prior_tries_hash(\&format_prior_answer,
760: [\@whichfoils,
761: 'submissiongrading',
1.81 raeburn 762: 'submissionitems'],
763: $questiontype);
1.64 albertel 764: }
1.5 sakharuk 765: if ($target ne 'tex') {$result.="<br />";} else {$result.=' \\\\ ';}
1.1 albertel 766: return $result;
767: }
768:
769: sub getfoilcounts {
770: my ($max)=@_;
771: # +1 since instructors will count from 1
772: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
773: if (&Apache::response::showallfoils()) { $max=$count; }
774: if ($count>$max) { $count=$max }
775: &Apache::lonxml::debug("Count is $count from $max");
776: return $count;
777: }
778:
779:
780: sub start_conceptgroup {
781: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
782: $Apache::matchresponse::conceptgroup=1;
783: %Apache::response::conceptgroup=();
784: my $result;
785: if ($target eq 'edit') {
786: $result.=&Apache::edit::tag_start($target,$token,
787: "Concept Grouped Foils");
788: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
789: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
790: }
791: if ($target eq 'modified') {
792: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
793: $safeeval,'concept');
794: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
795: }
796: return $result;
797: }
798:
799: sub end_conceptgroup {
800: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
801: $Apache::matchresponse::conceptgroup=0;
802: my $result='';
1.28 albertel 803: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
804: $target eq 'tex' || $target eq 'analyze') {
805: &Apache::response::pick_foil_for_concept($target,
806: ['value','text','location'],
807: \%Apache::hint::match,
808: $parstack,$safeeval);
1.1 albertel 809: } elsif ($target eq 'edit') {
810: $result=&Apache::edit::end_table();
811: }
812: return $result;
813: }
814:
815: sub insert_conceptgroup {
816: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
817: return $result;
818: }
819:
820: sub start_foil {
821: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
822: my $result='';
1.28 albertel 823: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 824: &Apache::lonxml::startredirection;
1.46 albertel 825: if ($target eq 'analyze') {
826: &Apache::response::check_if_computed($token,$parstack,$safeeval,'value');
827: }
1.1 albertel 828: } elsif ($target eq 'edit') {
829: $result=&Apache::edit::tag_start($target,$token,"Foil");
830: my $level='-2';
831: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
832: $result.=&Apache::edit::text_arg('Name:','name',$token);
833: my @names;
834: if (defined(@{ $Apache::response::itemgroup{'names'} })) {
835: @names=@{ $Apache::response::itemgroup{'names'} };
836: }
837: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
838: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
839: $safeeval,'-3');
840: if ($randomize ne 'no') {
841: $result.=&Apache::edit::select_arg('Location:','location',
842: ['random','top','bottom'],$token);
843: }
844: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
845: } elsif ($target eq 'modified') {
846: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
847: $safeeval,'value',
848: 'name','location');
849: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
850: }
851: return $result;
852: }
853:
854: sub end_foil {
855: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
856: my $text ='';
857: my $result = '';
1.28 albertel 858: if ($target eq 'web' || $target eq 'tex' || $target eq 'analyze') {
1.1 albertel 859: $text=&Apache::lonxml::endredirection;
860: }
1.48 albertel 861:
1.28 albertel 862: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
863: $target eq 'tex' || $target eq 'analyze') {
1.48 albertel 864: if ($target eq 'tex' && $Apache::lonhomework::type eq 'exam') {
865: $text='\vskip 5mm $\triangleright$ '.$text;
866: }
1.1 albertel 867: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
868: if ($value ne 'unused') {
869: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
870: &Apache::lonxml::debug("Got a name of :$name:");
1.49 albertel 871: if (!$name) {
1.70 bisitz 872: &Apache::lonxml::warning(&mt('Foils without names exist. This can cause problems to malfunction.'));
1.49 albertel 873: $name=$Apache::lonxml::curdepth;
874: }
1.1 albertel 875: &Apache::lonxml::debug("Using a name of :$name:");
1.37 albertel 876: if (defined($Apache::response::foilnames{$name})) {
1.70 bisitz 877: &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 878: }
1.38 albertel 879: $Apache::response::foilnames{$name}++;
1.37 albertel 880: my $location =&Apache::lonxml::get_param('location',$parstack,
881: $safeeval);
1.1 albertel 882: if ( $Apache::matchresponse::conceptgroup
883: && !&Apache::response::showallfoils() ) {
1.77 foxr 884:
1.1 albertel 885: push @{ $Apache::response::conceptgroup{'names'} }, $name;
886: $Apache::response::conceptgroup{"$name.value"} = $value;
1.48 albertel 887: $Apache::response::conceptgroup{"$name.text"} = $text;
1.1 albertel 888: $Apache::response::conceptgroup{"$name.location"} = $location;
889: } else {
1.77 foxr 890:
1.1 albertel 891: push @{ $Apache::response::foilgroup{'names'} }, $name;
892: $Apache::response::foilgroup{"$name.value"} = $value;
1.48 albertel 893: $Apache::response::foilgroup{"$name.text"} = $text;
1.1 albertel 894: $Apache::response::foilgroup{"$name.location"} = $location;
895: }
896: }
897: }
898: if ($target eq 'edit') {
899: $result.= &Apache::edit::tag_end($target,$token,'');
900: }
901: return $result;
902: }
903:
904: sub insert_foil {
905: return '
906: <foil name="" value="unused">
907: <startouttext />
908: <endouttext />
909: </foil>';
910: }
1.82 ! raeburn 911:
1.1 albertel 912: 1;
913: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>