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