Annotation of loncom/homework/response.pm, revision 1.71
1.38 albertel 1: # The LearningOnline Network with CAPA
1.1 albertel 2: # various response type definitons response definition
1.53 albertel 3: #
1.71 ! albertel 4: # $Id: response.pm,v 1.70 2003/04/08 02:42:55 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.65 bowersj2 36: &Apache::lonxml::register('Apache::response',('responseparam','parameter','dataresponse'));
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.69 albertel 71: if (defined($Apache::inputtags::paramstack[-1])) {
72: %Apache::inputtags::params=
73: @{ pop(@Apache::inputtags::paramstack) };
74: }
1.41 albertel 75: return '';
76: }
77:
1.38 albertel 78: # used by response to set the non-safe space random number generator to something
79: # that is stable and unique based on the part number and response number
1.26 albertel 80: sub setrandomnumber {
1.48 albertel 81: my $rndseed;
82: if ($ENV{'request.state'} eq "construct") {
83: $rndseed=$ENV{'form.rndseed'};
84: if (!$rndseed) { $rndseed=time; }
85: } else {
1.59 albertel 86: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
87: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
1.48 albertel 88: }
1.38 albertel 89: &Apache::lonxml::debug("randseed $rndseed");
90: # $rndseed=unpack("%32i",$rndseed);
91: $rndseed=$rndseed
92: +(&Apache::lonnet::numval($Apache::inputtags::part) << 10);
93: if (defined($Apache::inputtags::response['-1'])) {
94: $rndseed=$rndseed
95: +&Apache::lonnet::numval($Apache::inputtags::response['-1']);
96: }
1.64 albertel 97: &Math::Random::random_set_seed_from_phrase($rndseed);
1.38 albertel 98: &Apache::lonxml::debug("randseed $rndseed");
99: return '';
1.26 albertel 100: }
101:
1.7 www 102: sub meta_parameter_write {
1.38 albertel 103: my ($name,$type,$default,$display)=@_;
1.41 albertel 104: my $partref=$Apache::inputtags::part;
105: my $result='<parameter part="'.$Apache::inputtags::part.'"';
106: if (defined($Apache::inputtags::response[-1])) {
107: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
108: $partref.='_'.$Apache::inputtags::response[-1];
109: }
110: $result.= ' name="'.$name.'"'.
111: ' type="'.$type.'"'.
112: ($default?' default="'.$default.'"':'').
113: ($display?' display="'.$display.' [Part: '.$partref.']"':'')
114: .'></parameter>'
115: ."\n";
116: return $result;
1.33 www 117: }
118:
119: sub meta_package_write {
120: my $name=shift;
1.41 albertel 121: my $result = '<parameter part="'.$Apache::inputtags::part.'"';
122: if(defined($Apache::inputtags::response[-1])) {
123: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
124: }
125: $result.=' package="'.$name.'"></parameter>'."\n";
126: return $result;
1.7 www 127: }
128:
129: sub meta_stores_write {
1.10 www 130: my ($name,$type,$display)=@_;
1.41 albertel 131: my $partref=$Apache::inputtags::part;
132: my $result = '<stores part="'.$Apache::inputtags::part.'"';
133: if (defined($Apache::inputtags::response[-1])) {
134: $result.= ' id="'.$Apache::inputtags::response[-1].'"';
135: $partref.='_'.$Apache::inputtags::response[-1];
136: }
137: $result.= ' name="'.$name.'"'.
138: ' type="'.$type.'"'.
139: ' display="'.$display.' [Part: '.$partref.']"'.
140: "></stores>\n";
1.7 www 141: }
142:
143: sub mandatory_part_meta {
144: #
145: # Autogenerate metadata for mandatory
146: # input (from RAT or lonparmset) and
147: # output (to lonspreadsheet)
148: # of each part
149: #
1.38 albertel 150: return
1.34 www 151: # &meta_parameter_write('opendate','date_start','',
152: # 'Opening Date').
153: # &meta_parameter_write('duedate','date_end','',
154: # 'Due Date').
155: # &meta_parameter_write('answerdate','date_start','',
156: # 'Show Answer Date').
157: # &meta_parameter_write('weight','int_zeropos','',
158: # 'Available Points').
159: # &meta_parameter_write('maxtries','int_pos','',
160: # 'Maximum Number of Tries').
1.39 www 161: &meta_package_write('part').
1.10 www 162: &meta_stores_write('solved','string',
1.7 www 163: 'Problem Status').
1.38 albertel 164: &meta_stores_write('tries','int_zeropos',
1.24 albertel 165: 'Number of Attempts').
1.38 albertel 166: &meta_stores_write('awarded','float',
1.7 www 167: 'Partial Credit Factor');
168: #
169: # Note: responseid-specific data 'submission' and 'awarddetail'
170: # not available to spreadsheet -> skip here
171: #
1.14 albertel 172: }
173:
1.15 albertel 174: sub check_for_previous {
1.38 albertel 175: my ($curresponse,$partid,$id) = @_;
176: my %previous;
177: $previous{'used'} = 0;
1.68 albertel 178: foreach my $key (sort(keys(%Apache::lonhomework::history))) {
1.38 albertel 179: if ($key =~ /resource\.$partid\.$id\.submission/) {
180: &Apache::lonxml::debug("Trying $key");
181: my $pastresponse=$Apache::lonhomework::history{$key};
182: if ($pastresponse eq $curresponse) {
183: $previous{'used'} = 1;
184: my $history;
185: if ( $key =~ /^(\d+):/ ) {
186: $history=$1;
187: $previous{'award'} = $Apache::lonhomework::history{"$history:resource.$partid.$id.awarddetail"};
188: $previous{'last'}='0';
1.68 albertel 189: push(@{ $previous{'version'} },$history);
1.38 albertel 190: } else {
191: $previous{'award'} = $Apache::lonhomework::history{"resource.$partid.$id.awarddetail"};
192: $previous{'last'}='1';
1.32 albertel 193: }
1.38 albertel 194: if (! $previous{'award'} ) { $previous{'award'} = 'UNKNOWN'; }
195: &Apache::lonxml::debug("got a match :$previous{'award'}:$previous{'used'}:");
196: }
1.32 albertel 197: }
1.38 albertel 198: }
1.68 albertel 199: &Apache::lonhomework::showhash(%previous);
1.38 albertel 200: return %previous;
1.54 albertel 201: }
202:
203: sub handle_previous {
204: my ($previous,$ad)=@_;
205: if ($$previous{'used'} && ($$previous{'award'} eq $ad) ) {
206: if ($$previous{'last'}) {
207: push(@Apache::inputtags::previous,'PREVIOUSLY_LAST');
208: } else {
209: push(@Apache::inputtags::previous,'PREVIOUSLY_USED');
210: }
1.68 albertel 211: push(@Apache::inputtags::previous_version,$$previous{'version'});
1.54 albertel 212: }
1.44 albertel 213: }
214:
1.45 albertel 215: sub view_or_modify {
1.47 albertel 216: my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
1.45 albertel 217: my $myself=0;
1.46 albertel 218: if ( ($name eq $ENV{'user.name'}) && ($domain eq $ENV{'user.domain'}) ) {
1.45 albertel 219: $myself=1;
220: }
221: my $vgr=&Apache::lonnet::allowed('vgr',$courseid);
222: my $mgr=&Apache::lonnet::allowed('vgr',$courseid);
223: if ($mgr) { return "M"; }
224: if ($vgr) { return "V"; }
225: if ($myself) { return "V"; }
226: return '';
227: }
228:
1.44 albertel 229: sub start_dataresponse {
230: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
231: my $id = &Apache::response::start_response($parstack,$safeeval);
232: my $result;
1.45 albertel 233: if ($target eq 'web') {
234: $result = $token->[2]->{'display'}.':';
235: } elsif ($target eq 'meta') {
1.44 albertel 236: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
237: $token->[2]->{'type'},
238: $token->[2]->{'display'});
239: $result .= &Apache::response::meta_package_write('dataresponse');
240: }
241: return $result;
242: }
243:
244: sub end_dataresponse {
245: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
246: my $result;
247: if ( $target eq 'web' ) {
248: } elsif ($target eq 'grade' ) {
249: if ( defined $ENV{'form.submitted'}) {
1.47 albertel 250: my ($symb,$courseid,$domain,$name)=&Apache::lonxml::whichuser();
1.45 albertel 251: my $allowed=&Apache::lonnet::allowed('mgr',$courseid);
252: if ($allowed) {
1.66 albertel 253: &Apache::response::setup_params('dataresponse');
1.45 albertel 254: my $partid = $Apache::inputtags::part;
255: my $id = $Apache::inputtags::response['-1'];
1.70 albertel 256: my $response = $ENV{'form.HWVAL_'.$id};
1.45 albertel 257: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
258: if ( $response =~ /[^\s]/) {
259: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
260: $response;
261: $Apache::lonhomework::results{
262: "resource.$partid.$id.submission"}=
263: $response;
264: $Apache::lonhomework::results{
265: "resource.$partid.$id.awarddetail"}=
266: 'SUBMITTED';
267: }
268: } else {
269: $result='Not Permitted to change values.'
1.44 albertel 270: }
271: }
272: }
273: &Apache::response::end_response;
274: return $result;
1.3 albertel 275: }
276:
277: sub start_responseparam {
1.38 albertel 278: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.43 albertel 279: my $result='';
1.42 albertel 280: if ($target eq 'meta') {
281: $result = &meta_parameter_write($token->[2]->{'name'},
282: $token->[2]->{'type'},
283: $token->[2]->{'default'},
284: $token->[2]->{'description'});
1.38 albertel 285: } elsif ($target eq 'edit') {
1.56 matthew 286: $result.=&Apache::edit::tag_start($target,$token);
1.38 albertel 287: $result.=&Apache::edit::text_arg('Name:','name',$token).
288: &Apache::edit::text_arg('Type:','type',$token).
289: &Apache::edit::text_arg('Description:','description',$token).
290: &Apache::edit::text_arg('Default:','default',$token).
291: "</td></tr>";
292: $result.=&Apache::edit::end_table;
293: } elsif ($target eq 'modified') {
294: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
295: 'name','type','description',
296: 'default');
297: if ($constructtag) {
298: $result = &Apache::edit::rebuild_tag($token);
299: $result.=&Apache::edit::handle_insert();
300: }
1.66 albertel 301: } elsif ($target eq 'grade' || $target eq 'answer' || $target eq 'web' ||
302: $target eq 'tex' || $target eq 'analyze' ) {
1.52 albertel 303: if ($ENV{'request.state'} eq 'construct') {
304: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
305: my $default = &Apache::lonxml::get_param('default',$parstack,$safeeval);
1.61 albertel 306: if ($name) {$Apache::inputtags::params{$name}=$default;}
1.52 albertel 307: }
1.38 albertel 308: }
1.42 albertel 309: return $result;
1.3 albertel 310: }
311:
312: sub end_responseparam {
1.38 albertel 313: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
314: if ($target eq 'edit') { return ('','no'); }
315: return '';
1.55 albertel 316: }
317:
318: sub start_parameter {
319: my $result = &start_responseparam(@_);
320: return $result;
321: }
322:
323: sub end_parameter {
324: my $result = &end_responseparam(@_);
325: return $result;
1.42 albertel 326: }
327:
1.67 albertel 328: sub reset_params {
329: %Apache::inputtags::params=();
330: }
331:
1.42 albertel 332: sub setup_params {
333: my ($tag) = @_;
334:
1.52 albertel 335: if ($ENV{'request.state'} eq 'construct') { return; }
1.43 albertel 336: my %paramlist=();
1.42 albertel 337: foreach my $key (keys(%Apache::lonnet::packagetab)) {
338: if ($key =~ /^$tag/) {
339: my ($package,$name) = split(/&/,$key);
1.43 albertel 340: $paramlist{$name}=1;
1.42 albertel 341: }
342: }
1.43 albertel 343: foreach my $key (keys(%paramlist)) {
1.42 albertel 344: my $entry= 'resource.'.$Apache::inputtags::part;
345: if (defined($Apache::inputtags::response[-1])) {
346: $entry.='_'.$Apache::inputtags::response[-1];
347: }
348: $entry.='.'.$key;
349: &Apache::lonxml::debug("looking for $entry");
350: my $value = &Apache::lonnet::EXT("$entry");
351: &Apache::lonxml::debug("$key has value :$value:");
352: if ($value eq 'con_lost' || $value =~ /^error:/) {
353: &Apache::lonxml::debug("using nothing");
354: $Apache::inputtags::params{$key}='';
355: } else {
356: &Apache::lonxml::debug("using value");
357: $Apache::inputtags::params{$key}=$value;
358: }
359: }
1.48 albertel 360: }
361:
362: sub answer_header {
363: my ($type) = @_;
364: my $result;
1.50 albertel 365: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse' ) {
1.48 albertel 366: $result = '<table border="1"><tr><th>Answer for Part:'.
367: $Apache::inputtags::part. '</th></tr><tr>'."\n";
368: } else {
1.49 albertel 369: $result = '<table border="1"><tr><td>Answer for Part:'.
370: $Apache::inputtags::part. '</td>'."\n";
1.48 albertel 371: }
372: return $result;
373: }
374:
375: sub answer_part {
376: my ($type,$answer) = @_;
377: my $result;
1.50 albertel 378: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48 albertel 379: $result = '<td>'.$answer.'</td>';
380: } else {
381: $result = '<td>'.$answer.'</td>';
382: }
383: return $result;
384: }
385:
386: sub answer_footer {
387: my ($type) = @_;
388: my $result;
1.50 albertel 389: if ($type eq 'optionresponse' || $type eq 'radiobuttonresponse') {
1.48 albertel 390: $result = '</tr></table>';
391: } else {
392: $result = '</tr></table>';
393: }
394: return $result;
1.1 albertel 395: }
1.2 albertel 396:
1.62 albertel 397: sub showallfoils {
398: my $return=0;
399: if (defined($ENV{'form.showallfoils'}) &&
400: $ENV{'request.state'} eq 'construct') {
401: $return=1;
402: }
403: return $return;
1.70 albertel 404: }
405:
406: sub getresponse {
407: my ($temp)=@_;
408: my $formparm='form.HWVAL_'.$Apache::inputtags::response['-1'];
409: my $response;
410: if (!defined($temp)) {
411: $temp=1;
412: } else {
413: $formparm.=":$temp";
414: }
415: my %let_to_num=('A'=>0,'B'=>1,'C'=>2,'D'=>3,'E'=>4,'F'=>5,'G'=>6,'H'=>7,
416: 'I'=>8,'J'=>9,'K'=>10,'L'=>11,'M'=>12,'N'=>13,'O'=>14,
417: 'P'=>15,'Q'=>16,'R'=>17,'S'=>18,'T'=>19,'U'=>20,'V'=>21,
418: 'W'=>22,'X'=>23,'Y'=>24,'Z'=>25);
419: if ($ENV{'form.submitted'} eq 'scantron') {
1.71 ! albertel 420: my $part = $Apache::inputtags::part;
! 421: my $id = $Apache::inputtags::response[-1];
1.70 albertel 422: $response = $ENV{'scantron.'.($Apache::lonxml::counter+$temp-1).
423: '.answer'};
1.71 ! albertel 424: # save bubbled letter for later
! 425: $Apache::lonhomework::results{"resource.$part.$id.scantron"}.=
! 426: $response;
1.70 albertel 427: $response = $let_to_num{$response};
428: } else {
429: $response = $ENV{$formparm};
430: }
431: return $response;
1.62 albertel 432: }
1.71 ! albertel 433:
! 434: sub repetition {
! 435: my $id = $Apache::inputtags::part;
! 436: my $weight = &Apache::lonnet::EXT("resource.$id.weight");
! 437: my $repetition = int $weight/9;
! 438: if ($weight % 9 != 0) {$repetition++;}
! 439: return $repetition;
! 440: }
! 441:
1.1 albertel 442: 1;
443: __END__
1.38 albertel 444:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>