Annotation of loncom/homework/matchresponse.pm, revision 1.3
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # Full matching style response
3: #
1.3 ! albertel 4: # $Id: matchresponse.pm,v 1.2 2003/02/20 08:57:51 albertel 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: # 2/21 Guy
29:
30: package Apache::matchresponse;
31: use strict;
32: use HTML::Entities();
33: use Math::Random();
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::matchresponse',('matchresponse'));
37: }
38:
39: sub start_matchresponse {
40: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
41: my $result;
42: #when in a matchresponse use these
43: &Apache::lonxml::register('Apache::matchresponse',
44: ('foilgroup','foil','conceptgroup','item',
45: 'itemgroup'));
46: push (@Apache::lonxml::namespace,'matchresponse');
47: my $id = &Apache::response::start_response($parstack,$safeeval);
48: %Apache::hint::match=();
49: if ($target eq 'meta') {
50: $result=&Apache::response::meta_package_write('matchresponse');
51: } elsif ($target eq 'edit' ) {
52: $result.=&Apache::edit::start_table($token).
53: '<tr><td>'.&Apache::lonxml::description($token)."</td><td>Delete:".
54: &Apache::edit::deletelist($target,$token)
55: ."</td><td> ".&Apache::edit::end_row()
56: .&Apache::edit::start_spanning_row();
57:
58: $result.=
59: &Apache::edit::text_arg('Max Number Of Shown Foils:','max',$token,'4').
60: &Apache::edit::select_arg('Randomize Foil Order','randomize',
61: ['yes','no'],$token).
62: &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n";
63: } elsif ($target eq 'modified') {
64: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
65: $safeeval,'max',
66: 'randomize');
67: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
68: }
69: return $result;
70: }
71:
72: sub end_matchresponse {
73: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
74: my $result;
75: if ($target eq 'edit') { $result=&Apache::edit::end_table(); }
76: &Apache::response::end_response;
77: pop @Apache::lonxml::namespace;
78: &Apache::lonxml::deregister('Apache::matchresponse',
79: ('foilgroup','foil','conceptgroup'));
80: return $result;
81: }
82:
83: %Apache::response::itemgroup=();
84: sub start_itemgroup {
85: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
86: my $result;
87: %Apache::response::itemgroup=();
88: if ($target eq 'edit') {
89: $result=&Apache::edit::tag_start($target,$token);
90: $result.=&Apache::edit::select_arg('Randomize Order:','randomize',
91: ['yes','no'],$token);
92: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
93: }
94: return $result;
95: }
96:
97: sub end_itemgroup {
98: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
99: my $result;
100:
101: if (!defined(@{ $Apache::response::itemgroup{'names'} })) { return; }
102: my @names=@{ $Apache::response::itemgroup{'names'} };
103: my $randomize =&Apache::lonxml::get_param('randomize',$parstack,$safeeval);
1.2 albertel 104: if ($randomize ne 'no' ) {
1.3 ! albertel 105: @names=&whichorder($#names+1,$randomize,0,
! 106: \%Apache::response::itemgroup)
1.1 albertel 107: }
108: my %letter_name_map;
109: my %name_letter_map;
110: my @alphabet=('A'..'Z');
111: my $i=0;
112: foreach my $name (@names) {
113: $letter_name_map{$alphabet[$i]}=$name;
114: $name_letter_map{$name}=$alphabet[$i];
115: $i++;
116: }
117: $Apache::response::itemgroup{'letter_name_map'}=\%letter_name_map;
118: $Apache::response::itemgroup{'name_letter_map'}=\%name_letter_map;
119: if ($target eq 'web') {
120: $result.='<table>';
121: my $i=0;
122: foreach my $name (@names) {
123: $result.='<tr><td>'.$alphabet[$i].'</td><td>'.
124: $Apache::response::itemgroup{$name.'.text'}.
125: '</td></tr>';
126: $i++;
127: }
128: $result.='</table>';
129: } elsif ($target eq 'edit') { $result=&Apache::edit::end_table(); }
130: return $result;
131: }
132:
133: sub start_item {
134: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
135: my $result='';
136: if ($target eq 'web' ) {
137: &Apache::lonxml::startredirection;
138: } elsif ($target eq 'edit') {
1.3 ! albertel 139: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
! 140: $safeeval,'-2');
1.1 albertel 141: $result=&Apache::edit::tag_start($target,$token,"Item");
142: $result.=&Apache::edit::text_arg('Name:','name',$token);
1.3 ! albertel 143: if ($randomize ne 'no') {
! 144: $result.=&Apache::edit::select_arg('Location:','location',
! 145: ['random','top','bottom'],
! 146: $token);
! 147: }
! 148: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
1.1 albertel 149: } elsif ($target eq 'modified') {
150: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.3 ! albertel 151: $safeeval,'name',
! 152: 'location');
1.1 albertel 153: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
154: }
155: return $result;
156: }
157:
158: sub end_item {
159: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
160: my $text ='';
161: my $result = '';
162: if ($target eq 'web') {
163: $text=&Apache::lonxml::endredirection;
164: }
165: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
166: $target eq 'edit') {
167: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
1.3 ! albertel 168: my $location=&Apache::lonxml::get_param('location',$parstack,
! 169: $safeeval);
1.1 albertel 170: &Apache::lonxml::debug("Got a name of :$name:");
171: if (!$name) { $name=$Apache::lonxml::curdepth; }
172: &Apache::lonxml::debug("Using a name of :$name:");
173: push @{ $Apache::response::itemgroup{'names'} }, $name;
174: $Apache::response::itemgroup{"$name.text"} = $text;
1.3 ! albertel 175: $Apache::response::itemgroup{"$name.location"} = $location;
1.1 albertel 176: }
177: if ($target eq 'edit') {
178: $result.= &Apache::edit::tag_end($target,$token,'');
179: }
180: return $result;
181: }
182:
183: sub insert_item {
184: return '
185: <item name="">
186: <startouttext />
187: <endouttext />
188: </item>';
189: }
190:
191: %Apache::response::foilgroup=();
192: sub start_foilgroup {
193: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
194: my $result;
195: %Apache::response::foilgroup=();
196: $Apache::matchresponse::conceptgroup=0;
197: &Apache::response::setrandomnumber();
198: if ($target eq 'edit') {
199: $result.=&Apache::edit::start_table($token)
200: ."<tr><td>Collection Of Foils</td><td>Delete:"
201: .&Apache::edit::deletelist($target,$token)
202: ."</td><td> ".&Apache::edit::end_row()
203: .&Apache::edit::start_spanning_row()."\n";
204: }
205: return $result;
206: }
207:
208: sub end_foilgroup {
209: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
210: my $result;
211: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer') {
212: my $max = &Apache::lonxml::get_param('max',$parstack,$safeeval,'-2');
213: my $randomize = &Apache::lonxml::get_param('randomize',$parstack,
214: $safeeval,'-2');
215: if ($target eq 'web') {
216: $result=&displayfoils($target,$max,$randomize);
217: } elsif ($target eq 'answer' ) {
218: $result=&displayanswers($max,$randomize);
219: } elsif ( $target eq 'grade') {
220: &grade_response($max,$randomize);
221: }
222: } elsif ($target eq 'edit') {
223: $result=&Apache::edit::end_table();
224: }
225: return $result;
226: }
227:
228: sub displayanswers {
229: my ($max,$randomize,@opt)=@_;
230: if (!defined(@{ $Apache::response::foilgroup{'names'} })) { return; }
231: my @names = @{ $Apache::response::foilgroup{'names'} };
1.3 ! albertel 232: my @whichfoils = &whichorder(&getfoilcounts($max),$randomize,
! 233: &Apache::response::showallfoils(),
! 234: \%Apache::response::foilgroup);
1.1 albertel 235: my $result=&Apache::response::answer_header('matchresponse');
236: my %name_letter_map;
237: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
238: %name_letter_map=
239: %{ $Apache::response::itemgroup{'name_letter_map'} };
240: }
241: foreach my $name (@whichfoils) {
242: my $value_name=$Apache::response::foilgroup{$name.'.value'};
243: my $letter=$name_letter_map{$value_name};
244: $result.=&Apache::response::answer_part('matchresponse',$letter);
245: }
246: $result.=&Apache::response::answer_footer('matchresponse');
247: return $result;
248: }
249:
250:
251: sub grade_response {
252: my ($max,$randomize)=@_;
1.3 ! albertel 253: my (@whichfoils)=&whichorder(&getfoilcounts($max),$randomize,
! 254: &Apache::response::showallfoils(),
! 255: \%Apache::response::foilgroup);
1.1 albertel 256: if (!defined($ENV{'form.submitted'})) { return; }
257: my %responsehash;
258: my %grade;
259: my ($temp,$right,$wrong,$ignored)=(0,0,0,0);
260: my %letter_name_map;
261: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
262: %letter_name_map=
263: %{ $Apache::response::itemgroup{'letter_name_map'} };
264: }
265: foreach my $name (@whichfoils) {
266: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
267: my $responsename = $letter_name_map{$response};
268: $responsehash{$name}=$responsename;
269: my $value=$Apache::response::foilgroup{$name.'.value'};
270: if ( $response =~ /[^\s]/) {
271: &Apache::lonxml::debug("submitted a $response for $value<br />\n");
272: if ($value eq $responsename) {
273: $grade{$name}='1'; $right++;
274: } else {
275: $grade{$name}='0'; $wrong++;
276: }
277: } else {
278: $ignored++;
279: }
280: $temp++;
281: }
282: my $part=$Apache::inputtags::part;
283: my $id = $Apache::inputtags::response['-1'];
284: my $responsestr=&Apache::lonnet::hash2str(%responsehash);
285: my $gradestr =&Apache::lonnet::hash2str(%grade);
286: my %previous =&Apache::response::check_for_previous($responsestr,
287: $part,$id);
288: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored ");
289: my $ad;
290: if ($wrong==0 && $ignored==0) {
291: $ad='EXACT_ANS';
292: } elsif ($wrong==0 && $right==0) {
293: #nothing submitted
294: } else {
295: if ($ignored==0) {
296: $ad='INCORRECT';
297: } else {
298: $ad='MISSING_ANSWER';
299: }
300: }
301: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
302: $responsestr;
303: $Apache::lonhomework::results{"resource.$part.$id.submissiongrading"}=
304: $gradestr;
305: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
306: &Apache::response::handle_previous(\%previous,$ad);
307: }
308:
309: sub displayfoils {
310: my ($target,$max,$randomize)=@_;
311: my $result;
1.3 ! albertel 312: my (@whichfoils)=&whichorder(&getfoilcounts($max),$randomize,
! 313: &Apache::response::showallfoils(),
! 314: \%Apache::response::foilgroup);
1.1 albertel 315: my $part=$Apache::inputtags::part;
316: my $solved=$Apache::lonhomework::history{"resource.$part.solved"};
317: my $status=$Apache::inputtags::status[-1];
318: my %letter_name_map;
319: if (defined(%{ $Apache::response::itemgroup{'letter_name_map'} })) {
320: %letter_name_map=
321: %{ $Apache::response::itemgroup{'letter_name_map'} };
322: }
323: my %name_letter_map;
324: if (defined(%{ $Apache::response::itemgroup{'name_letter_map'} })) {
325: %name_letter_map=
326: %{ $Apache::response::itemgroup{'name_letter_map'} };
327: }
328: if (($solved =~ /^correct/) || ($status eq 'SHOW_ANSWER')) {
329: foreach my $name (@whichfoils) {
330: my $text=$Apache::response::foilgroup{$name.'.text'};
331: my $value=$Apache::response::foilgroup{$name.'.value'};
332: my $letter=$name_letter_map{$value};
333: $result.='<br />'.$letter.':'.$text;
334: }
335: } else {
336: my $i = 0;
337: my $temp=0;
338: my $id=$Apache::inputtags::response[-1];
339: my $part=$Apache::inputtags::part;
340: my $lastresponse=$Apache::lonhomework::history{"resource.$part.$id.submission"};
341: my %lastresponse=&Apache::lonnet::str2hash($lastresponse);
342: foreach my $name (@whichfoils) {
343: my $lastopt=$lastresponse{$name};
344: my $last_letter=$name_letter_map{$lastopt};
345: my $optionlist="<option></option>\n";
346: my $option;
347: foreach $option (sort(keys(%letter_name_map))) {
348: if ($option eq $last_letter) {
349: $optionlist.="<option selected=\"on\">$option</option>\n";
350: } else {
351: $optionlist.="<option>$option</option>\n";
352: }
353: }
354: $optionlist='<select name="HWVAL_'.
355: $Apache::inputtags::response[-1].':'.$temp.'">'.
356: $optionlist."</select>\n";
357: my $text=$Apache::response::foilgroup{$name.'.text'};
358: $result.='<br />'.$optionlist.$text."\n";
359: $temp++;
360: }
361: }
362: $result.="<br />";
363: return $result;
364: }
365:
366: sub getfoilcounts {
367: my ($max)=@_;
368: # +1 since instructors will count from 1
369: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
370: if (&Apache::response::showallfoils()) { $max=$count; }
371: if ($count>$max) { $count=$max }
372: &Apache::lonxml::debug("Count is $count from $max");
373: return $count;
374: }
375:
1.3 ! albertel 376: sub whichorder {
! 377: my ($max,$randomize,$showall,$hash)=@_;
! 378: #&Apache::lonxml::debug("man $max randomize $randomize");
! 379: if (!defined(@{ $$hash{'names'} })) { return; }
! 380: my @names = @{ $$hash{'names'} };
1.1 albertel 381: my @whichopt =();
382: my (%top,@toplist,%bottom,@bottomlist);
1.3 ! albertel 383: if (!($showall || ($randomize eq 'no'))) {
1.1 albertel 384: my $current=0;
385: foreach my $name (@names) {
386: $current++;
1.3 ! albertel 387: if ($$hash{"$name.location"} eq 'top') {
1.1 albertel 388: $top{$name}=$current;
1.3 ! albertel 389: } elsif ($$hash{"$name.location"} eq 'bottom') {
1.1 albertel 390: $bottom{$name}=$current;
391: }
392: }
393: }
394: while ((($#whichopt+1) < $max) && ($#names > -1)) {
1.3 ! albertel 395: #&Apache::lonxml::debug("Have $#whichopt max is $max");
1.1 albertel 396: my $aopt;
1.3 ! albertel 397: if ($showall || ($randomize eq 'no')) {
1.1 albertel 398: $aopt=0;
399: } else {
400: $aopt=int(&Math::Random::random_uniform() * ($#names+1));
401: }
1.3 ! albertel 402: #&Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1.1 albertel 403: $aopt=splice(@names,$aopt,1);
1.3 ! albertel 404: #&Apache::lonxml::debug("Picked $aopt");
1.1 albertel 405: if ($top{$aopt}) {
406: $toplist[$top{$aopt}]=$aopt;
407: } elsif ($bottom{$aopt}) {
408: $bottomlist[$bottom{$aopt}]=$aopt;
409: } else {
410: push (@whichopt,$aopt);
411: }
412: }
413: for (my $i=0;$i<=$#toplist;$i++) {
414: if ($toplist[$i]) { unshift(@whichopt,$toplist[$i]) }
415: }
416: for (my $i=0;$i<=$#bottomlist;$i++) {
417: if ($bottomlist[$i]) { push(@whichopt,$bottomlist[$i]) }
418: }
419:
420: return @whichopt;
421: }
422:
423: sub start_conceptgroup {
424: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
425: $Apache::matchresponse::conceptgroup=1;
426: %Apache::response::conceptgroup=();
427: my $result;
428: if ($target eq 'edit') {
429: $result.=&Apache::edit::tag_start($target,$token,
430: "Concept Grouped Foils");
431: $result.=&Apache::edit::text_arg('Concept:','concept',$token,'50').
432: &Apache::edit::end_row().&Apache::edit::start_spanning_row();
433: }
434: if ($target eq 'modified') {
435: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
436: $safeeval,'concept');
437: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
438: }
439: return $result;
440: }
441:
442: sub end_conceptgroup {
443: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
444: $Apache::matchresponse::conceptgroup=0;
445: my $result='';
446: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ) {
447: #if not there aren't any foils to display and thus no question
448: if (defined(@{ $Apache::response::conceptgroup{'names'} })) {
449: my @names = @{ $Apache::response::conceptgroup{'names'} };
450: my $pick=int(&Math::Random::random_uniform() * ($#names+1));
451: my $name=$names[$pick];
452: push @{ $Apache::response::foilgroup{'names'} }, $name;
453: $Apache::response::foilgroup{"$name.value"} =
454: $Apache::response::conceptgroup{"$name.value"};
455: $Apache::response::foilgroup{"$name.text"} =
456: $Apache::response::conceptgroup{"$name.text"};
457: $Apache::response::foilgroup{"$name.location"} =
458: $Apache::response::conceptgroup{"$name.location"};
459: my $concept = &Apache::lonxml::get_param('concept',$parstack,
460: $safeeval);
461: $Apache::response::foilgroup{"$name.concept"} = $concept;
462: &Apache::lonxml::debug("Selecting $name in $concept");
463: if ($target eq 'web') {
464: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
465: push(@{ $Apache::hint::match{"$part_id.concepts"} },
466: $concept);
467: $Apache::hint::match{"$part_id.concept.$concept"}=
468: $Apache::response::conceptgroup{'names'};
469: }
470: }
471: } elsif ($target eq 'edit') {
472: $result=&Apache::edit::end_table();
473: }
474: return $result;
475: }
476:
477: sub insert_conceptgroup {
478: my $result="\n\t\t<conceptgroup concept=\"\">".&insert_foil()."\n\t\t</conceptgroup>\n";
479: return $result;
480: }
481:
482: sub start_foil {
483: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
484: my $result='';
485: if ($target eq 'web' ) {
486: &Apache::lonxml::startredirection;
487: } elsif ($target eq 'edit') {
488: $result=&Apache::edit::tag_start($target,$token,"Foil");
489: my $level='-2';
490: if ($$tagstack[-2] eq 'conceptgroup') { $level = '-3'; }
491: $result.=&Apache::edit::text_arg('Name:','name',$token);
492: my @names;
493: if (defined(@{ $Apache::response::itemgroup{'names'} })) {
494: @names=@{ $Apache::response::itemgroup{'names'} };
495: }
496: $result.= &Apache::edit::select_or_text_arg('Correct Option:','value',['unused',@names],$token,'15');
497: my $randomize=&Apache::lonxml::get_param('randomize',$parstack,
498: $safeeval,'-3');
499: if ($randomize ne 'no') {
500: $result.=&Apache::edit::select_arg('Location:','location',
501: ['random','top','bottom'],$token);
502: }
503: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
504: } elsif ($target eq 'modified') {
505: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
506: $safeeval,'value',
507: 'name','location');
508: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
509: }
510: return $result;
511: }
512:
513: sub end_foil {
514: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
515: my $text ='';
516: my $result = '';
517: if ($target eq 'web') {
518: $text=&Apache::lonxml::endredirection;
519: }
520: if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
521: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
522: if ($value ne 'unused') {
523: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
524: my $location =&Apache::lonxml::get_param('location',$parstack,$safeeval);
525: &Apache::lonxml::debug("Got a name of :$name:");
526: if (!$name) { $name=$Apache::lonxml::curdepth; }
527: &Apache::lonxml::debug("Using a name of :$name:");
528: if ( $Apache::matchresponse::conceptgroup
529: && !&Apache::response::showallfoils() ) {
530: push @{ $Apache::response::conceptgroup{'names'} }, $name;
531: $Apache::response::conceptgroup{"$name.value"} = $value;
532: $Apache::response::conceptgroup{"$name.text"} = $text;
533: $Apache::response::conceptgroup{"$name.location"} = $location;
534: } else {
535: push @{ $Apache::response::foilgroup{'names'} }, $name;
536: $Apache::response::foilgroup{"$name.value"} = $value;
537: $Apache::response::foilgroup{"$name.text"} = $text;
538: $Apache::response::foilgroup{"$name.location"} = $location;
539: }
540: }
541: }
542: if ($target eq 'edit') {
543: $result.= &Apache::edit::tag_end($target,$token,'');
544: }
545: return $result;
546: }
547:
548: sub insert_foil {
549: return '
550: <foil name="" value="unused">
551: <startouttext />
552: <endouttext />
553: </foil>';
554: }
555: 1;
556: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>