Annotation of loncom/homework/inputtags.pm, revision 1.125
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.125 ! albertel 4: # $Id: inputtags.pm,v 1.124 2003/11/25 23:04:58 www Exp $
1.47 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
1.1 albertel 27:
28: package Apache::inputtags;
1.55 albertel 29: use HTML::Entities();
1.1 albertel 30: use strict;
1.82 www 31: use Apache::loncommon;
1.115 www 32: use Apache::lonlocal;
1.1 albertel 33:
1.50 harris41 34: BEGIN {
1.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.115 www 481: $message = &mt("Answer Submitted");
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.71 albertel 501: if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
502: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
1.53 albertel 503: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
504: return '';
505: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.101 albertel 506: /^correct/ || $Apache::lonhomework::scantronmode ||
507: lc($Apache::lonhomework::problemstatus) eq 'no') {
1.43 albertel 508: #handle assignment of tries and solved status
1.93 albertel 509: my $solvemsg;
510: if ($Apache::lonhomework::scantronmode) {
511: $solvemsg='correct_by_scantron';
512: } else {
513: $solvemsg='correct_by_student';
514: }
1.53 albertel 515: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
516: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
517: }
1.92 albertel 518: if ( $award eq 'ASSIGNED_SCORE') {
519: $Apache::lonhomework::results{"resource.$id.tries"} =
520: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
521: $Apache::lonhomework::results{"resource.$id.solved"} =
1.93 albertel 522: $solvemsg;
1.103 albertel 523: my $numawards=scalar(@Apache::inputtags::response);
1.92 albertel 524: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1.103 albertel 525: foreach my $res (@Apache::inputtags::response) {
1.92 albertel 526: $Apache::lonhomework::results{"resource.$id.awarded"}+=
527: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
528: }
1.93 albertel 529: if ($numawards > 0) {
530: $Apache::lonhomework::results{"resource.$id.awarded"}/=
531: $numawards;
532: }
1.92 albertel 533: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
1.43 albertel 534: $Apache::lonhomework::results{"resource.$id.tries"} =
535: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
536: $Apache::lonhomework::results{"resource.$id.solved"} =
1.93 albertel 537: $solvemsg;
1.43 albertel 538: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
539: } elsif ( $award eq 'INCORRECT' ) {
540: $Apache::lonhomework::results{"resource.$id.tries"} =
541: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.119 albertel 542: if (lc($Apache::lonhomework::problemstatus) eq 'no') {
1.118 albertel 543: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
544: }
1.43 albertel 545: $Apache::lonhomework::results{"resource.$id.solved"} =
1.59 ng 546: 'incorrect_attempted'
1.43 albertel 547: } elsif ( $award eq 'SUBMITTED' ) {
548: $Apache::lonhomework::results{"resource.$id.tries"} =
549: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
550: $Apache::lonhomework::results{"resource.$id.solved"} =
551: 'ungraded_attempted';
1.59 ng 552: } elsif ( $award eq 'DRAFT' ) {
553: $Apache::lonhomework::results{"resource.$id.solved"} = '';
1.43 albertel 554: } elsif ( $award eq 'NO_RESPONSE' ) {
1.87 albertel 555: #no real response so delete any data that got stored
556: &removealldata($id);
557: return '';
1.43 albertel 558: } else {
559: $Apache::lonhomework::results{"resource.$id.solved"} =
560: 'incorrect_attempted';
1.101 albertel 561: if (lc($Apache::lonhomework::problemstatus) eq 'no') {
562: $Apache::lonhomework::results{"resource.$id.tries"} =
563: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.118 albertel 564: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1.101 albertel 565: }
1.43 albertel 566: }
567:
568: # check if this was a previous submission if it was delete the
569: # unneeded data and update the previously_used attribute
570: if ( $previously_used eq 'PREVIOUSLY_USED') {
1.101 albertel 571: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
572: delete($Apache::lonhomework::results{"resource.$id.tries"});
573: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
574: }
1.43 albertel 575: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1.58 ng 576: #delete all data as they student didn't do anything, but save
577: #the list of collaborators.
1.87 albertel 578: &removealldata($id);
1.43 albertel 579: #and since they didn't do anything we were never here
580: return '';
581: } else {
582: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40 albertel 583: }
1.85 albertel 584: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
585: /^correct/ ) {
586: #delete all data as they student already has it correct
1.87 albertel 587: &removealldata($id);
1.85 albertel 588: #and since they didn't do anything we were never here
589: return '';
1.43 albertel 590: }
591: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 592: }
593:
1.9 albertel 594: sub grade {
1.43 albertel 595: my ($target) = @_;
596: my $id = $Apache::inputtags::part;
597: my $response='';
598: if ( defined $ENV{'form.submitted'}) {
599: my @awards = ();
1.103 albertel 600: foreach $response (@Apache::inputtags::response) {
1.43 albertel 601: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
602: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.54 albertel 603: &Apache::lonxml::debug("keeping $value from $response for $id");
604: push (@awards,$value);
1.43 albertel 605: }
606: my $finalaward = &finalizeawards(@awards);
607: my $previously_used;
608: if ( $#Apache::inputtags::previous eq $#awards ) {
1.75 albertel 609: my $match=0;
610: my @matches;
611: foreach my $versionar (@Apache::inputtags::previous_version) {
612: foreach my $version (@$versionar) {
613: $matches[$version]++;
614: }
615: }
616: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
617: if ($match) {
618: $previously_used = 'PREVIOUSLY_LAST';
619: foreach my $value (@Apache::inputtags::previous) {
620: if ($value eq 'PREVIOUSLY_USED' ) {
621: $previously_used = $value;
622: last;
623: }
624: }
1.43 albertel 625: }
626: }
627: &Apache::lonxml::debug("final award $finalaward, $previously_used");
628: &setgradedata($finalaward,$id,$previously_used);
629: }
630: return '';
1.1 albertel 631: }
632:
1.11 albertel 633: sub gradestatus {
1.63 sakharuk 634: my ($id,$target) = @_;
1.43 albertel 635: my $showbutton = 1;
1.105 albertel 636: my $bgcolor = '';
1.43 albertel 637: my $message = '';
1.53 albertel 638: my $latemessage = '';
1.43 albertel 639: my $trystr='';
640: my $button='';
641: my $previousmsg='';
642:
643: my $status = $Apache::inputtags::status['-1'];
644: &Apache::lonxml::debug("gradestatus has :$status:");
1.121 albertel 645: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
646: $status ne 'INVALID_ACCESS') {
1.43 albertel 647: my $award = $Apache::lonhomework::history{"resource.$id.award"};
648: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
649: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
650: &Apache::lonxml::debug("Found Award |$award|$solved|");
1.112 albertel 651: if ( $award ne '' || $solved ne '') {
1.43 albertel 652: &Apache::lonxml::debug('Getting message');
1.105 albertel 653: ($showbutton,$bgcolor,$message,$previousmsg) =
1.68 sakharuk 654: &decideoutput($award,$solved,$previous,$target);
1.63 sakharuk 655: if ($target eq 'tex') {
656: $message=' '.$message.' ';
657: } else {
1.105 albertel 658: $message="<td bgcolor=\"$bgcolor\">$message</td>";
1.63 sakharuk 659: if ($previousmsg) {
1.105 albertel 660: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
1.63 sakharuk 661: }
1.43 albertel 662: }
663: }
664: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
665: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
666: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1.123 albertel 667: #if tries are set to negative turn off the Tries/Button and messages
668: if (defined($maxtries) && $maxtries < 0) { return ''; }
1.43 albertel 669: if ( $tries eq '' ) { $tries = '0'; }
670: if ( $maxtries eq '' ) { $maxtries = '2'; }
671: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
672: if ( $showbutton ) {
1.63 sakharuk 673: if ($target eq 'tex') {
1.97 sakharuk 674: if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam') {
1.116 www 675: $trystr = ' {\vskip 1 mm \small \textit{'.&mt('Tries').'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
1.83 sakharuk 676: } else {
677: $trystr = '\vskip 0 mm ';
1.67 sakharuk 678: }
1.63 sakharuk 679: } else {
1.125 ! albertel 680: $trystr = "<td>".&mt('Tries')." $tries";
! 681: if($ENV{'request.state'} ne 'construct') { $trystr.="/$maxtries"; }
! 682: $trystr.="</td>";
1.63 sakharuk 683: }
1.43 albertel 684: }
685: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
686: if ( $showbutton ) {
1.63 sakharuk 687: if ($target ne 'tex') {
1.116 www 688: $button = '<br /><input type="submit" name="submit" value="'.&mt('Submit Answer').'" />';
1.63 sakharuk 689: }
1.43 albertel 690: }
1.53 albertel 691: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
692: #last submissions was after due date
1.63 sakharuk 693: if ($target eq 'tex') {
694: $latemessage=' The last submission was after the Due Date ';
695: } else {
696: $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
697: }
1.53 albertel 698: }
1.43 albertel 699: }
1.53 albertel 700: my $output= $previousmsg.$latemessage.$message.$trystr;
1.43 albertel 701: if ($output =~ /^\s*$/) {
702: return $button;
703: } else {
1.63 sakharuk 704: if ($target eq 'tex') {
705: return $button.' \vskip 0 mm '.$output.' ';
706: } else {
707: return $button.'<table><tr>'.$output.'</tr></table>';
708: }
1.43 albertel 709: }
1.11 albertel 710: }
1.1 albertel 711: 1;
712: __END__
1.43 albertel 713:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>