1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.134 2004/03/05 21:31:07 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: package Apache::inputtags;
29: use HTML::Entities();
30: use strict;
31: use Apache::loncommon;
32: use Apache::lonlocal;
33:
34: BEGIN {
35: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
36: }
37:
38:
39: sub initialize_inputtags {
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: # submission it was used in
51: @Apache::inputtags::previous_version=();
52: # id of current part, 0 means that no part is current (inside <problem> only
53: $Apache::inputtags::part='';
54: # list of all part ids seen
55: @Apache::inputtags::partlist=();
56: # list of problem date statuses, the first element is for <problem>
57: # if there is a second element it is for the current <part>
58: @Apache::inputtags::status=();
59: # hash of defined params for the current response
60: %Apache::inputtags::params=();
61: # list of all ids, for <import>, these get join()ed and prepended
62: @Apache::inputtags::import=();
63: # list of all import ids seen
64: @Apache::inputtags::importlist=();
65: # just used to note whether we have seen a response that isn't in a part
66: $Apache::inputtags::response_with_no_part=0;
67: }
68:
69: sub check_for_duplicate_ids {
70: my %check;
71: foreach my $id (@Apache::inputtags::partlist,
72: @Apache::inputtags::responselist,
73: @Apache::inputtags::importlist) {
74: $check{$id}++;
75: }
76: my @duplicates;
77: foreach my $id (sort(keys(%check))) {
78: if ($check{$id} > 1) {
79: push(@duplicates,$id);
80: }
81: }
82: if (@duplicates) {
83: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
84: }
85: }
86:
87: sub start_input {
88: my ($parstack,$safeeval)=@_;
89: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
90: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
91: push (@Apache::inputtags::input,$id);
92: push (@Apache::inputtags::inputlist,$id);
93: return $id;
94: }
95:
96: sub end_input {
97: pop @Apache::inputtags::input;
98: return '';
99: }
100:
101: sub addchars {
102: my ($fieldid,$addchars)=@_;
103: my $output='';
104: foreach (split(/\,/,$addchars)) {
105: $output.='<a href="javascript:void(document.forms.lonhomework.'.
106: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
107: }
108: return $output;
109: }
110:
111: sub start_textfield {
112: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
113: my $result = "";
114: my $id = &start_input($parstack,$safeeval);
115: my $resid=$Apache::inputtags::response[-1];
116: if ($target eq 'web') {
117: $Apache::lonxml::evaluate--;
118: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
119: my $partid=$Apache::inputtags::part;
120: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"});
121: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
122: if ( $cols eq '') { $cols = 80; }
123: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
124: if ( $rows eq '') { $rows = 10; }
125: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
126: $result='';
127: if ($addchars) {
128: $result.=&addchars('HWVAL_'.$resid,$addchars);
129: }
130: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" '.
131: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
132: if ($oldresponse ne '') {
133: #get rid of any startup text if the user has already responded
134: &Apache::lonxml::get_all_text("/textfield",$parser);
135: }
136: } else {
137: #right or wrong don't show it
138: #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
139: $result='';
140: #get rid of any startup text
141: &Apache::lonxml::get_all_text("/textfield",$parser);
142: }
143: } elsif ($target eq 'grade') {
144: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
145: if ($seedtext eq $ENV{'form.HWVAL_'.$resid}) {
146: # if the seed text is still there it wasn't a real submission
147: $ENV{'form.HWVAL_'.$resid}='';
148: }
149: } elsif ($target eq 'edit') {
150: $result.=&Apache::edit::tag_start($target,$token);
151: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
152: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
153: $result.=&Apache::edit::text_arg
154: ('Click-On Texts (comma sep):','addchars',$token,10);
155: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
156: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
157: } elsif ($target eq 'modified') {
158: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
159: $safeeval,'rows','cols','addchars');
160: if ($constructtag) {
161: $result = &Apache::edit::rebuild_tag($token);
162: } else {
163: $result=$token->[4];
164: }
165: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
166: } elsif ($target eq 'tex') {
167: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
168: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
169: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
170: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
171: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
172: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
173: } else {
174: my $TeXwidth=$width_of_box/80;
175: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
176: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
177: $result.='}}}\vskip 2 mm ';
178: }
179: }
180: return $result;
181: }
182:
183: sub end_textfield {
184: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
185: my $result;
186: if ($target eq 'web') {
187: $Apache::lonxml::evaluate++;
188: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
189: return "</textarea>";
190: }
191: } elsif ($target eq 'edit') {
192: $result=&Apache::edit::end_table();
193: }
194: &end_input;
195: return $result;
196: }
197:
198: sub start_textline {
199: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
200: my $result = "";
201: if ($target eq 'web') {
202: $Apache::lonxml::evaluate--;
203: my $partid=$Apache::inputtags::part;
204: my $id=$Apache::inputtags::response[-1];
205: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
206: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
207: my $maxlength;
208: if ($size eq '') { $size=20; } else {
209: if ($size < 20) { $maxlength=$size; }
210: }
211: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
212: if ($Apache::lonhomework::type ne 'exam') {
213: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
214: $result='';
215: if ($addchars) {
216: $result.=&addchars('HWVAL_'.$id,$addchars);
217: }
218: $result.= '<input type="text" name="HWVAL_'.$id.'" value="'.
219: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
220: }
221: } else {
222: #right or wrong don't show what was last typed in.
223: #$result='<i>'.$oldresponse.'</i>';
224: $result='';
225: }
226: } elsif ($target eq 'edit') {
227: $result=&Apache::edit::tag_start($target,$token);
228: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
229: &Apache::edit::text_arg
230: ('Click-On Texts (comma sep):','addchars',$token,10)."</td></tr>";
231: $result.=&Apache::edit::end_table;
232: } elsif ($target eq 'modified') {
233: my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size','addchars');
234: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
235: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
236: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
237: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
238: $result='\framebox['.$size.'][s]{\tiny\strut}';
239: }
240: return $result;
241: }
242:
243: sub end_textline {
244: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
245: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
246: elsif ($target eq 'edit') { return ('','no'); }
247: return "";
248: }
249:
250: sub start_hiddenline {
251: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
252: my $result = "";
253: if ($target eq 'web') {
254: $Apache::lonxml::evaluate--;
255: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
256: my $partid=$Apache::inputtags::part;
257: my $id=$Apache::inputtags::response[-1];
258: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
259: if ($Apache::lonhomework::type ne 'exam') {
260: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
261: $oldresponse.'" />';
262: }
263: }
264: } elsif ($target eq 'edit') {
265: $result=&Apache::edit::tag_start($target,$token);
266: $result.=&Apache::edit::end_table;
267: }
268: return $result;
269: }
270:
271: sub end_hiddenline {
272: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
273: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
274: elsif ($target eq 'edit') { return ('','no'); }
275: return "";
276: }
277:
278: sub finalizeawards {
279: my $result='';
280: my $award;
281: if ($#_ == '-1') { $result = "NO_RESPONSE"; }
282: if ($result eq '' ) {
283: my $blankcount;
284: foreach $award (@_) {
285: if ($award eq '') {
286: $result='MISSING_ANSWER';
287: $blankcount++;
288: }
289: }
290: if ($blankcount == ($#_ + 1)) { $result = 'NO_RESPONSE'; }
291: }
292: if ($result eq '' ) {
293: foreach $award (@_) { if ($award eq 'MISSING_ANSWER') {$result='MISSING_ANSWER'; last;}}
294: }
295: if ($result eq '' ) {
296: foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
297: }
298: if ($result eq '' ) {
299: foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
300: }
301: if ($result eq '' ) {
302: foreach $award (@_) { if ($award eq 'TOO_LONG') {$result='TOO_LONG'; last;}}
303: }
304: if ($result eq '' ) {
305: foreach $award (@_) {
306: if ($award eq 'UNIT_FAIL' ||
307: $award eq 'NO_UNIT' ||
308: $award eq 'UNIT_NOTNEEDED') {
309: $result=$award; last;
310: }
311: }
312: }
313: if ($result eq '' ) {
314: foreach $award (@_) {
315: if ($award eq 'WANTED_NUMERIC' ||
316: $award eq 'BAD_FORMULA') {$result=$award; last;}
317: }
318: }
319: if ($result eq '' ) {
320: foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
321: }
322: if ($result eq '' ) {
323: foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
324: }
325: if ($result eq '' ) {
326: foreach $award (@_) { if ($award eq 'MISORDERED_RANK') {$result=$award; last;} }
327: }
328: if ($result eq '' ) {
329: foreach $award (@_) { if ($award eq 'INVALID_FILETYPE') {$result=$award; last;} }
330: }
331: if ($result eq '' ) {
332: foreach $award (@_) { if ($award eq 'DRAFT') {$result=$award; last;} }
333: }
334: if ($result eq '' ) {
335: foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
336: }
337: if ($result eq '' ) {
338: foreach $award (@_) { if ($award eq 'ASSIGNED_SCORE') {$result=$award; last;} }
339: }
340: if ($result eq '' ) {
341: foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
342: }
343: if ($result eq '' ) { $result='EXACT_ANS'; }
344: return $result
345: }
346:
347: sub decideoutput {
348: my ($award,$solved,$previous,$target)=@_;
349: my $message='';
350: my $button=0;
351: my $previousmsg;
352: my $bgcolor='orange';
353: my %possiblecolors =
354: ( 'correct' => '#aaffaa',
355: 'charged_try' => '#ffaaaa',
356: 'not_charged_try' => '#ffffaa',
357: 'no_message' => '#fffff',
358: );
359: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
360:
361: if ($solved =~ /^correct/) {
362: if ($award eq 'ASSIGNED_SCORE') {
363: $message = &mt("A score has been assigned.");
364: } else {
365: if ($target eq 'tex') {
366: $message = '\textbf{'.&mt('You are correct.').'}';
367: } else {
368: $message = "<b>".&mt('You are correct.')."</b>";
369: }
370: unless ($ENV{'course.'.
371: $ENV{'request.course.id'}.
372: '.disable_receipt_display'} eq 'yes') {
373: $message.=(($target eq 'web')?'<br />':' ').
374: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
375: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
376: }
377: }
378: $bgcolor=$possiblecolors{'correct'};
379: $button=0;
380: $previousmsg='';
381: } elsif ($solved =~ /^excused/) {
382: if ($target eq 'tex') {
383: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
384: } else {
385: $message = "<b>".&mt('You are excused from the problem.')."</b>";
386: }
387: $bgcolor=$possiblecolors{'charged_try'};
388: $button=0;
389: $previousmsg='';
390: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
391: if ($solved =~ /^incorrect/ || $solved eq '') {
392: $message = &mt("Incorrect");
393: $bgcolor=$possiblecolors{'charged_try'};
394: $button=1;
395: } else {
396: $message = "<b>".&mt('You are correct.')."</b>";
397: unless ($ENV{'course.'.
398: $ENV{'request.course.id'}.
399: '.disable_receipt_display'} eq 'yes') {
400: $message.=(($target eq 'web')?'<br />':' ').
401: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
402: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
403: }
404: $bgcolor=$possiblecolors{'correct'};
405: $button=0;
406: $previousmsg='';
407: }
408: } elsif ($award eq 'NO_RESPONSE') {
409: $message = '';
410: $bgcolor=$possiblecolors{'no_feedback'};
411: $button=1;
412: } elsif ($award eq 'MISSING_ANSWER') {
413: $message = &mt('Some items were not submitted.');
414: $bgcolor=$possiblecolors{'not_charged_try'};
415: $button = 1;
416: } elsif ($award eq 'ERROR') {
417: $message = &mt('An error occured while grading your answer.');
418: $bgcolor=$possiblecolors{'not_charged_try'};
419: $button = 1;
420: } elsif ($award eq 'TOO_LONG') {
421: $message = &mt("The submitted answer was too long.");
422: $bgcolor=$possiblecolors{'not_charged_try'};
423: $button=1;
424: } elsif ($award eq 'WANTED_NUMERIC') {
425: $message = &mt("This question expects a numeric answer.");
426: $bgcolor=$possiblecolors{'not_charged_try'};
427: $button=1;
428: } elsif ($award eq 'MISORDERED_RANK') {
429: $message = &mt('You have provided an invalid ranking');
430: if ($target ne 'tex') {
431: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
432: }
433: $bgcolor=$possiblecolors{'not_charged_try'};
434: $button=1;
435: } elsif ($award eq 'INVALID_FILETYPE') {
436: $message = &mt('The filetype extension of the file you uploaded is not allowed.');
437: $bgcolor=$possiblecolors{'not_charged_try'};
438: $button=1;
439: } elsif ($award eq 'SIG_FAIL') {
440: $message = &mt("Significant figures are incorrect, submission was not graded.");# you provided %s significant figures";
441: $bgcolor=$possiblecolors{'not_charged_try'};
442: $button=1;
443: } elsif ($award eq 'UNIT_FAIL') {
444: $message = &mt("Units incorrect.");
445: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} #Computer reads units as %s";
446: $bgcolor=$possiblecolors{'not_charged_try'};
447: $button=1;
448: } elsif ($award eq 'UNIT_NOTNEEDED') {
449: $message = &mt("Only a number required.");# Computer reads units of %s";
450: $bgcolor=$possiblecolors{'not_charged_try'};
451: $button=1;
452: } elsif ($award eq 'NO_UNIT') {
453: $message = &mt("Units required");
454: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
455: $bgcolor=$possiblecolors{'not_charged_try'};
456: $button=1;
457: } elsif ($award eq 'BAD_FORMULA') {
458: $message = &mt("Unable to understand formula");
459: $bgcolor=$possiblecolors{'not_charged_try'};
460: $button=1;
461: } elsif ($award eq 'INCORRECT') {
462: $message = &mt("Incorrect");
463: $bgcolor=$possiblecolors{'charged_try'};
464: $button=1;
465: } elsif ($award eq 'SUBMITTED') {
466: $message = &mt("Your submission has been recorded.");
467: $bgcolor=$possiblecolors{'correct'};
468: $button=1;
469: } elsif ($award eq 'DRAFT') {
470: $message = "A draft copy has been saved.";
471: $bgcolor=$possiblecolors{'not_charged_try'};
472: $button=1;
473: } elsif ($award eq 'ASSIGNED_SCORE') {
474: $message = "A score has been assigned.";
475: $bgcolor=$possiblecolors{'correct'};
476: $button=0;
477: } else {
478: $message = &mt("Unknown message").": $award";
479: $button=1;
480: }
481: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
482: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
483: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
484: $bgcolor=$possiblecolors{'correct'};
485: $button=1;
486: }
487: return ($button,$bgcolor,$message,$previousmsg);
488: }
489:
490: sub removealldata {
491: my ($id)=@_;
492: foreach my $key (keys(%Apache::lonhomework::results)) {
493: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
494: &Apache::lonxml::debug("Removing $key");
495: delete($Apache::lonhomework::results{$key});
496: }
497: }
498: }
499:
500: sub setgradedata {
501: my ($award,$id,$previously_used) = @_;
502: # if the student already has it correct, don't modify the status
503: if (!$Apache::lonhomework::scantronmode &&
504: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
505: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
506: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
507: return '';
508: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
509: /^correct/ || $Apache::lonhomework::scantronmode ||
510: lc($Apache::lonhomework::problemstatus) eq 'no') {
511: #handle assignment of tries and solved status
512: my $solvemsg;
513: if ($Apache::lonhomework::scantronmode) {
514: $solvemsg='correct_by_scantron';
515: } else {
516: $solvemsg='correct_by_student';
517: }
518: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
519: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
520: }
521: if ( $award eq 'ASSIGNED_SCORE') {
522: $Apache::lonhomework::results{"resource.$id.tries"} =
523: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
524: $Apache::lonhomework::results{"resource.$id.solved"} =
525: $solvemsg;
526: my $numawards=scalar(@Apache::inputtags::response);
527: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
528: foreach my $res (@Apache::inputtags::response) {
529: $Apache::lonhomework::results{"resource.$id.awarded"}+=
530: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
531: }
532: if ($numawards > 0) {
533: $Apache::lonhomework::results{"resource.$id.awarded"}/=
534: $numawards;
535: }
536: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
537: $Apache::lonhomework::results{"resource.$id.tries"} =
538: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
539: $Apache::lonhomework::results{"resource.$id.solved"} =
540: $solvemsg;
541: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
542: } elsif ( $award eq 'INCORRECT' ) {
543: $Apache::lonhomework::results{"resource.$id.tries"} =
544: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
545: if (lc($Apache::lonhomework::problemstatus) eq 'no') {
546: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
547: }
548: $Apache::lonhomework::results{"resource.$id.solved"} =
549: 'incorrect_attempted'
550: } elsif ( $award eq 'SUBMITTED' ) {
551: $Apache::lonhomework::results{"resource.$id.tries"} =
552: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
553: $Apache::lonhomework::results{"resource.$id.solved"} =
554: 'ungraded_attempted';
555: } elsif ( $award eq 'DRAFT' ) {
556: $Apache::lonhomework::results{"resource.$id.solved"} = '';
557: } elsif ( $award eq 'NO_RESPONSE' ) {
558: #no real response so delete any data that got stored
559: &removealldata($id);
560: return '';
561: } else {
562: $Apache::lonhomework::results{"resource.$id.solved"} =
563: 'incorrect_attempted';
564: if (lc($Apache::lonhomework::problemstatus) eq 'no') {
565: $Apache::lonhomework::results{"resource.$id.tries"} =
566: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
567: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
568: }
569: }
570:
571: # did either of the overall awards chage? If so ignore the
572: # previous check
573: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
574: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
575: ($Apache::lonhomework::results{"resource.$id.solved"} eq
576: $Apache::lonhomework::history{"resource.$id.solved"})) {
577: # check if this was a previous submission if it was delete the
578: # unneeded data and update the previously_used attribute
579: if ( $previously_used eq 'PREVIOUSLY_USED') {
580: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
581: delete($Apache::lonhomework::results{"resource.$id.tries"});
582: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
583: }
584: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
585: #delete all data as they student didn't do anything, but save
586: #the list of collaborators.
587: &removealldata($id);
588: #and since they didn't do anything we were never here
589: return '';
590: } else {
591: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
592: }
593: }
594: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
595: /^correct/ ) {
596: #delete all data as they student already has it correct
597: &removealldata($id);
598: #and since they didn't do anything we were never here
599: return '';
600: }
601: $Apache::lonhomework::results{"resource.$id.award"} = $award;
602: }
603:
604: sub grade {
605: my ($target) = @_;
606: my $id = $Apache::inputtags::part;
607: my $response='';
608: if ( defined $ENV{'form.submitted'}) {
609: my @awards = ();
610: foreach $response (@Apache::inputtags::response) {
611: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
612: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
613: &Apache::lonxml::debug("keeping $value from $response for $id");
614: push (@awards,$value);
615: }
616: my $finalaward = &finalizeawards(@awards);
617: my $previously_used;
618: if ( $#Apache::inputtags::previous eq $#awards ) {
619: my $match=0;
620: my @matches;
621: foreach my $versionar (@Apache::inputtags::previous_version) {
622: foreach my $version (@$versionar) {
623: $matches[$version]++;
624: }
625: }
626: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
627: if ($match) {
628: $previously_used = 'PREVIOUSLY_LAST';
629: foreach my $value (@Apache::inputtags::previous) {
630: if ($value eq 'PREVIOUSLY_USED' ) {
631: $previously_used = $value;
632: last;
633: }
634: }
635: }
636: }
637: &Apache::lonxml::debug("final award $finalaward, $previously_used");
638: &setgradedata($finalaward,$id,$previously_used);
639: }
640: return '';
641: }
642:
643: sub gradestatus {
644: my ($id,$target) = @_;
645: my $showbutton = 1;
646: my $bgcolor = '';
647: my $message = '';
648: my $latemessage = '';
649: my $trystr='';
650: my $button='';
651: my $previousmsg='';
652:
653: my $status = $Apache::inputtags::status['-1'];
654: &Apache::lonxml::debug("gradestatus has :$status:");
655: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
656: $status ne 'INVALID_ACCESS') {
657: my $award = $Apache::lonhomework::history{"resource.$id.award"};
658: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
659: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
660: &Apache::lonxml::debug("Found Award |$award|$solved|");
661: if ( $award ne '' || $solved ne '') {
662: &Apache::lonxml::debug('Getting message');
663: ($showbutton,$bgcolor,$message,$previousmsg) =
664: &decideoutput($award,$solved,$previous,$target);
665: if ($target eq 'tex') {
666: $message='\vskip 2 mm '.$message.' ';
667: } else {
668: $message="<td bgcolor=\"$bgcolor\">$message</td>";
669: if ($previousmsg) {
670: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
671: }
672: }
673: }
674: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
675: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
676: &Apache::lonxml::debug("got maxtries of :$maxtries:");
677: #if tries are set to negative turn off the Tries/Button and messages
678: if (defined($maxtries) && $maxtries < 0) { return ''; }
679: if ( $tries eq '' ) { $tries = '0'; }
680: if ( $maxtries eq '' ) { $maxtries = '2'; }
681: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
682: my $tries_text=&mt('Tries');
683: if ( $Apache::lonhomework::type eq 'survey') { $tries_text=&mt('Submissions'); }
684: if ( $showbutton ) {
685: if ($target eq 'tex') {
686: if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam') {
687: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
688: } else {
689: $trystr = '\vskip 0 mm ';
690: }
691: } else {
692: $trystr = "<td>".$tries_text." $tries";
693: if($ENV{'request.state'} ne 'construct') {
694: $trystr.="/$maxtries";
695: } else {
696: if (defined($Apache::inputtags::params{'maxtries'})) {
697: $trystr.="/".$Apache::inputtags::params{'maxtries'};
698: }
699: }
700: $trystr.="</td>";
701: }
702: }
703: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
704: if ( $showbutton ) {
705: if ($target ne 'tex') {
706: $button = '<input type="submit" name="submit" value="'.&mt('Submit Answer').'" />';
707: }
708: }
709: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
710: #last submissions was after due date
711: if ($target eq 'tex') {
712: $latemessage=' The last submission was after the Due Date ';
713: } else {
714: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
715: }
716: }
717: }
718: my $output= $previousmsg.$latemessage.$message.$trystr;
719: if ($output =~ /^\s*$/) {
720: return $button;
721: } else {
722: if ($target eq 'tex') {
723: return $button.' \vskip 0 mm '.$output.' ';
724: } else {
725: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
726: }
727: }
728: }
729: 1;
730: __END__
731:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>