Annotation of loncom/homework/response.pm, revision 1.63
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.63 ! albertel 4: # $Id: response.pm,v 1.62 2002/05/03 19:32:57 albertel Exp $
1.53 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: #
1.7 www 28: # 11/23,11/24,11/28 Gerd Kortemeyer
1.38 albertel 29: # Guy Albertelli
1.39 www 30: # 08/04,08/07 Gerd Kortemeyer
1.5 www 31:
1.1 albertel 32: package Apache::response;
33: use strict;
34:
1.57 harris41 35: BEGIN {
1.63 ! albertel 36: &Apache::lonxml::register('Apache::response',('responseparam','parameter'));
1.1 albertel 37: }
38:
1.13 albertel 39: sub start_response {
1.41 albertel 40: my ($parstack,$safeeval)=@_;
1.38 albertel 41: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
42: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
1.51 albertel 43: if ($#Apache::inputtags::import > -1) {
44: &Apache::lonxml::debug("Turning :$id: into");
45: $id = join('_',@Apache::inputtags::import).'_'.$id;
46: &Apache::lonxml::debug("New :$id:");
47: }
1.38 albertel 48: push (@Apache::inputtags::response,$id);
49: push (@Apache::inputtags::responselist,$id);
50: @Apache::inputtags::inputlist=();
51: return $id;
1.13 albertel 52: }
53:
54: sub end_response {
55: pop @Apache::inputtags::response;
1.38 albertel 56: @Apache::inputtags::inputlist=();
1.13 albertel 57: return '';
58: }
59:
1.41 albertel 60: sub start_hintresponse {
61: my ($parstack,$safeeval)=@_;
62: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
63: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
64: push (@Apache::inputtags::response,$id);
1.60 albertel 65: push (@Apache::inputtags::paramstack,[%Apache::inputtags::params]);
1.41 albertel 66: return $id;
67: }
68:
69: sub end_hintresponse {
70: pop @Apache::inputtags::response;
1.60 albertel 71: %Apache::inputtags::params=@{pop(@Apache::inputtags::paramstack)};
1.41 albertel 72: return '';
73: }
74:
1.38 albertel 75: # used by response to set the non-safe space random number generator to something
76: # that is stable and unique based on the part number and response number
1.26 albertel 77: sub setrandomnumber {
1.48 albertel 78: my $rndseed;
79: if ($ENV{'request.state'} eq "construct") {
80: $rndseed=$ENV{'form.rndseed'};
81: if (!$rndseed) { $rndseed=time; }
82: } else {
1.59 albertel 83: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
84: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.48 albertel 85: }
1.38 albertel 86: &Apache::lonxml::debug("randseed $rndseed");
87: # $rndseed=unpack("%32i",$rndseed);
88: $rndseed=$rndseed
89: +(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
90: if (defined($Apache::inputtags::response['-1'])) {
91: $rndseed=$rndseed
92: +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
93: }
94: srand($rndseed);
95: &Apache::lonxml::debug("randseed $rndseed");
96: return '';
1.26 albertel 97: }
98:
1.7 www 99: sub meta_parameter_write {
1.38 albertel 100: my ($name,$type,$default,$display)=@_;
1.41 albertel 101: my $partref=$Apache::inputtags::part;
102: my $result='<parameter part="'.$Apache::inputtags::part.'"';
103: if (defined($Apache::inputtags::response[-1])) {
104: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
105: $partref.='_'.$Apache::inputtags::response[-1];
106: }
107: $result.= ' name="'.$name.'"'.
108: ' type="'.$type.'"'.
109: ($default?' default="'.$default.'"':'').
110: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
111: .'></parameter>'
112: ."\n";
113: return $result;
1.33 www 114: }
115:
116: sub meta_package_write {
117: my $name=shift;
1.41 albertel 118: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
119: if(defined($Apache::inputtags::response[-1])) {
120: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
121: }
122: $result.=' package="'.$name.'"></parameter>'."\n";
123: return $result;
1.7 www 124: }
125:
126: sub meta_stores_write {
1.10 www 127: my ($name,$type,$display)=@_;
1.41 albertel 128: my $partref=$Apache::inputtags::part;
129: my $result = '<stores part="'.$Apache::inputtags::part.'"';
130: if (defined($Apache::inputtags::response[-1])) {
131: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
132: $partref.='_'.$Apache::inputtags::response[-1];
133: }
134: $result.= ' name="'.$name.'"'.
135: ' type="'.$type.'"'.
136: ' display="'.$display.' [Part: '.$partref.']"'.
137: "></stores>\n";
1.7 www 138: }
139:
140: sub mandatory_part_meta {
141: #
142: # Autogenerate metadata for mandatory
143: # input (from RAT or lonparmset) and
144: # output (to lonspreadsheet)
145: # of each part
146: #
1.38 albertel 147: return
1.34 www 148: # &meta_parameter_write('opendate','date_start','',
149: # 'Opening Date').
150: # &meta_parameter_write('duedate','date_end','',
151: # 'Due Date').
152: # &meta_parameter_write('answerdate','date_start','',
153: # 'Show Answer Date').
154: # &meta_parameter_write('weight','int_zeropos','',
155: # 'Available Points').
156: # &meta_parameter_write('maxtries','int_pos','',
157: # 'Maximum Number of Tries').
1.39 www 158: &meta_package_write('part').
1.10 www 159: &meta_stores_write('solved','string',
1.7 www 160: 'Problem Status').
1.38 albertel 161: &meta_stores_write('tries','int_zeropos',
1.24 albertel 162: 'Number of Attempts').
1.38 albertel 163: &meta_stores_write('awarded','float',
1.7 www 164: 'Partial Credit Factor');
165: #
166: # Note: responseid-specific data 'submission' and 'awarddetail'
167: # not available to spreadsheet -> skip here
168: #
1.14 albertel 169: }
170:
1.15 albertel 171: sub check_for_previous {
1.38 albertel 172: my ($curresponse,$partid,$id) = @_;
173: my %previous;
174: $previous{'used'} = 0;
175: foreach my $key (reverse(sort(keys(%Apache::lonhomework::history)))) {
176: if ($key =~ /resource\.$partid\.$id\.submission/) {
177: &Apache::lonxml::debug("Trying $key");
178: my $pastresponse=$Apache::lonhomework::history{$key};
179: if ($pastresponse eq $curresponse) {
180: $previous{'used'} = 1;
181: my $history;
182: if ( $key =~ /^(\d+):/ ) {
183: $history=$1;
184: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
185: $previous{'last'}='0';
186: } else {
187: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
188: $previous{'last'}='1';
1.32 albertel 189: }
1.38 albertel 190: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
191: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
192: last;
193: }
1.32 albertel 194: }
1.38 albertel 195: }
196: return %previous;
1.54 albertel 197: }
198:
199: sub handle_previous {
200: my ($previous,$ad)=@_;
201: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
202: if ($$previous{'last'}) {
203: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
204: } else {
205: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
206: }
207: }
1.44 albertel 208: }
209:
1.45 albertel 210: sub view_or_modify {
1.47 albertel 211: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.45 albertel 212: my $myself=0;
1.46 albertel 213: if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
1.45 albertel 214: $myself=1;
215: }
216: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
217: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
218: if ($mgr) { return "M"; }
219: if ($vgr) { return "V"; }
220: if ($myself) { return "V"; }
221: return '';
222: }
223:
1.44 albertel 224: sub start_dataresponse {
225: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
226: my $id = &Apache::response::start_response($parstack,$safeeval);
227: my $result;
1.45 albertel 228: if ($target eq 'web') {
229: $result = $token->[2]->{'display'}.':';
230: } elsif ($target eq 'meta') {
1.44 albertel 231: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
232: $token->[2]->{'type'},
233: $token->[2]->{'display'});
234: $result .= &Apache::response::meta_package_write('dataresponse');
235: }
236: return $result;
237: }
238:
239: sub end_dataresponse {
240: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
241: my $result;
242: if ( $target eq 'web' ) {
243: } elsif ($target eq 'grade' ) {
244: if ( defined $ENV{'form.submitted'}) {
1.47 albertel 245: my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
1.45 albertel 246: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
247: if ($allowed) {
248: &Apache::response::setup_params('datasubmission');
249: my $partid = $Apache::inputtags::part;
250: my $id = $Apache::inputtags::response['-1'];
251: my $response = $ENV{'form.HWVAL'.$id};
252: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
253: if ( $response =~ /[^\s]/) {
254: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
255: $response;
256: $Apache::lonhomework::results{
257: "resource.$partid.$id.submission"}=
258: $response;
259: $Apache::lonhomework::results{
260: "resource.$partid.$id.awarddetail"}=
261: 'SUBMITTED';
262: }
263: } else {
264: $result='Not Permitted to change values.'
1.44 albertel 265: }
266: }
267: }
268: &Apache::response::end_response;
269: return $result;
1.3 albertel 270: }
271:
272: sub start_responseparam {
1.38 albertel 273: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.43 albertel 274: my $result='';
1.42 albertel 275: if ($target eq 'meta') {
276: $result = &meta_parameter_write($token->[2]->{'name'},
277: $token->[2]->{'type'},
278: $token->[2]->{'default'},
279: $token->[2]->{'description'});
1.38 albertel 280: } elsif ($target eq 'edit') {
1.56 matthew 281: $result.=&Apache::edit::tag_start($target,$token);
1.38 albertel 282: $result.=&Apache::edit::text_arg('Name:','name',$token).
283: &Apache::edit::text_arg('Type:','type',$token).
284: &Apache::edit::text_arg('Description:','description',$token).
285: &Apache::edit::text_arg('Default:','default',$token).
286: "</td></tr>";
287: $result.=&Apache::edit::end_table;
288: } elsif ($target eq 'modified') {
289: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
290: 'name','type','description',
291: 'default');
292: if ($constructtag) {
293: $result = &Apache::edit::rebuild_tag($token);
294: $result.=&Apache::edit::handle_insert();
295: }
1.60 albertel 296: } elsif ($target eq 'grade' || $target eq 'answer' ||
297: $target eq 'web' || $target eq 'tex') {
1.52 albertel 298: if ($ENV{'request.state'} eq 'construct') {
299: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
300: my $default = &Apache::lonxml::get_param('default',$parstack,$safeeval);
1.61 albertel 301: if ($name) {$Apache::inputtags::params{$name}=$default;}
1.52 albertel 302: }
1.38 albertel 303: }
1.42 albertel 304: return $result;
1.3 albertel 305: }
306:
307: sub end_responseparam {
1.38 albertel 308: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
309: if ($target eq 'edit') { return ('','no'); }
310: return '';
1.55 albertel 311: }
312:
313: sub start_parameter {
314: my $result = &start_responseparam(@_);
315: return $result;
316: }
317:
318: sub end_parameter {
319: my $result = &end_responseparam(@_);
320: return $result;
1.42 albertel 321: }
322:
323: sub setup_params {
324: my ($tag) = @_;
325:
1.52 albertel 326: if ($ENV{'request.state'} eq 'construct') { return; }
1.60 albertel 327: %Apache::inputtags::params=();
1.43 albertel 328: my %paramlist=();
1.42 albertel 329: foreach my $key (keys(%Apache::lonnet::packagetab)) {
330: if ($key =~ /^$tag/) {
331: my ($package,$name) = split(/&/,$key);
1.43 albertel 332: $paramlist{$name}=1;
1.42 albertel 333: }
334: }
1.43 albertel 335: foreach my $key (keys(%paramlist)) {
1.42 albertel 336: my $entry= 'resource.'.$Apache::inputtags::part;
337: if (defined($Apache::inputtags::response[-1])) {
338: $entry.='_'.$Apache::inputtags::response[-1];
339: }
340: $entry.='.'.$key;
341: &Apache::lonxml::debug("looking for $entry");
342: my $value = &Apache::lonnet::EXT("$entry");
343: &Apache::lonxml::debug("$key has value :$value:");
344: if ($value eq 'con_lost' || $value =~ /^error:/) {
345: &Apache::lonxml::debug("using nothing");
346: $Apache::inputtags::params{$key}='';
347: } else {
348: &Apache::lonxml::debug("using value");
349: $Apache::inputtags::params{$key}=$value;
350: }
351: }
1.48 albertel 352: }
353:
354: sub answer_header {
355: my ($type) = @_;
356: my $result;
1.50 albertel 357: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
1.48 albertel 358: $result = '<table border="1"><tr><th>Answer for Part:'.
359: $Apache::inputtags::part. '</th></tr><tr>'."\n";
360: } else {
1.49 albertel 361: $result = '<table border="1"><tr><td>Answer for Part:'.
362: $Apache::inputtags::part. '</td>'."\n";
1.48 albertel 363: }
364: return $result;
365: }
366:
367: sub answer_part {
368: my ($type,$answer) = @_;
369: my $result;
1.50 albertel 370: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48 albertel 371: $result = '<td>'.$answer.'</td>';
372: } else {
373: $result = '<td>'.$answer.'</td>';
374: }
375: return $result;
376: }
377:
378: sub answer_footer {
379: my ($type) = @_;
380: my $result;
1.50 albertel 381: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48 albertel 382: $result = '</tr></table>';
383: } else {
384: $result = '</tr></table>';
385: }
386: return $result;
1.1 albertel 387: }
1.2 albertel 388:
1.62 albertel 389: sub showallfoils {
390: my $return=0;
391: if (defined($ENV{'form.showallfoils'}) &&
392: $ENV{'request.state'} eq 'construct') {
393: $return=1;
394: }
395: return $return;
396: }
1.1 albertel 397: 1;
398: __END__
1.38 albertel 399:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>