File:
[LON-CAPA] /
loncom /
homework /
inputtags.pm
Revision
1.92:
download - view:
text,
annotated -
select for diffs
Sat Apr 19 09:00:54 2003 UTC (21 years, 4 months ago) by
albertel
Branches:
MAIN
CVS tags:
HEAD
- saving my work
- I am not sure this is correct yet but it is a step closer,
- New award detail ASSIGNED_SCORE, causes inputtags to grab individual awardeds if exist from all responses and then do ain arithmatic average
1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.92 2003/04/19 09:00:54 albertel Exp $
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: #
28: # 2/19 Guy
29:
30: package Apache::inputtags;
31: use HTML::Entities();
32: use strict;
33: use Apache::loncommon;
34:
35: BEGIN {
36: &Apache::lonxml::register('Apache::inputtags',('textfield','textline'));
37: }
38:
39:
40: sub initialize_inputtags {
41: # list of current input ids
42: @Apache::inputtags::input=();
43: # list of all input ids seen in this problem
44: @Apache::inputtags::inputlist=();
45: # list of all current response ids
46: @Apache::inputtags::response=();
47: # list of all response ids seen in this problem
48: @Apache::inputtags::responselist=();
49: # list of whether or not a specific response was previously used
50: @Apache::inputtags::previous=();
51: # submission it was used in
52: @Apache::inputtags::previous_version=();
53: # id of current part, 0 means that no part is current (inside <problem> only
54: $Apache::inputtags::part='';
55: # list of problem date statuses, the first element is for <problem>
56: # if there is a second element it is for the current <part>
57: @Apache::inputtags::status=();
58: # hash of defined params for the current response
59: %Apache::inputtags::params=();
60: # list of all ids, for <import>, these get join()ed and prepended
61: @Apache::inputtags::import=();
62: }
63:
64: sub start_input {
65: my ($parstack,$safeeval)=@_;
66: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
67: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
68: push (@Apache::inputtags::input,$id);
69: push (@Apache::inputtags::inputlist,$id);
70: return $id;
71: }
72:
73: sub end_input {
74: pop @Apache::inputtags::input;
75: return '';
76: }
77:
78: sub start_textfield {
79: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
80: my $result = "";
81: my $id = &start_input($parstack,$safeeval);
82: my $resid=$Apache::inputtags::response[-1];
83: if ($target eq 'web') {
84: $Apache::lonxml::evaluate--;
85: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
86: my $partid=$Apache::inputtags::part;
87: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"});
88: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
89: if ( $cols eq '') { $cols = 80; }
90: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
91: if ( $rows eq '') { $rows = 10; }
92: $result= '<textarea name="HWVAL_'.$resid.'" '.
93: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
94: if ($oldresponse ne '') {
95: #get rid of any startup text if the user has already responded
96: &Apache::lonxml::get_all_text("/textfield",$parser);
97: }
98: } else {
99: #right or wrong don't show it
100: #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
101: $result='';
102: #get rid of any startup text
103: &Apache::lonxml::get_all_text("/textfield",$parser);
104: }
105: } elsif ($target eq 'grade') {
106: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
107: if ($seedtext eq $ENV{'form.HWVAL_'.$resid}) {
108: # if the seed text is still there it wasn't a real submission
109: $ENV{'form.HWVAL_'.$resid}='';
110: }
111: } elsif ($target eq 'edit') {
112: $result.=&Apache::edit::tag_start($target,$token);
113: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
114: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
115: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
116: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
117: } elsif ($target eq 'modified') {
118: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
119: $safeeval,'rows','cols');
120: if ($constructtag) {
121: $result = &Apache::edit::rebuild_tag($token);
122: } else {
123: $result=$token->[4];
124: }
125: $result.=&Apache::edit::modifiedfield();
126: } elsif ($target eq 'tex') {
127: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
128: my $number_of_lines= &Apache::lonxml::get_param('rows',$parstack,$safeeval);
129: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
130: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
131: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
132: }
133: }
134: return $result;
135: }
136:
137: sub end_textfield {
138: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
139: my $result;
140: if ($target eq 'web') {
141: $Apache::lonxml::evaluate++;
142: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
143: return "</textarea>";
144: }
145: } elsif ($target eq 'edit') {
146: $result=&Apache::edit::end_table();
147: }
148: &end_input;
149: return $result;
150: }
151:
152: sub start_textline {
153: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
154: my $result = "";
155: if ($target eq 'web') {
156: $Apache::lonxml::evaluate--;
157: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
158: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
159: my $maxlength;
160: if ($size eq '') { $size=20; } else {
161: if ($size < 20) { $maxlength=$size; }
162: }
163: my $partid=$Apache::inputtags::part;
164: my $id=$Apache::inputtags::response[-1];
165: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
166: if ($Apache::lonhomework::type ne 'exam') {
167: $result= '<input type="text" name="HWVAL_'.$id.'" value="'.
168: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
169: }
170: } else {
171: #right or wrong don't show what was last typed in.
172: #$result='<i>'.$oldresponse.'</i>';
173: $result='';
174: }
175: } elsif ($target eq 'edit') {
176: $result=&Apache::edit::tag_start($target,$token);
177: $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
178: $result.=&Apache::edit::end_table;
179: } elsif ($target eq 'modified') {
180: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
181: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
182: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
183: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
184: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
185: $result='\framebox['.$size.'][s]{\tiny\strut}';
186: }
187: return $result;
188: }
189:
190: sub end_textline {
191: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
192: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
193: elsif ($target eq 'edit') { return ('','no'); }
194: return "";
195: }
196:
197: sub finalizeawards {
198: my $result='';
199: my $award;
200: if ($#_ == '-1') { $result = "NO_RESPONSE"; }
201: if ($result eq '' ) {
202: my $blankcount;
203: foreach $award (@_) {
204: if ($award eq '') {
205: $result='MISSING_ANSWER';
206: $blankcount++;
207: }
208: }
209: if ($blankcount == ($#_ + 1)) { $result = 'NO_RESPONSE'; }
210: }
211: if ($result eq '' ) {
212: foreach $award (@_) { if ($award eq 'MISSING_ANSWER') {$result='MISSING_ANSWER'; last;}}
213: }
214: if ($result eq '' ) {
215: foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
216: }
217: if ($result eq '' ) {
218: foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
219: }
220:
221: if ($result eq '' ) {
222: foreach $award (@_) {
223: if ($award eq 'UNIT_FAIL' ||
224: $award eq 'NO_UNIT' ||
225: $award eq 'UNIT_NOTNEEDED') {
226: $result=$award; last;
227: }
228: }
229: }
230: if ($result eq '' ) {
231: foreach $award (@_) {
232: if ($award eq 'WANTED_NUMERIC' ||
233: $award eq 'BAD_FORMULA') {$result=$award; last;}
234: }
235: }
236: if ($result eq '' ) {
237: foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
238: }
239: if ($result eq '' ) {
240: foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
241: }
242: if ($result eq '' ) {
243: foreach $award (@_) { if ($award eq 'MISORDERED_RANK') {$result=$award; last;} }
244: }
245: if ($result eq '' ) {
246: foreach $award (@_) { if ($award eq 'INVALID_FILETYPE') {$result=$award; last;} }
247: }
248: if ($result eq '' ) {
249: foreach $award (@_) { if ($award eq 'DRAFT') {$result=$award; last;} }
250: }
251: if ($result eq '' ) {
252: foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
253: }
254: if ($result eq '' ) {
255: foreach $award (@_) { if ($award eq 'ASSIGNED_SCORE') {$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
262: }
263:
264: sub decideoutput {
265: my ($award,$solved,$previous,$target)=@_;
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: if ($award eq 'ASSIGNED_SCORE') {
274: $message = "A score has been assigned.";
275: } else {
276: if ($target eq 'tex') {
277: $message = '\textbf{You are correct}.';
278: } else {
279: $message = "<b>You are correct.</b>";
280: }
281: $message=' Your receipt is '.&Apache::lonnet::receipt;
282: }
283: $button=0;
284: $previousmsg='';
285: } elsif ($solved =~ /^excused/) {
286: $message = "<b>You are excused from the problem.</b>";
287: $button=0;
288: $previousmsg='';
289: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
290: if ($solved =~ /^incorrect/ || $solved eq '') {
291: $message = "Incorrect";
292: $button=1;
293: } else {
294: $message = "<b>You are correct.</b> Your receipt is ".
295: &Apache::lonnet::receipt;
296: $button=0;
297: $previousmsg='';
298: }
299: } elsif ($award eq 'NO_RESPONSE') {
300: $message = '';
301: $button=1;
302: } elsif ($award eq 'MISSING_ANSWER') {
303: $message = 'Some parts were not submitted';
304: $button = 1;
305: } elsif ($award eq 'WANTED_NUMERIC') {
306: $message = "This question expects a numeric answer";
307: $button=1;
308: } elsif ($award eq 'MISORDERED_RANK') {
309: $message = 'You have provided an invalid ranking, please refer to '.
310: &Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
311: $button=1;
312: } elsif ($award eq 'INVALID_FILETYPE') {
313: $message = 'The filetype extension of the file you uploaded is not allowed.';
314: $button=1;
315: } elsif ($award eq 'SIG_FAIL') {
316: $message = "Please adjust significant figures.";# you provided %s significant figures";
317: $button=1;
318: } elsif ($award eq 'UNIT_FAIL') {
319: $message = "Units incorrect. ".
320: &Apache::loncommon::help_open_topic('Physical_Units'); #Computer reads units as %s";
321: $button=1;
322: } elsif ($award eq 'UNIT_NOTNEEDED') {
323: $message = "Only a number required.";# Computer reads units of %s";
324: $button=1;
325: } elsif ($award eq 'NO_UNIT') {
326: $message = "Units required".
327: &Apache::loncommon::help_open_topic('Physical_Units');
328: $button=1;
329: } elsif ($award eq 'BAD_FORMULA') {
330: $message = "Unable to understand formula";
331: $button=1;
332: } elsif ($award eq 'INCORRECT') {
333: $message = "Incorrect";
334: $button=1;
335: } elsif ($award eq 'SUBMITTED') {
336: $message = "Your submission has been recorded.";
337: $button=1;
338: } elsif ($award eq 'DRAFT') {
339: $message = "A draft copy has been saved.";
340: $button=1;
341: } elsif ($award eq 'ASSIGNED_SCORE') {
342: $message = "A score has been assigned.";
343: $button=0;
344: } else {
345: $message = "Unknown message: $award";
346: $button=1;
347: }
348: if (lc($Apache::lonhomework::problemstatus) ne 'yes') {
349: $message = "Answer Submitted";
350: $button=1;
351: }
352: return ($button,$message,$previousmsg);
353: }
354:
355: sub removealldata {
356: my ($id)=@_;
357: foreach my $key (keys(%Apache::lonhomework::results)) {
358: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
359: &Apache::lonxml::debug("Removing $key");
360: delete($Apache::lonhomework::results{$key});
361: }
362: }
363: }
364:
365: sub setgradedata {
366: my ($award,$id,$previously_used) = @_;
367: # if the student already has it correct, don't modify the status
368: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
369: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
370: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
371: return '';
372: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
373: /^correct/ ) {
374: #handle assignment of tries and solved status
375: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
376: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
377: }
378: if ( $award eq 'ASSIGNED_SCORE') {
379: $Apache::lonhomework::results{"resource.$id.tries"} =
380: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
381: $Apache::lonhomework::results{"resource.$id.solved"} =
382: 'correct_by_student';
383: my $numawards=scalar(@Apache::inputtags::response);
384: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
385: foreach my $res (@Apache::inputtags::response) {
386: $Apache::lonhomework::results{"resource.$id.awarded"}+=
387: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
388: }
389: $Apache::lonhomework::results{"resource.$id.awarded"}/=$numawards;
390: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
391: $Apache::lonhomework::results{"resource.$id.tries"} =
392: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
393: $Apache::lonhomework::results{"resource.$id.solved"} =
394: 'correct_by_student';
395: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
396: } elsif ( $award eq 'INCORRECT' ) {
397: $Apache::lonhomework::results{"resource.$id.tries"} =
398: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
399: $Apache::lonhomework::results{"resource.$id.solved"} =
400: 'incorrect_attempted'
401: } elsif ( $award eq 'SUBMITTED' ) {
402: $Apache::lonhomework::results{"resource.$id.tries"} =
403: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
404: $Apache::lonhomework::results{"resource.$id.solved"} =
405: 'ungraded_attempted';
406: } elsif ( $award eq 'DRAFT' ) {
407: $Apache::lonhomework::results{"resource.$id.solved"} = '';
408: } elsif ( $award eq 'NO_RESPONSE' ) {
409: #no real response so delete any data that got stored
410: &removealldata($id);
411: return '';
412: } else {
413: $Apache::lonhomework::results{"resource.$id.solved"} =
414: 'incorrect_attempted';
415: }
416:
417: # check if this was a previous submission if it was delete the
418: # unneeded data and update the previously_used attribute
419: if ( $previously_used eq 'PREVIOUSLY_USED') {
420: delete($Apache::lonhomework::results{"resource.$id.tries"});
421: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
422: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
423: #delete all data as they student didn't do anything, but save
424: #the list of collaborators.
425: &removealldata($id);
426: #and since they didn't do anything we were never here
427: return '';
428: } else {
429: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
430: }
431: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
432: /^correct/ ) {
433: #delete all data as they student already has it correct
434: &removealldata($id);
435: #and since they didn't do anything we were never here
436: return '';
437: }
438: $Apache::lonhomework::results{"resource.$id.award"} = $award;
439: }
440:
441: sub grade {
442: my ($target) = @_;
443: my $id = $Apache::inputtags::part;
444: my $response='';
445: if ( defined $ENV{'form.submitted'}) {
446: my @awards = ();
447: foreach $response (@Apache::inputtags::responselist) {
448: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
449: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
450: &Apache::lonxml::debug("keeping $value from $response for $id");
451: push (@awards,$value);
452: }
453: my $finalaward = &finalizeawards(@awards);
454: my $previously_used;
455: if ( $#Apache::inputtags::previous eq $#awards ) {
456: my $match=0;
457: my @matches;
458: foreach my $versionar (@Apache::inputtags::previous_version) {
459: foreach my $version (@$versionar) {
460: $matches[$version]++;
461: }
462: }
463: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
464: if ($match) {
465: $previously_used = 'PREVIOUSLY_LAST';
466: foreach my $value (@Apache::inputtags::previous) {
467: if ($value eq 'PREVIOUSLY_USED' ) {
468: $previously_used = $value;
469: last;
470: }
471: }
472: }
473: }
474: &Apache::lonxml::debug("final award $finalaward, $previously_used");
475: &setgradedata($finalaward,$id,$previously_used);
476: }
477: return '';
478: }
479:
480: sub gradestatus {
481: my ($id,$target) = @_;
482: my $showbutton = 1;
483: my $message = '';
484: my $latemessage = '';
485: my $trystr='';
486: my $button='';
487: my $previousmsg='';
488:
489: my $status = $Apache::inputtags::status['-1'];
490: &Apache::lonxml::debug("gradestatus has :$status:");
491: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE') {
492: my $award = $Apache::lonhomework::history{"resource.$id.award"};
493: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
494: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
495: &Apache::lonxml::debug("Found Award |$award|$solved|");
496: if ( $award ne '' ) {
497: &Apache::lonxml::debug('Getting message');
498: ($showbutton,$message,$previousmsg) =
499: &decideoutput($award,$solved,$previous,$target);
500: if ($target eq 'tex') {
501: $message=' '.$message.' ';
502: } else {
503: $message="<td bgcolor=\"#aaffaa\">$message</td>";
504: if ($previousmsg) {
505: $previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
506: }
507: }
508: }
509: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
510: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
511: &Apache::lonxml::debug("got maxtries of :$maxtries:");
512: if ( $tries eq '' ) { $tries = '0'; }
513: if ( $maxtries eq '' ) { $maxtries = '2'; }
514: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
515: if ( $showbutton ) {
516: if ($target eq 'tex') {
517: if ($ENV{'request.state'} ne "construct") {
518: $trystr = ' {\small \textit{Tries} '.$tries.'/'.$maxtries.'} \vskip 0 mm ';
519: } else {
520: $trystr = '\vskip 0 mm ';
521: }
522: } else {
523: $trystr = "<td>Tries $tries/$maxtries</td>";
524: }
525: }
526: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
527: if ( $showbutton ) {
528: if ($target ne 'tex') {
529: $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
530: }
531: }
532: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
533: #last submissions was after due date
534: if ($target eq 'tex') {
535: $latemessage=' The last submission was after the Due Date ';
536: } else {
537: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
538: }
539: }
540: }
541: my $output= $previousmsg.$latemessage.$message.$trystr;
542: if ($output =~ /^\s*$/) {
543: return $button;
544: } else {
545: if ($target eq 'tex') {
546: return $button.' \vskip 0 mm '.$output.' ';
547: } else {
548: return $button.'<table><tr>'.$output.'</tr></table>';
549: }
550: }
551: }
552: 1;
553: __END__
554:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>