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