Annotation of loncom/homework/inputtags.pm, revision 1.144
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.144 ! albertel 4: # $Id: inputtags.pm,v 1.143 2004/06/03 14:28:39 www 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',
320: 'APPROX_ANS', 'EXACT_ANS') {
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';
333: my %possiblecolors =
334: ( 'correct' => '#aaffaa',
335: 'charged_try' => '#ffaaaa',
336: 'not_charged_try' => '#ffffaa',
337: 'no_message' => '#fffff',
338: );
339: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
340:
341: if ($solved =~ /^correct/) {
342: if ($award eq 'ASSIGNED_SCORE') {
343: $message = &mt("A score has been assigned.");
344: } else {
345: if ($target eq 'tex') {
346: $message = '\textbf{'.&mt('You are correct.').'}';
347: } else {
348: $message = "<b>".&mt('You are correct.')."</b>";
349: }
1.144 ! albertel 350: $message.= " ".&mt(" Computer's answer now shown.");
1.135 albertel 351: unless ($ENV{'course.'.
352: $ENV{'request.course.id'}.
353: '.disable_receipt_display'} eq 'yes') {
354: $message.=(($target eq 'web')?'<br />':' ').
355: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
356: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
357: }
358: }
359: $bgcolor=$possiblecolors{'correct'};
360: $button=0;
361: $previousmsg='';
362: } elsif ($solved =~ /^excused/) {
363: if ($target eq 'tex') {
364: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
365: } else {
366: $message = "<b>".&mt('You are excused from the problem.')."</b>";
367: }
368: $bgcolor=$possiblecolors{'charged_try'};
369: $button=0;
370: $previousmsg='';
371: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
372: if ($solved =~ /^incorrect/ || $solved eq '') {
1.144 ! albertel 373: $message = &mt("Incorrect").".";
1.135 albertel 374: $bgcolor=$possiblecolors{'charged_try'};
375: $button=1;
376: } else {
1.144 ! albertel 377: if ($target eq 'tex') {
! 378: $message = '\textbf{'.&mt('You are correct.').'}';
! 379: } else {
! 380: $message = "<b>".&mt('You are correct.')."</b>";
! 381: }
! 382: $message.= " ".&mt(" Computer's answer now shown.");
1.135 albertel 383: unless ($ENV{'course.'.
384: $ENV{'request.course.id'}.
385: '.disable_receipt_display'} eq 'yes') {
386: $message.=(($target eq 'web')?'<br />':' ').
387: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
388: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
389: }
390: $bgcolor=$possiblecolors{'correct'};
391: $button=0;
392: $previousmsg='';
393: }
394: } elsif ($award eq 'NO_RESPONSE') {
395: $message = '';
396: $bgcolor=$possiblecolors{'no_feedback'};
397: $button=1;
398: } elsif ($award eq 'MISSING_ANSWER') {
399: $message = &mt('Some items were not submitted.');
400: $bgcolor=$possiblecolors{'not_charged_try'};
401: $button = 1;
402: } elsif ($award eq 'ERROR') {
403: $message = &mt('An error occured while grading your answer.');
404: $bgcolor=$possiblecolors{'not_charged_try'};
405: $button = 1;
406: } elsif ($award eq 'TOO_LONG') {
407: $message = &mt("The submitted answer was too long.");
408: $bgcolor=$possiblecolors{'not_charged_try'};
409: $button=1;
410: } elsif ($award eq 'WANTED_NUMERIC') {
411: $message = &mt("This question expects a numeric answer.");
412: $bgcolor=$possiblecolors{'not_charged_try'};
413: $button=1;
414: } elsif ($award eq 'MISORDERED_RANK') {
415: $message = &mt('You have provided an invalid ranking');
416: if ($target ne 'tex') {
417: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
418: }
419: $bgcolor=$possiblecolors{'not_charged_try'};
420: $button=1;
421: } elsif ($award eq 'INVALID_FILETYPE') {
422: $message = &mt('The filetype extension of the file you uploaded is not allowed.');
423: $bgcolor=$possiblecolors{'not_charged_try'};
424: $button=1;
425: } elsif ($award eq 'SIG_FAIL') {
1.136 albertel 426: $message = &mt("Significant figures are incorrect, you provided [_1] significant figures while [_2] to [_3] were expected. Submission not graded.",(split(/:/,$awardmsg)));
1.135 albertel 427: $bgcolor=$possiblecolors{'not_charged_try'};
428: $button=1;
1.137 albertel 429: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
430: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
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 'UNIT_INVALID_STUDENT') {
1.144 ! albertel 435: $message = &mt('Unable to interpret units. Computer reads units as "[_1]"','<tt>'.$awardmsg.'</tt>.');
1.137 albertel 436: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
437: $bgcolor=$possiblecolors{'not_charged_try'};
438: $button=1;
1.140 matthew 439: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.144 ! albertel 440: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units ','<tt>'.$awardmsg.'</tt>.');
1.136 albertel 441: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.135 albertel 442: $bgcolor=$possiblecolors{'not_charged_try'};
443: $button=1;
444: } elsif ($award eq 'UNIT_NOTNEEDED') {
1.144 ! albertel 445: $message = &mt('Only a number required. Computer reads units of "[_1]"','<tt>'.$awardmsg.'</tt>.');
1.135 albertel 446: $bgcolor=$possiblecolors{'not_charged_try'};
447: $button=1;
448: } elsif ($award eq 'NO_UNIT') {
1.144 ! albertel 449: $message = &mt("Units required").'.';
1.135 albertel 450: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
451: $bgcolor=$possiblecolors{'not_charged_try'};
452: $button=1;
453: } elsif ($award eq 'BAD_FORMULA') {
454: $message = &mt("Unable to understand formula");
455: $bgcolor=$possiblecolors{'not_charged_try'};
456: $button=1;
457: } elsif ($award eq 'INCORRECT') {
1.144 ! albertel 458: $message = &mt("Incorrect").'.';
1.135 albertel 459: $bgcolor=$possiblecolors{'charged_try'};
460: $button=1;
461: } elsif ($award eq 'SUBMITTED') {
462: $message = &mt("Your submission has been recorded.");
463: $bgcolor=$possiblecolors{'correct'};
464: $button=1;
465: } elsif ($award eq 'DRAFT') {
1.144 ! albertel 466: $message = &mt("A draft copy has been saved.");
1.135 albertel 467: $bgcolor=$possiblecolors{'not_charged_try'};
468: $button=1;
469: } elsif ($award eq 'ASSIGNED_SCORE') {
1.144 ! albertel 470: $message = &mt("A score has been assigned.");
1.135 albertel 471: $bgcolor=$possiblecolors{'correct'};
472: $button=0;
1.144 ! albertel 473: } elsif ($award eq '') {
! 474: $bgcolor=$possiblecolors{'not_charged_try'};
! 475: $button=1;
1.135 albertel 476: } else {
477: $message = &mt("Unknown message").": $award";
478: $button=1;
479: }
480: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
481: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
482: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
483: $bgcolor=$possiblecolors{'correct'};
484: $button=1;
485: }
1.144 ! albertel 486: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
! 487: $message.=" ".&mt("Computer's answer now shown.");
! 488: }
1.135 albertel 489: return ($button,$bgcolor,$message,$previousmsg);
1.12 albertel 490: }
491:
1.88 albertel 492: sub removealldata {
1.87 albertel 493: my ($id)=@_;
494: foreach my $key (keys(%Apache::lonhomework::results)) {
495: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
496: &Apache::lonxml::debug("Removing $key");
497: delete($Apache::lonhomework::results{$key});
498: }
499: }
500: }
501:
1.142 albertel 502: sub hidealldata {
503: my ($id)=@_;
504: foreach my $key (keys(%Apache::lonhomework::results)) {
505: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
506: &Apache::lonxml::debug("Hidding $key");
507: my $newkey=$key;
508: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
509: $Apache::lonhomework::results{$newkey}=
510: $Apache::lonhomework::results{$key};
511: delete($Apache::lonhomework::results{$key});
512: }
513: }
514: }
515:
1.12 albertel 516: sub setgradedata {
1.136 albertel 517: my ($award,$msg,$id,$previously_used) = @_;
1.135 albertel 518: # if the student already has it correct, don't modify the status
1.141 albertel 519: if ($Apache::lonhomework::scantronmode && defined($ENV{'form.CODE'})) {
520: $Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
521: }
1.135 albertel 522: if (!$Apache::lonhomework::scantronmode &&
523: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
524: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
525: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 526: return '';
1.135 albertel 527: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
528: /^correct/ || $Apache::lonhomework::scantronmode ||
529: lc($Apache::lonhomework::problemstatus) eq 'no') {
530: #handle assignment of tries and solved status
531: my $solvemsg;
532: if ($Apache::lonhomework::scantronmode) {
533: $solvemsg='correct_by_scantron';
534: } else {
535: $solvemsg='correct_by_student';
536: }
537: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
538: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
539: }
540: if ( $award eq 'ASSIGNED_SCORE') {
541: $Apache::lonhomework::results{"resource.$id.tries"} =
542: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
543: $Apache::lonhomework::results{"resource.$id.solved"} =
544: $solvemsg;
545: my $numawards=scalar(@Apache::inputtags::response);
546: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
547: foreach my $res (@Apache::inputtags::response) {
548: $Apache::lonhomework::results{"resource.$id.awarded"}+=
549: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
550: }
551: if ($numawards > 0) {
552: $Apache::lonhomework::results{"resource.$id.awarded"}/=
553: $numawards;
554: }
555: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
556: $Apache::lonhomework::results{"resource.$id.tries"} =
557: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
558: $Apache::lonhomework::results{"resource.$id.solved"} =
559: $solvemsg;
560: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
561: } elsif ( $award eq 'INCORRECT' ) {
562: $Apache::lonhomework::results{"resource.$id.tries"} =
563: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
564: if (lc($Apache::lonhomework::problemstatus) eq 'no') {
565: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
566: }
567: $Apache::lonhomework::results{"resource.$id.solved"} =
568: 'incorrect_attempted';
569: } elsif ( $award eq 'SUBMITTED' ) {
570: $Apache::lonhomework::results{"resource.$id.tries"} =
571: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
572: $Apache::lonhomework::results{"resource.$id.solved"} =
573: 'ungraded_attempted';
574: } elsif ( $award eq 'DRAFT' ) {
575: $Apache::lonhomework::results{"resource.$id.solved"} = '';
576: } elsif ( $award eq 'NO_RESPONSE' ) {
577: #no real response so delete any data that got stored
1.129 albertel 578: &removealldata($id);
579: return '';
580: } else {
1.135 albertel 581: $Apache::lonhomework::results{"resource.$id.solved"} =
582: 'incorrect_attempted';
583: if (lc($Apache::lonhomework::problemstatus) eq 'no') {
584: $Apache::lonhomework::results{"resource.$id.tries"} =
585: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
586: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
587: }
588: }
1.136 albertel 589: if (defined($msg)) {
590: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
591: }
1.135 albertel 592: # did either of the overall awards chage? If so ignore the
593: # previous check
594: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
595: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
596: ($Apache::lonhomework::results{"resource.$id.solved"} eq
597: $Apache::lonhomework::history{"resource.$id.solved"})) {
598: # check if this was a previous submission if it was delete the
599: # unneeded data and update the previously_used attribute
600: if ( $previously_used eq 'PREVIOUSLY_USED') {
601: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
602: delete($Apache::lonhomework::results{"resource.$id.tries"});
603: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
604: }
605: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
606: #delete all data as they student didn't do anything, but save
607: #the list of collaborators.
608: &removealldata($id);
609: #and since they didn't do anything we were never here
610: return '';
611: } else {
612: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
613: }
1.101 albertel 614: }
1.135 albertel 615: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
616: /^correct/ ) {
617: #delete all data as they student already has it correct
618: &removealldata($id);
619: #and since they didn't do anything we were never here
620: return '';
1.40 albertel 621: }
1.135 albertel 622: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 623: }
624:
1.9 albertel 625: sub grade {
1.135 albertel 626: my ($target) = @_;
627: my $id = $Apache::inputtags::part;
628: my $response='';
629: if ( defined $ENV{'form.submitted'}) {
1.136 albertel 630: my (@awards,@msgs);
1.135 albertel 631: foreach $response (@Apache::inputtags::response) {
632: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
633: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
634: &Apache::lonxml::debug("keeping $value from $response for $id");
635: push (@awards,$value);
1.136 albertel 636: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
637: &Apache::lonxml::debug("got message $value from $response for $id");
638: push (@msgs,$value);
1.135 albertel 639: }
1.136 albertel 640: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135 albertel 641: my $previously_used;
642: if ( $#Apache::inputtags::previous eq $#awards ) {
643: my $match=0;
644: my @matches;
645: foreach my $versionar (@Apache::inputtags::previous_version) {
646: foreach my $version (@$versionar) {
647: $matches[$version]++;
648: }
649: }
650: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
651: if ($match) {
652: $previously_used = 'PREVIOUSLY_LAST';
653: foreach my $value (@Apache::inputtags::previous) {
654: if ($value eq 'PREVIOUSLY_USED' ) {
655: $previously_used = $value;
656: last;
657: }
1.75 albertel 658: }
659: }
1.43 albertel 660: }
1.136 albertel 661: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
662: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 663: }
1.135 albertel 664: return '';
1.1 albertel 665: }
666:
1.11 albertel 667: sub gradestatus {
1.135 albertel 668: my ($id,$target) = @_;
669: my $showbutton = 1;
670: my $bgcolor = '';
671: my $message = '';
672: my $latemessage = '';
673: my $trystr='';
674: my $button='';
675: my $previousmsg='';
676:
677: my $status = $Apache::inputtags::status['-1'];
678: &Apache::lonxml::debug("gradestatus has :$status:");
679: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
680: $status ne 'INVALID_ACCESS') {
681: my $award = $Apache::lonhomework::history{"resource.$id.award"};
682: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
683: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136 albertel 684: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
685: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144 ! albertel 686: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135 albertel 687: &Apache::lonxml::debug('Getting message');
688: ($showbutton,$bgcolor,$message,$previousmsg) =
1.136 albertel 689: &decideoutput($award,$awardmsg,$solved,$previous,$target);
1.144 ! albertel 690: &Apache::lonnet::logthis(" mesage is $message status is $status");
1.135 albertel 691: if ($target eq 'tex') {
692: $message='\vskip 2 mm '.$message.' ';
693: } else {
694: $message="<td bgcolor=\"$bgcolor\">$message</td>";
695: if ($previousmsg) {
696: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
697: }
698: }
699: }
700: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
701: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
702: &Apache::lonxml::debug("got maxtries of :$maxtries:");
703: #if tries are set to negative turn off the Tries/Button and messages
704: if (defined($maxtries) && $maxtries < 0) { return ''; }
705: if ( $tries eq '' ) { $tries = '0'; }
706: if ( $maxtries eq '' ) { $maxtries = '2'; }
707: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
708: my $tries_text=&mt('Tries');
709: if ( $Apache::lonhomework::type eq 'survey') { $tries_text=&mt('Submissions'); }
710: if ( $showbutton ) {
711: if ($target eq 'tex') {
712: if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam') {
713: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
714: } else {
715: $trystr = '\vskip 0 mm ';
716: }
717: } else {
1.136 albertel 718: $trystr = "<td><nobr>".$tries_text." $tries";
1.135 albertel 719: if($ENV{'request.state'} ne 'construct') {
720: $trystr.="/$maxtries";
721: } else {
722: if (defined($Apache::inputtags::params{'maxtries'})) {
723: $trystr.="/".$Apache::inputtags::params{'maxtries'};
724: }
725: }
1.136 albertel 726: $trystr.="</nobr></td>";
1.135 albertel 727: }
728: }
729: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
730: if ( $showbutton ) {
731: if ($target ne 'tex') {
732: $button = '<input type="submit" name="submit" value="'.&mt('Submit Answer').'" />';
733: }
734: }
735: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
736: #last submissions was after due date
737: if ($target eq 'tex') {
738: $latemessage=' The last submission was after the Due Date ';
739: } else {
740: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
741: }
742: }
743: }
744: my $output= $previousmsg.$latemessage.$message.$trystr;
745: if ($output =~ /^\s*$/) {
746: return $button;
1.63 sakharuk 747: } else {
1.135 albertel 748: if ($target eq 'tex') {
749: return $button.' \vskip 0 mm '.$output.' ';
750: } else {
751: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
752: }
1.63 sakharuk 753: }
1.11 albertel 754: }
1.1 albertel 755: 1;
756: __END__
1.43 albertel 757:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>