Annotation of loncom/homework/inputtags.pm, revision 1.156
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.156 ! albertel 4: # $Id: inputtags.pm,v 1.155 2005/01/20 20:46:03 albertel Exp $
1.47 albertel 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/
1.1 albertel 27:
28: package Apache::inputtags;
1.55 albertel 29: use HTML::Entities();
1.1 albertel 30: use strict;
1.82 www 31: use Apache::loncommon;
1.115 www 32: use Apache::lonlocal;
1.1 albertel 33:
1.50 harris41 34: BEGIN {
1.135 albertel 35: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1 albertel 36: }
37:
1.43 albertel 38:
1.1 albertel 39: sub initialize_inputtags {
1.135 albertel 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;
1.144 albertel 68: # storage location so the begin <*response> tag can generate the correct
69: # answer string for display by the <textline />
70: %Apache::inputtags::answertxt=();
1.103 albertel 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: }
1.1 albertel 89: }
90:
1.14 albertel 91: sub start_input {
1.135 albertel 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;
1.14 albertel 98: }
99:
100: sub end_input {
1.135 albertel 101: pop @Apache::inputtags::input;
102: return '';
1.14 albertel 103: }
104:
1.124 www 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:
1.48 albertel 115: sub start_textfield {
1.135 albertel 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;
1.138 albertel 124: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135 albertel 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);
1.143 www 128: if ( $rows eq '') { $rows = 16; }
1.135 albertel 129: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
130: $result='';
131: if ($addchars) {
132: $result.=&addchars('HWVAL_'.$resid,$addchars);
133: }
1.143 www 134: push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
135: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135 albertel 136: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
137: if ($oldresponse ne '') {
1.143 www 138:
1.135 albertel 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
1.84 albertel 147: &Apache::lonxml::get_all_text("/textfield",$parser);
1.61 albertel 148: }
1.135 albertel 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: }
1.60 albertel 186: }
1.135 albertel 187: return $result;
1.6 albertel 188: }
189:
1.48 albertel 190: sub end_textfield {
1.135 albertel 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;
1.6 albertel 203: }
204:
1.1 albertel 205: sub start_textline {
1.135 albertel 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: }
1.138 albertel 218: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.135 albertel 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.
1.144 albertel 230: $result='<i>'.$Apache::inputtags::answertxt{$id}.'</i>';
231: #$result='';
1.135 albertel 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;
1.1 albertel 248: }
249:
250: sub end_textline {
1.135 albertel 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 "";
1.9 albertel 255: }
256:
1.98 albertel 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];
1.138 albertel 265: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.98 albertel 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 {
1.135 albertel 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 "";
1.98 albertel 283: }
284:
1.136 albertel 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:
1.9 albertel 295: sub finalizeawards {
1.136 albertel 296: my ($awardref,$msgref)=@_;
297: my $result=undef;
1.135 albertel 298: my $award;
1.136 albertel 299: my $msg;
300: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135 albertel 301: if ($result eq '' ) {
302: my $blankcount;
1.136 albertel 303: foreach $award (@$awardref) {
1.135 albertel 304: if ($award eq '') {
305: $result='MISSING_ANSWER';
306: $blankcount++;
307: }
308: }
1.136 albertel 309: if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
1.135 albertel 310: }
1.136 albertel 311: if (defined($result)) { return ($result,$msg); }
312: foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
1.137 albertel 313: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
314: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
315: 'UNIT_FAIL', 'NO_UNIT',
1.136 albertel 316: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
317: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
318: 'MISORDERED_RANK', 'INVALID_FILETYPE',
319: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
1.153 albertel 320: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
1.136 albertel 321: ($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
322: if (defined($result)) { return ($result,$msg); }
1.135 albertel 323: }
1.136 albertel 324: return ('ERROR',undef);
1.9 albertel 325: }
326:
1.10 albertel 327: sub decideoutput {
1.136 albertel 328: my ($award,$awardmsg,$solved,$previous,$target)=@_;
1.135 albertel 329: my $message='';
330: my $button=0;
331: my $previousmsg;
332: my $bgcolor='orange';
1.148 albertel 333: my $added_computer_text=0;
1.135 albertel 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>";
1.150 albertel 350: $message.=" ".&mt("Computer's answer now shown above.");
1.135 albertel 351: }
1.148 albertel 352: $added_computer_text=1;
1.135 albertel 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 '') {
1.144 albertel 375: $message = &mt("Incorrect").".";
1.135 albertel 376: $bgcolor=$possiblecolors{'charged_try'};
377: $button=1;
378: } else {
1.144 albertel 379: if ($target eq 'tex') {
380: $message = '\textbf{'.&mt('You are correct.').'}';
381: } else {
382: $message = "<b>".&mt('You are correct.')."</b>";
1.150 albertel 383: $message.=" ".&mt("Computer's answer now shown above.");
1.144 albertel 384: }
1.148 albertel 385: $added_computer_text=1;
1.135 albertel 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') {
1.145 albertel 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);
1.135 albertel 434: $bgcolor=$possiblecolors{'not_charged_try'};
435: $button=1;
1.137 albertel 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') {
1.155 albertel 442: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137 albertel 443: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
444: $bgcolor=$possiblecolors{'not_charged_try'};
445: $button=1;
1.140 matthew 446: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155 albertel 447: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136 albertel 448: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.135 albertel 449: $bgcolor=$possiblecolors{'not_charged_try'};
450: $button=1;
451: } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155 albertel 452: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.135 albertel 453: $bgcolor=$possiblecolors{'not_charged_try'};
454: $button=1;
455: } elsif ($award eq 'NO_UNIT') {
1.144 albertel 456: $message = &mt("Units required").'.';
1.135 albertel 457: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
458: $bgcolor=$possiblecolors{'not_charged_try'};
459: $button=1;
1.153 albertel 460: } elsif ($award eq 'COMMA_FAIL') {
461: $message = &mt("Proper comma separation is required").'.';
462: $bgcolor=$possiblecolors{'not_charged_try'};
463: $button=1;
1.135 albertel 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') {
1.144 albertel 469: $message = &mt("Incorrect").'.';
1.135 albertel 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') {
1.144 albertel 477: $message = &mt("A draft copy has been saved.");
1.135 albertel 478: $bgcolor=$possiblecolors{'not_charged_try'};
479: $button=1;
480: } elsif ($award eq 'ASSIGNED_SCORE') {
1.144 albertel 481: $message = &mt("A score has been assigned.");
1.135 albertel 482: $bgcolor=$possiblecolors{'correct'};
483: $button=0;
1.144 albertel 484: } elsif ($award eq '') {
485: $bgcolor=$possiblecolors{'not_charged_try'};
486: $button=1;
1.135 albertel 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: }
1.148 albertel 497: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1.150 albertel 498: !$added_computer_text && $target ne 'tex') {
1.149 albertel 499: $message.=" ".&mt("Computer's answer now shown above.");
1.148 albertel 500: $added_computer_text=1;
1.144 albertel 501: }
1.135 albertel 502: return ($button,$bgcolor,$message,$previousmsg);
1.12 albertel 503: }
504:
1.155 albertel 505: sub markup_unit {
506: my ($unit,$target)=@_;
507: if ($target eq 'tex') {
508: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
509: } else {
510: return "<tt>".$unit."</tt>";
511: }
512: }
513:
1.88 albertel 514: sub removealldata {
1.87 albertel 515: my ($id)=@_;
516: foreach my $key (keys(%Apache::lonhomework::results)) {
517: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
518: &Apache::lonxml::debug("Removing $key");
519: delete($Apache::lonhomework::results{$key});
520: }
521: }
522: }
523:
1.142 albertel 524: sub hidealldata {
525: my ($id)=@_;
526: foreach my $key (keys(%Apache::lonhomework::results)) {
527: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
528: &Apache::lonxml::debug("Hidding $key");
529: my $newkey=$key;
530: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
531: $Apache::lonhomework::results{$newkey}=
532: $Apache::lonhomework::results{$key};
533: delete($Apache::lonhomework::results{$key});
534: }
535: }
536: }
537:
1.12 albertel 538: sub setgradedata {
1.136 albertel 539: my ($award,$msg,$id,$previously_used) = @_;
1.154 albertel 540: if ($Apache::lonhomework::scantronmode &&
541: &Apache::lonnet::validCODE($ENV{'form.CODE'})) {
1.141 albertel 542: $Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
1.154 albertel 543: } elsif ($Apache::lonhomework::scantronmode &&
544: $ENV{'form.CODE'} eq '' &&
545: $Apache::lonhomework::history{"resource.CODE"} ne '') {
546: $Apache::lonhomework::results{"resource.CODE"}='';
1.141 albertel 547: }
1.154 albertel 548:
1.135 albertel 549: if (!$Apache::lonhomework::scantronmode &&
550: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
551: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
552: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 553: return '';
1.135 albertel 554: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
555: /^correct/ || $Apache::lonhomework::scantronmode ||
556: lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154 albertel 557: # the student doesn't already have it correct,
558: # or we are in a mode (scantron orno problem status) where a correct
559: # can become incorrect
560: # handle assignment of tries and solved status
1.135 albertel 561: my $solvemsg;
562: if ($Apache::lonhomework::scantronmode) {
563: $solvemsg='correct_by_scantron';
564: } else {
565: $solvemsg='correct_by_student';
566: }
567: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
568: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
569: }
570: if ( $award eq 'ASSIGNED_SCORE') {
571: $Apache::lonhomework::results{"resource.$id.tries"} =
572: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
573: $Apache::lonhomework::results{"resource.$id.solved"} =
574: $solvemsg;
575: my $numawards=scalar(@Apache::inputtags::response);
576: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
577: foreach my $res (@Apache::inputtags::response) {
578: $Apache::lonhomework::results{"resource.$id.awarded"}+=
579: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
580: }
581: if ($numawards > 0) {
582: $Apache::lonhomework::results{"resource.$id.awarded"}/=
583: $numawards;
584: }
585: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
586: $Apache::lonhomework::results{"resource.$id.tries"} =
587: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
588: $Apache::lonhomework::results{"resource.$id.solved"} =
589: $solvemsg;
590: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
591: } elsif ( $award eq 'INCORRECT' ) {
592: $Apache::lonhomework::results{"resource.$id.tries"} =
593: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.152 albertel 594: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
595: $Apache::lonhomework::scantronmode) {
1.135 albertel 596: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
597: }
598: $Apache::lonhomework::results{"resource.$id.solved"} =
599: 'incorrect_attempted';
600: } elsif ( $award eq 'SUBMITTED' ) {
601: $Apache::lonhomework::results{"resource.$id.tries"} =
602: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
603: $Apache::lonhomework::results{"resource.$id.solved"} =
604: 'ungraded_attempted';
605: } elsif ( $award eq 'DRAFT' ) {
606: $Apache::lonhomework::results{"resource.$id.solved"} = '';
607: } elsif ( $award eq 'NO_RESPONSE' ) {
608: #no real response so delete any data that got stored
1.129 albertel 609: &removealldata($id);
610: return '';
611: } else {
1.135 albertel 612: $Apache::lonhomework::results{"resource.$id.solved"} =
613: 'incorrect_attempted';
1.152 albertel 614: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
615: $Apache::lonhomework::scantronmode) {
1.135 albertel 616: $Apache::lonhomework::results{"resource.$id.tries"} =
617: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
618: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
619: }
620: }
1.136 albertel 621: if (defined($msg)) {
622: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
623: }
1.135 albertel 624: # did either of the overall awards chage? If so ignore the
625: # previous check
626: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
627: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
628: ($Apache::lonhomework::results{"resource.$id.solved"} eq
629: $Apache::lonhomework::history{"resource.$id.solved"})) {
630: # check if this was a previous submission if it was delete the
631: # unneeded data and update the previously_used attribute
632: if ( $previously_used eq 'PREVIOUSLY_USED') {
633: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
634: delete($Apache::lonhomework::results{"resource.$id.tries"});
635: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
636: }
637: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
638: #delete all data as they student didn't do anything, but save
639: #the list of collaborators.
640: &removealldata($id);
641: #and since they didn't do anything we were never here
642: return '';
643: } else {
644: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
645: }
1.101 albertel 646: }
1.135 albertel 647: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
648: /^correct/ ) {
649: #delete all data as they student already has it correct
650: &removealldata($id);
651: #and since they didn't do anything we were never here
652: return '';
1.40 albertel 653: }
1.135 albertel 654: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 655: }
656:
1.9 albertel 657: sub grade {
1.135 albertel 658: my ($target) = @_;
659: my $id = $Apache::inputtags::part;
660: my $response='';
661: if ( defined $ENV{'form.submitted'}) {
1.136 albertel 662: my (@awards,@msgs);
1.135 albertel 663: foreach $response (@Apache::inputtags::response) {
664: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
665: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
666: &Apache::lonxml::debug("keeping $value from $response for $id");
667: push (@awards,$value);
1.136 albertel 668: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
669: &Apache::lonxml::debug("got message $value from $response for $id");
670: push (@msgs,$value);
1.135 albertel 671: }
1.136 albertel 672: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135 albertel 673: my $previously_used;
674: if ( $#Apache::inputtags::previous eq $#awards ) {
675: my $match=0;
676: my @matches;
677: foreach my $versionar (@Apache::inputtags::previous_version) {
678: foreach my $version (@$versionar) {
679: $matches[$version]++;
680: }
681: }
682: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
683: if ($match) {
684: $previously_used = 'PREVIOUSLY_LAST';
685: foreach my $value (@Apache::inputtags::previous) {
686: if ($value eq 'PREVIOUSLY_USED' ) {
687: $previously_used = $value;
688: last;
689: }
1.75 albertel 690: }
691: }
1.43 albertel 692: }
1.136 albertel 693: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
694: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 695: }
1.135 albertel 696: return '';
1.1 albertel 697: }
698:
1.11 albertel 699: sub gradestatus {
1.135 albertel 700: my ($id,$target) = @_;
701: my $showbutton = 1;
702: my $bgcolor = '';
703: my $message = '';
704: my $latemessage = '';
705: my $trystr='';
706: my $button='';
707: my $previousmsg='';
708:
709: my $status = $Apache::inputtags::status['-1'];
710: &Apache::lonxml::debug("gradestatus has :$status:");
711: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
712: $status ne 'INVALID_ACCESS') {
713: my $award = $Apache::lonhomework::history{"resource.$id.award"};
714: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
715: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136 albertel 716: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
717: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144 albertel 718: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135 albertel 719: &Apache::lonxml::debug('Getting message');
720: ($showbutton,$bgcolor,$message,$previousmsg) =
1.136 albertel 721: &decideoutput($award,$awardmsg,$solved,$previous,$target);
1.135 albertel 722: if ($target eq 'tex') {
723: $message='\vskip 2 mm '.$message.' ';
724: } else {
725: $message="<td bgcolor=\"$bgcolor\">$message</td>";
726: if ($previousmsg) {
727: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
728: }
729: }
730: }
731: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
732: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
733: &Apache::lonxml::debug("got maxtries of :$maxtries:");
734: #if tries are set to negative turn off the Tries/Button and messages
735: if (defined($maxtries) && $maxtries < 0) { return ''; }
736: if ( $tries eq '' ) { $tries = '0'; }
737: if ( $maxtries eq '' ) { $maxtries = '2'; }
738: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
739: my $tries_text=&mt('Tries');
740: if ( $Apache::lonhomework::type eq 'survey') { $tries_text=&mt('Submissions'); }
741: if ( $showbutton ) {
742: if ($target eq 'tex') {
1.147 sakharuk 743: if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $ENV{'form.suppress_tries'} ne 'yes') {
1.135 albertel 744: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
745: } else {
746: $trystr = '\vskip 0 mm ';
747: }
748: } else {
1.136 albertel 749: $trystr = "<td><nobr>".$tries_text." $tries";
1.135 albertel 750: if($ENV{'request.state'} ne 'construct') {
751: $trystr.="/$maxtries";
752: } else {
753: if (defined($Apache::inputtags::params{'maxtries'})) {
754: $trystr.="/".$Apache::inputtags::params{'maxtries'};
755: }
756: }
1.136 albertel 757: $trystr.="</nobr></td>";
1.135 albertel 758: }
759: }
760: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
761: if ( $showbutton ) {
762: if ($target ne 'tex') {
1.156 ! albertel 763: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135 albertel 764: }
765: }
766: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
767: #last submissions was after due date
768: if ($target eq 'tex') {
769: $latemessage=' The last submission was after the Due Date ';
770: } else {
771: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
772: }
773: }
774: }
775: my $output= $previousmsg.$latemessage.$message.$trystr;
776: if ($output =~ /^\s*$/) {
777: return $button;
1.63 sakharuk 778: } else {
1.135 albertel 779: if ($target eq 'tex') {
780: return $button.' \vskip 0 mm '.$output.' ';
781: } else {
782: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
783: }
1.63 sakharuk 784: }
1.11 albertel 785: }
1.1 albertel 786: 1;
787: __END__
1.43 albertel 788:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>