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