Annotation of loncom/homework/inputtags.pm, revision 1.53
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.53 ! albertel 4: # $Id: inputtags.pm,v 1.52 2002/01/30 17:32:42 albertel Exp $
1.47 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.43 albertel 28: # 2/19 Guy
1.1 albertel 29:
30: package Apache::inputtags;
31: use strict;
32:
1.50 harris41 33: BEGIN {
1.48 albertel 34: &Apache::lonxml::register('Apache::inputtags',('textfield','textline','datasubmission'));
1.1 albertel 35: }
36:
1.43 albertel 37:
1.1 albertel 38: sub initialize_inputtags {
1.43 albertel 39: # list of current input ids
40: @Apache::inputtags::input=();
41: # list of all input ids seen in this problem
42: @Apache::inputtags::inputlist=();
43: # list of all current response ids
44: @Apache::inputtags::response=();
45: # list of all response ids seen in this problem
46: @Apache::inputtags::responselist=();
47: # list of whether or not a specific response was previously used
48: @Apache::inputtags::previous=();
49: # id of current part, 0 means that no part is current (inside <problem> only
50: $Apache::inputtags::part='';
1.46 albertel 51: # list of problem date statuses, the first element is for <problem>
52: # if there is a second element it is for the current <part>
1.43 albertel 53: @Apache::inputtags::status=();
1.46 albertel 54: # hash of defined params for the current response
1.43 albertel 55: %Apache::inputtags::params=();
1.46 albertel 56: # list of all ids, for <import>, these get join()ed and prepended
57: @Apache::inputtags::import=();
1.1 albertel 58: }
59:
1.14 albertel 60: sub start_input {
1.43 albertel 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::input,$id);
65: push (@Apache::inputtags::inputlist,$id);
66: return $id;
1.14 albertel 67: }
68:
69: sub end_input {
1.43 albertel 70: pop @Apache::inputtags::input;
71: return '';
1.14 albertel 72: }
73:
1.48 albertel 74: sub start_textfield {
1.43 albertel 75: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
76: my $result = "";
77: my $id = &start_input($parstack,$safeeval);
78: if ($target eq 'web') {
79: my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
80: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
81: if ( $cols eq '') { $cols = 80; }
82: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
83: if ( $rows eq '') { $rows = 10; }
1.45 albertel 84: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
85: $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
86: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
87: } else {
88: $result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
89: }
1.43 albertel 90: if ($oldresponse ne '') {
91: #get rid of any startup text if the user has already responded
1.51 albertel 92: &Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
93: }
94: }
95: if ($target eq 'grade') {
96: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
97: if ($seedtext eq $ENV{'form.HWVAL'.$Apache::inputtags::response[-1]}) {
98: # if the seed text is still there it wasn't a real submission
99: $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']}='';
1.30 albertel 100: }
1.43 albertel 101: }
102: return $result;
1.6 albertel 103: }
104:
1.48 albertel 105: sub end_textfield {
1.43 albertel 106: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
107: if ($target eq 'web') {
1.45 albertel 108: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
109: return "</textarea>";
110: }
111: }
1.43 albertel 112: &end_input;
113: return '';
1.6 albertel 114: }
115:
1.1 albertel 116: sub start_textline {
1.43 albertel 117: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
118: my $result = "";
119: if ($target eq 'web') {
120: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
121: if ($size eq '') { $size=20; }
1.44 albertel 122: my $partid=$Apache::inputtags::part;
123: my $id=$Apache::inputtags::response['-1'];
124: my $oldresponse =
125: $Apache::lonhomework::history{"resource.$partid.$id.submission"};
1.45 albertel 126: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
127: $result= '<input type="text" name="HWVAL'.$id.'" value="'.
128: $oldresponse.'" size="'.$size.'" />';
129: } else {
130: $result='<i>'.$oldresponse.'</i>';
131: }
1.44 albertel 132: } elsif ($target eq 'edit') {
1.49 matthew 133: $result=&Apache::edit::tag_start($target,$token);
1.43 albertel 134: $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
135: $result.=&Apache::edit::end_table;
1.44 albertel 136: } elsif ($target eq 'modified') {
1.43 albertel 137: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
138: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
139: }
140: return $result;
1.1 albertel 141: }
142:
143: sub end_textline {
1.43 albertel 144: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
145: if ($target eq 'edit') { return ('','no'); }
146: return "";
1.1 albertel 147: }
148:
1.6 albertel 149: sub start_datasubmission {
1.44 albertel 150: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
151: my $id = &Apache::response::start_response($parstack,$safeeval);
152: my $result;
153: if ($target eq 'meta') {
154: $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
155: $token->[2]->{'type'},
156: $token->[2]->{'display'});
157: $result .= &Apache::response::meta_package_write('datasubmission');
158: }
159: return $result;
1.6 albertel 160: }
161:
162: sub end_datasubmission {
1.43 albertel 163: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.44 albertel 164: my $result;
165: if ( $target eq 'web' ) {
166: } elsif ($target eq 'grade' ) {
167: if ( defined $ENV{'form.submitted'}) {
168: &Apache::response::setup_params('datasubmission');
169: my $partid = $Apache::inputtags::part;
170: my $id = $Apache::inputtags::response['-1'];
171: my $response = $ENV{'form.HWVAL'.$id};
172: my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
173: if ( $response =~ /[^\s]/) {
174: $Apache::lonhomework::results{"resource.$partid.$id.$name"}=
175: $response;
176: }
177: }
1.43 albertel 178: }
1.44 albertel 179: &Apache::response::end_response;
180: return $result;
1.9 albertel 181: }
182:
183: sub finalizeawards {
1.43 albertel 184: my $result='';
185: my $award;
186: if ($#_ == '-1') { $result = "NO_RESPONSE"; }
187: if ($result eq '' ) {
188: foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
189: }
190: if ($result eq '' ) {
191: foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
192: }
193: if ($result eq '' ) {
194: foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
195: }
196:
197: if ($result eq '' ) {
198: foreach $award (@_) {
199: if ($award eq 'UNIT_FAIL' ||
200: $award eq 'NO_UNIT' ||
201: $award eq 'UNIT_NOTNEEDED') {
202: $result=$award; last;
203: }
204: }
205: }
206: if ($result eq '' ) {
207: foreach $award (@_) {
208: if ($award eq 'WANTED_NUMERIC' ||
209: $award eq 'BAD_FORMULA') {$result=$award; last;}
210: }
211: }
212: if ($result eq '' ) {
213: foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
214: }
215: if ($result eq '' ) {
216: foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
217: }
218: if ($result eq '' ) {
219: foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
220: }
221: if ($result eq '' ) {
222: foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
223: }
224: if ($result eq '' ) { $result='EXACT_ANS'; }
225: return $result
1.9 albertel 226: }
227:
1.10 albertel 228: sub decideoutput {
1.43 albertel 229: my ($award,$solved,$previous)=@_;
230: my $message='';
231: my $button=0;
232: my $previousmsg;
233:
234: if ($previous) { $previousmsg='You have entered that answer before'; }
235:
236: if ($solved =~ /^correct/) {
237: $message = "<b>You are correct.</b> Your receipt is ".
238: &Apache::lonnet::receipt;
239: $button=0;
240: $previousmsg='';
241: } elsif ($solved =~ /^excused/) {
242: $message = "<b>You are excused from the problem.</b>";
243: $button=0;
244: $previousmsg='';
245: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
246: if ($solved =~ /^incorrect/ || $solved eq '') {
247: $message = "Incorrect";
248: $button=1;
1.37 albertel 249: } else {
1.43 albertel 250: $message = "<b>You are correct.</b> Your receipt is ".
251: &Apache::lonnet::receipt;
252: $button=0;
253: $previousmsg='';
254: }
255: } elsif ($award eq 'NO_RESPONSE') {
256: $message = '';
257: $button=1;
258: } elsif ($award eq 'MISSING_ANSWER') {
259: $message = 'Some parts were not submitted';
260: $button = 1;
261: } elsif ($award eq 'WANTED_NUMERIC') {
262: $message = "This question expects a numeric answer";
263: $button=1;
264: } elsif ($award eq 'SIG_FAIL') {
265: $message = "Please adjust significant figures.";# you provided %s significant figures";
266: $button=1;
267: } elsif ($award eq 'UNIT_FAIL') {
268: $message = "Units incorrect."; #Computer reads units as %s";
269: $button=1;
270: } elsif ($award eq 'UNIT_NOTNEEDED') {
271: $message = "Only a number required.";# Computer reads units of %s";
272: $button=1;
273: } elsif ($award eq 'NO_UNIT') {
274: $message = "Units required";
275: $button=1;
276: } elsif ($award eq 'BAD_FORMULA') {
277: $message = "Unable to understand formula";
278: $button=1;
279: } elsif ($award eq 'INCORRECT') {
280: $message = "Incorrect";
281: $button=1;
282: } elsif ($award eq 'SUBMITTED') {
283: $message = "Your submission has been recorded.";
284: $button=1;
285: } else {
286: $message = "Unknown message: $award";
287: $button=1;
288: }
289: return ($button,$message,$previousmsg);
1.12 albertel 290: }
291:
292: sub setgradedata {
1.43 albertel 293: my ($award,$id,$previously_used) = @_;
294: # if the student already has it correct, don't modify the status
1.53 ! albertel 295: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER') {
! 296: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
! 297: return '';
! 298: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.43 albertel 299: /^correct/ ) {
300: #handle assignment of tries and solved status
1.53 ! albertel 301: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
! 302: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
! 303: }
1.43 albertel 304: if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
305: $Apache::lonhomework::results{"resource.$id.tries"} =
306: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
307: $Apache::lonhomework::results{"resource.$id.solved"} =
308: 'correct_by_student';
309: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
310: } elsif ( $award eq 'INCORRECT' ) {
311: $Apache::lonhomework::results{"resource.$id.tries"} =
312: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
313: $Apache::lonhomework::results{"resource.$id.solved"} =
314: 'incorrect_attempted';
315: } elsif ( $award eq 'SUBMITTED' ) {
316: $Apache::lonhomework::results{"resource.$id.tries"} =
317: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
318: $Apache::lonhomework::results{"resource.$id.solved"} =
319: 'ungraded_attempted';
320: } elsif ( $award eq 'NO_RESPONSE' ) {
321: return '';
322: } else {
323: $Apache::lonhomework::results{"resource.$id.solved"} =
324: 'incorrect_attempted';
325: }
326:
327: # check if this was a previous submission if it was delete the
328: # unneeded data and update the previously_used attribute
329: if ( $previously_used eq 'PREVIOUSLY_USED') {
330: delete($Apache::lonhomework::results{"resource.$id.tries"});
331: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
332: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
333: #delete all data as they student didn't do anything
334: foreach my $key (keys(%Apache::lonhomework::results)) {
335: if ($key =~ /^resource\.$id\./) {
336: &Apache::lonxml::debug("Removing $key");
337: delete($Apache::lonhomework::results{$key});
338: }
339: }
340: #and since they didn't do anything we were never here
341: return '';
342: } else {
343: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40 albertel 344: }
1.43 albertel 345: }
346: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 347: }
348:
1.9 albertel 349: sub grade {
1.43 albertel 350: my ($target) = @_;
351: my $id = $Apache::inputtags::part;
352: my $response='';
353: if ( defined $ENV{'form.submitted'}) {
354: my @awards = ();
355: foreach $response (@Apache::inputtags::responselist) {
356: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
357: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
358: if ( $value ne '' ) {
359: &Apache::lonxml::debug("keeping $value from $response for $id");
360: push (@awards,$value);
361: } else {
362: &Apache::lonxml::debug("skipping $value from $response for $id");
363: }
364: }
365: my $finalaward = &finalizeawards(@awards);
366: my $previously_used;
367: if ( $#Apache::inputtags::previous eq $#awards ) {
368: $previously_used = 'PREVIOUSLY_LAST';
369: foreach my $value (@Apache::inputtags::previous) {
370: if ($value eq 'PREVIOUSLY_USED' ) {
371: $previously_used = $value;
372: last;
373: }
374: }
375: }
376: &Apache::lonxml::debug("final award $finalaward, $previously_used");
377: &setgradedata($finalaward,$id,$previously_used);
378: }
379: return '';
1.1 albertel 380: }
381:
1.11 albertel 382: sub gradestatus {
1.43 albertel 383: my ($id) = @_;
384: my $showbutton = 1;
385: my $message = '';
1.53 ! albertel 386: my $latemessage = '';
1.43 albertel 387: my $trystr='';
388: my $button='';
389: my $previousmsg='';
390:
391: my $status = $Apache::inputtags::status['-1'];
392: &Apache::lonxml::debug("gradestatus has :$status:");
393: if ( $status ne 'CLOSED' ) {
394: my $award = $Apache::lonhomework::history{"resource.$id.award"};
395: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
396: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
397: &Apache::lonxml::debug("Found Award |$award|$solved|");
398: if ( $award ne '' ) {
399: &Apache::lonxml::debug('Getting message');
400: ($showbutton,$message,$previousmsg) =
401: &decideoutput($award,$solved,$previous);
402: $message="<td bgcolor=\"#aaffaa\">$message</td>";
403: if ($previousmsg) {
404: $previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
405: }
406: }
407: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
408: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
409: &Apache::lonxml::debug("got maxtries of :$maxtries:");
410: if ( $tries eq '' ) { $tries = '0'; }
411: if ( $maxtries eq '' ) { $maxtries = '2'; }
412: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
413: if ( $showbutton ) {
414: $trystr = "<td>Tries $tries/$maxtries</td>";
415: }
416: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
417: if ( $showbutton ) {
1.52 albertel 418: $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
1.43 albertel 419: }
1.53 ! albertel 420: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
! 421: #last submissions was after due date
! 422: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
! 423: }
1.43 albertel 424: }
1.53 ! albertel 425: my $output= $previousmsg.$latemessage.$message.$trystr;
1.43 albertel 426: if ($output =~ /^\s*$/) {
427: return $button;
428: } else {
1.53 ! albertel 429: return $button.'<table><tr>'.$output.'</tr></table>';
1.43 albertel 430: }
1.11 albertel 431: }
1.1 albertel 432: 1;
433: __END__
1.43 albertel 434:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>