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