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