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