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