Annotation of loncom/homework/optionresponse.pm, revision 1.9
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # option list style responses
1.4 albertel 3: # 2/21 Guy
1.1 albertel 4: package Apache::optionresponse;
5: use strict;
1.6 albertel 6: use Apache::response;
1.1 albertel 7:
8: sub BEGIN {
9: &Apache::lonxml::register('Apache::optionresponse',('optionresponse'));
10: }
11:
12: sub start_optionresponse {
13: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.9 ! albertel 14: my $result='';
1.1 albertel 15: #when in a radiobutton response use these
1.2 albertel 16: &Apache::lonxml::register('Apache::optionresponse',('foilgroup','foil','conceptgroup'));
1.7 albertel 17: push (@Apache::lonxml::namespace,'optionresponse');
1.1 albertel 18: my $id = &Apache::response::start_response($parstack,$safeeval);
1.9 ! albertel 19: if ($target eq 'edit') {
! 20: $result.="<table width=\"100%\" border=\"2\"><tr><td>Multiple Option Response Question</td>
! 21: <td>Delete:".
! 22: &Apache::edit::deletelist($target,$token)
! 23: ."</td></tr><tr><td colspan=\"3\">\n";
! 24: }
! 25:
! 26: return $result;
1.1 albertel 27: }
28:
29: sub end_optionresponse {
30: &Apache::response::end_response;
1.7 albertel 31: pop @Apache::lonxml::namespace;
1.1 albertel 32: return '';
33: }
34:
1.7 albertel 35: sub insert_optionresponse {
36: return '
37: <optionresponse max="10">
38: <foilgroup options=\"\">
39: </foilgroup>
40: </optionresponse>';
41: }
42:
1.1 albertel 43: %Apache::response::foilgroup={};
44: sub start_foilgroup {
45: %Apache::response::foilgroup={};
1.9 ! albertel 46: $Apache::optionresponse::conceptgroup=0;
1.6 albertel 47: &Apache::response::setrandomnumber();
1.1 albertel 48: return '';
49: }
50:
51: sub end_foilgroup {
52: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
53:
54: my $result;
55: if ($target ne 'meta') {
56: my $name;
57: my ($count,$max) = &getfoilcounts($parstack,$safeeval);
58: if ($count>$max) { $count=$max }
59: &Apache::lonxml::debug("Count is $count from $max");
60: my $args ='';
61: if ( $#$parstack > 0 ) { $args=$$parstack['-1']; }
62: my @opt;
63: eval '@opt ='.&Apache::run::run("{$args;".'return $options}',$safeeval);
64: if ($target eq 'web') {
65: $result=&displayfoils($count,@opt);
66: } elsif ( $target eq 'grade') {
67: if ( defined $ENV{'form.submitted'}) {
68: my @whichopt = &whichfoils($count);
69: my $temp=1;my $name;
70: my $allresponse;
71: my $right=0;
72: my $wrong=0;
1.8 albertel 73: my $ignored=0;
1.1 albertel 74: foreach $name (@whichopt) {
75: my $response = $ENV{'form.HWVAL_'.$Apache::inputtags::response['-1'].":$temp"};
76: $allresponse.="$response:";
77: if ( $response =~ /[^\s]/) {
1.3 albertel 78: &Apache::lonxml::debug("submitted a $response<br />\n");
1.1 albertel 79: my $value=$Apache::response::foilgroup{$name.'.value'};
80: if ($value eq $response) {$right++;} else {$wrong++;}
1.8 albertel 81: } else {
82: $ignored++;
1.1 albertel 83: }
84: $temp++;
85: }
86: my $id = $Apache::inputtags::response['-1'];
87: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$allresponse;
1.8 albertel 88: &Apache::lonxml::debug("Got $right right and $wrong wrong, and $ignored were ignored");
89: if ($wrong==0 && $ignored==0) {
1.1 albertel 90: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='EXACT_ANS';
91: } else {
92: $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}='INCORRECT';
93: }
94: }
95: }
96: }
97: return $result;
98: }
99:
100: sub getfoilcounts {
101: my ($parstack,$safeeval)=@_;
102: my $rrargs ='';
103: if ( $#$parstack > 0 ) { $rrargs=$$parstack['-2']; }
104: my $max = &Apache::run::run("{$rrargs;".'return $max}',$safeeval);
1.9 ! albertel 105: # +1 since instructors will count from 1
! 106: my $count = $#{ $Apache::response::foilgroup{'names'} }+1;
1.1 albertel 107: return ($count,$max);
108: }
109:
110: sub whichfoils {
111: my ($max)=@_;
112: my @names = @{ $Apache::response::foilgroup{'names'} };
113: my @whichopt =();
1.9 ! albertel 114: while ((($#whichopt+1) < $max) && ($#names > -1)) {
! 115: &Apache::lonxml::debug("Have $#whichopt max is $max");
! 116: my $aopt=int(rand($#names+1));
1.6 albertel 117: &Apache::lonxml::debug("From $#whichopt $max $#names elms, picking $aopt");
1.1 albertel 118: $aopt=splice(@names,$aopt,1);
119: &Apache::lonxml::debug("Picked $aopt");
120: push (@whichopt,$aopt);
121: }
122: return @whichopt;
123: }
124:
125: sub displayfoils {
126: my ($max,@opt)=@_;
127: my @names = @{ $Apache::response::foilgroup{'names'} };
128: my @truelist;
129: my @falselist;
130: my $result;
131: my $name;
132: my @whichopt = &whichfoils($max);
133: my $optionlist="<option></option>\n";
134: my $option;
135: foreach $option (@opt) {
136: $optionlist.="<option>$option</option>\n";
137: }
138: if ($Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"} =~ /^correct/ ) {
139: foreach $name (@whichopt) {
140: $result.="<br />".$Apache::response::foilgroup{$name.'.value'}.
141: ":".$Apache::response::foilgroup{$name.'.text'}."\n";
142: }
143: } else {
144: my $temp=1;
145: foreach $name (@whichopt) {
146: $result.="<br /><select name=\"HWVAL_$Apache::inputtags::response['-1']:$temp\">"
147: .$optionlist
148: ."</select>\n".$Apache::response::foilgroup{$name.'.text'}."\n";
149: $temp++;
150: }
151: }
152: return $result."<br />";
153: }
154:
1.5 albertel 155:
1.2 albertel 156: sub start_conceptgroup {
1.5 albertel 157: $Apache::optionresponse::conceptgroup=1;
158: %Apache::response::conceptgroup={};
159: return '';
1.2 albertel 160: }
161:
162: sub end_conceptgroup {
1.5 albertel 163: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
164: $Apache::optionresponse::conceptgroup=0;
165: if ($target eq 'web' || $target eq 'grade') {
166: my @names = @{ $Apache::response::conceptgroup{'names'} };
167: my $pick=int rand $#names+1;
168: my $name=$names[$pick];
169: push @{ $Apache::response::foilgroup{'names'} }, $name;
170: $Apache::response::foilgroup{"$name.value"} = $Apache::response::conceptgroup{"$name.value"};
171: $Apache::response::foilgroup{"$name.text"} = $Apache::response::conceptgroup{"$name.text"};
172: my $args;
173: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
174: my $concept = &Apache::run::run("{$args;".'return $concept}',$safeeval);
175: $Apache::response::foilgroup{"$name.concept"} = $concept;
176: &Apache::lonxml::debug("Selecting $name in $concept");
177: }
178: return '';
1.2 albertel 179: }
180:
1.1 albertel 181: sub start_foil {
1.2 albertel 182: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.4 albertel 183: if ($target eq 'web') { &Apache::lonxml::startredirection; }
1.1 albertel 184: return '';
185: }
186:
187: sub end_foil {
188: my ($target,$token,$parstack,$parser,$safeeval,$style)=@_;
1.4 albertel 189: my $text ='';
190: if ($target eq 'web') { $text=&Apache::lonxml::endredirection; }
1.1 albertel 191: if ($target eq 'web' || $target eq 'grade') {
192: my $args ='';
193: if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
194: my $value = &Apache::run::run("{$args;".'return $value}',$safeeval);
195: if ($value ne 'unused') {
196: my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
1.5 albertel 197: if ( $Apache::optionresponse::conceptgroup ) {
198: push @{ $Apache::response::conceptgroup{'names'} }, $name;
199: $Apache::response::conceptgroup{"$name.value"} = $value;
200: $Apache::response::conceptgroup{"$name.text"} = $text;
201: } else {
202: push @{ $Apache::response::foilgroup{'names'} }, $name;
203: $Apache::response::foilgroup{"$name.value"} = $value;
204: $Apache::response::foilgroup{"$name.text"} = $text;
205: }
1.2 albertel 206: }
1.6 albertel 207: }
208: if ($target eq 'edit') {
209: my $args ='';
210: if ( $#$parstack > 1 ) { $args=$$parstack['-2']; }
211: my $options=&Apache::run::run("{$args;".'return $options}',$safeeval);
212: if (!$options && $#$parstack > 2 ) { $args=$$parstack['-2']; }
213: my @opt;
214: #eval '@opt ='.
1.1 albertel 215: }
216: return '';
217: }
218:
1.7 albertel 219: sub insert_foil {
220: return '
221: <foil name="" value="">
222: </foil>';
223: }
1.1 albertel 224: 1;
225: __END__
226:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>