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