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