Annotation of loncom/homework/inputtags.pm, revision 1.174
1.43 albertel 1: # The LearningOnline Network with CAPA
2: # input definitons
1.47 albertel 3: #
1.174 ! albertel 4: # $Id: inputtags.pm,v 1.173 2005/08/02 18:05:25 albertel Exp $
1.47 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
1.1 albertel 27:
28: package Apache::inputtags;
1.55 albertel 29: use HTML::Entities();
1.1 albertel 30: use strict;
1.82 www 31: use Apache::loncommon;
1.115 www 32: use Apache::lonlocal;
1.165 albertel 33: use Apache::lonnet;
1.1 albertel 34:
1.50 harris41 35: BEGIN {
1.135 albertel 36: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1 albertel 37: }
38:
1.43 albertel 39:
1.1 albertel 40: sub initialize_inputtags {
1.135 albertel 41: # list of current input ids
42: @Apache::inputtags::input=();
43: # list of all input ids seen in this problem
44: @Apache::inputtags::inputlist=();
45: # list of all current response ids
1.174 ! albertel 46: @Apache::inputtags::response=();
1.135 albertel 47: # list of all response ids seen in this problem
48: @Apache::inputtags::responselist=();
1.173 albertel 49: # list of all current hint ids
1.174 ! albertel 50: @Apache::inputtags::hint=();
1.173 albertel 51: # list of all hint ids seen in this problem
52: @Apache::inputtags::hintlist=();
1.135 albertel 53: # list of whether or not a specific response was previously used
54: @Apache::inputtags::previous=();
55: # submission it was used in
56: @Apache::inputtags::previous_version=();
57: # id of current part, 0 means that no part is current
58: # (inside <problem> only
59: $Apache::inputtags::part='';
60: # list of all part ids seen
61: @Apache::inputtags::partlist=();
62: # list of problem date statuses, the first element is for <problem>
63: # if there is a second element it is for the current <part>
64: @Apache::inputtags::status=();
65: # hash of defined params for the current response
66: %Apache::inputtags::params=();
67: # list of all ids, for <import>, these get join()ed and prepended
68: @Apache::inputtags::import=();
69: # list of all import ids seen
70: @Apache::inputtags::importlist=();
71: # just used to note whether we have seen a response that isn't in a part
72: $Apache::inputtags::response_with_no_part=0;
1.144 albertel 73: # storage location so the begin <*response> tag can generate the correct
74: # answer string for display by the <textline />
75: %Apache::inputtags::answertxt=();
1.103 albertel 76: }
77:
78: sub check_for_duplicate_ids {
79: my %check;
80: foreach my $id (@Apache::inputtags::partlist,
81: @Apache::inputtags::responselist,
1.173 albertel 82: @Apache::inputtags::hintlist,
1.103 albertel 83: @Apache::inputtags::importlist) {
84: $check{$id}++;
85: }
86: my @duplicates;
87: foreach my $id (sort(keys(%check))) {
88: if ($check{$id} > 1) {
89: push(@duplicates,$id);
90: }
91: }
92: if (@duplicates) {
93: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
94: }
1.1 albertel 95: }
96:
1.14 albertel 97: sub start_input {
1.135 albertel 98: my ($parstack,$safeeval)=@_;
99: my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
100: if ($id eq '') { $id = $Apache::lonxml::curdepth; }
101: push (@Apache::inputtags::input,$id);
102: push (@Apache::inputtags::inputlist,$id);
103: return $id;
1.14 albertel 104: }
105:
106: sub end_input {
1.135 albertel 107: pop @Apache::inputtags::input;
108: return '';
1.14 albertel 109: }
110:
1.124 www 111: sub addchars {
112: my ($fieldid,$addchars)=@_;
113: my $output='';
114: foreach (split(/\,/,$addchars)) {
115: $output.='<a href="javascript:void(document.forms.lonhomework.'.
116: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
117: }
118: return $output;
119: }
120:
1.48 albertel 121: sub start_textfield {
1.135 albertel 122: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
123: my $result = "";
124: my $id = &start_input($parstack,$safeeval);
125: my $resid=$Apache::inputtags::response[-1];
126: if ($target eq 'web') {
127: $Apache::lonxml::evaluate--;
128: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
129: my $partid=$Apache::inputtags::part;
1.138 albertel 130: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135 albertel 131: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
132: if ( $cols eq '') { $cols = 80; }
133: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143 www 134: if ( $rows eq '') { $rows = 16; }
1.135 albertel 135: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
136: $result='';
137: if ($addchars) {
138: $result.=&addchars('HWVAL_'.$resid,$addchars);
139: }
1.143 www 140: push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
141: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135 albertel 142: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
143: if ($oldresponse ne '') {
1.143 www 144:
1.135 albertel 145: #get rid of any startup text if the user has already responded
146: &Apache::lonxml::get_all_text("/textfield",$parser);
147: }
148: } else {
149: #right or wrong don't show it
150: #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
151: $result='';
152: #get rid of any startup text
1.84 albertel 153: &Apache::lonxml::get_all_text("/textfield",$parser);
1.61 albertel 154: }
1.135 albertel 155: } elsif ($target eq 'grade') {
156: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
1.165 albertel 157: if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
1.135 albertel 158: # if the seed text is still there it wasn't a real submission
1.165 albertel 159: $env{'form.HWVAL_'.$resid}='';
1.135 albertel 160: }
161: } elsif ($target eq 'edit') {
162: $result.=&Apache::edit::tag_start($target,$token);
163: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
164: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
165: $result.=&Apache::edit::text_arg
166: ('Click-On Texts (comma sep):','addchars',$token,10);
167: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
168: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
169: } elsif ($target eq 'modified') {
170: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
171: $safeeval,'rows','cols',
172: 'addchars');
173: if ($constructtag) {
174: $result = &Apache::edit::rebuild_tag($token);
175: } else {
176: $result=$token->[4];
177: }
178: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
179: } elsif ($target eq 'tex') {
180: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
181: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
182: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
183: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
184: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
185: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
186: } else {
187: my $TeXwidth=$width_of_box/80;
188: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
189: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
190: $result.='}}}\vskip 2 mm ';
191: }
1.60 albertel 192: }
1.135 albertel 193: return $result;
1.6 albertel 194: }
195:
1.48 albertel 196: sub end_textfield {
1.135 albertel 197: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
198: my $result;
199: if ($target eq 'web') {
200: $Apache::lonxml::evaluate++;
201: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
202: return "</textarea>";
203: }
204: } elsif ($target eq 'edit') {
205: $result=&Apache::edit::end_table();
206: }
207: &end_input;
208: return $result;
1.6 albertel 209: }
210:
1.1 albertel 211: sub start_textline {
1.135 albertel 212: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
213: my $result = "";
214: if ($target eq 'web') {
215: $Apache::lonxml::evaluate--;
216: my $partid=$Apache::inputtags::part;
217: my $id=$Apache::inputtags::response[-1];
218: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
219: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
220: my $maxlength;
221: if ($size eq '') { $size=20; } else {
222: if ($size < 20) { $maxlength=$size; }
223: }
1.138 albertel 224: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.135 albertel 225: if ($Apache::lonhomework::type ne 'exam') {
226: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
227: $result='';
228: if ($addchars) {
229: $result.=&addchars('HWVAL_'.$id,$addchars);
230: }
1.157 albertel 231: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
232: $safeeval);
233: if (lc($readonly) eq 'yes') {
234: $readonly=' readonly="readonly" ';
1.158 albertel 235: } else {
236: $readonly='';
1.157 albertel 237: }
238: $result.= '<input type="text" '.$readonly.' name="HWVAL_'.$id.'" value="'.
1.135 albertel 239: $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
240: }
241: } else {
242: #right or wrong don't show what was last typed in.
1.168 albertel 243: $result='<b>'.$Apache::inputtags::answertxt{$id}.'</b>';
1.144 albertel 244: #$result='';
1.135 albertel 245: }
246: } elsif ($target eq 'edit') {
247: $result=&Apache::edit::tag_start($target,$token);
248: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157 albertel 249: &Apache::edit::text_arg('Click-On Texts (comma sep):',
250: 'addchars',$token,10);
251: $result.=&Apache::edit::select_arg('Readonly:','readonly',
252: ['no','yes'],$token);
253: $result.=&Apache::edit::end_row();
254: $result.=&Apache::edit::end_table();
1.135 albertel 255: } elsif ($target eq 'modified') {
1.157 albertel 256: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
257: $safeeval,'size',
258: 'addchars','readonly');
1.135 albertel 259: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
260: } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
261: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
262: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
263: $result='\framebox['.$size.'][s]{\tiny\strut}';
264: }
265: return $result;
1.1 albertel 266: }
267:
268: sub end_textline {
1.135 albertel 269: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
270: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
271: elsif ($target eq 'edit') { return ('','no'); }
272: return "";
1.9 albertel 273: }
274:
1.98 albertel 275: sub start_hiddenline {
276: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
277: my $result = "";
278: if ($target eq 'web') {
279: $Apache::lonxml::evaluate--;
280: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
281: my $partid=$Apache::inputtags::part;
282: my $id=$Apache::inputtags::response[-1];
1.138 albertel 283: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.98 albertel 284: if ($Apache::lonhomework::type ne 'exam') {
285: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
286: $oldresponse.'" />';
287: }
288: }
289: } elsif ($target eq 'edit') {
290: $result=&Apache::edit::tag_start($target,$token);
291: $result.=&Apache::edit::end_table;
292: }
293: return $result;
294: }
295:
296: sub end_hiddenline {
1.135 albertel 297: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
298: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
299: elsif ($target eq 'edit') { return ('','no'); }
300: return "";
1.98 albertel 301: }
302:
1.160 albertel 303: # $part -> partid
304: # $id -> responseid
305: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
306: # $which -> 'uploadedonly' -> only newly uploaded files
307: # 'portfolioonly' -> only allow files from portfolio
308: # 'both' -> allow files from either location
309: # returns a table row <tr>
310: sub file_selector {
311: my ($part,$id,$uploadedfiletypes,$which)=@_;
312: if (!$uploadedfiletypes) { return ''; }
1.167 albertel 313:
314: my $jspart=$part;
315: $jspart=~s/\./_/g;
316:
1.160 albertel 317: my $result;
318:
1.162 albertel 319: $result.='<tr><td>';
320: if ($uploadedfiletypes ne '*') {
321: $result.=
322: &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
323: }
1.160 albertel 324: if ($which eq 'uploadonly' || $which eq 'both') {
325: $result.=&mt('Submit a file: (only one file can be uploaded)').
326: ' <br /><input type="file" size="50" name="HWFILE'.
1.167 albertel 327: $jspart.'_'.$id.'" /><br />';
1.160 albertel 328: my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
329:
330: if ($uploadedfile) {
331: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
332: push (@Apache::lonxml::extlinks,$url);
333: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
334: my $icon=&Apache::loncommon::icon($url);
335: my $curfile='<a href="'.$url.'"><img src="'.$icon.
336: '" border="0" />'.$uploadedfile.'</a>';
337: $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
338: } else {
339: #$result.=&mt('(Hand in a file you have prepared on your computer)');
340: }
341: }
342: if ( $which eq 'both') {
343: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
344: }
345: if ($which eq 'portfolioonly' || $which eq 'both') {
1.167 albertel 346: $result.='<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname=HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
1.160 albertel 347: &mt('Select Portfolio Files').'</a><br />'.
1.167 albertel 348: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
1.160 albertel 349: '<br />';
350: if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
1.161 albertel 351: my @filelist;
1.160 albertel 352: foreach my $file (split(',',&Apache::lonnet::unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
353: my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
354: my $url="/uploaded/$domain/$user/portfolio$file";
355: my $icon=&Apache::loncommon::icon($url);
1.161 albertel 356: push(@filelist,'<a href="'.$url.'"><img src="'.$icon.
357: '" border="0" />'.$file.'</a>');
1.160 albertel 358: }
1.161 albertel 359: $result.=&mt("Portfolio files previously selected: <strong>[_1]</strong>",join(', ',@filelist));
1.160 albertel 360: }
361: }
362: $result.='</td></tr>';
363: return $result;
364: }
365:
1.136 albertel 366: sub checkstatus {
367: my ($value,$awardref,$msgref)=@_;
368: for (my $i=0;$i<=$#$awardref;$i++) {
369: if ($$awardref[$i] eq $value) {
370: return ($$awardref[$i],$$msgref[$i]);
371: }
372: }
373: return(undef,undef);
374: }
375:
1.9 albertel 376: sub finalizeawards {
1.136 albertel 377: my ($awardref,$msgref)=@_;
378: my $result=undef;
1.135 albertel 379: my $award;
1.136 albertel 380: my $msg;
381: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135 albertel 382: if ($result eq '' ) {
383: my $blankcount;
1.136 albertel 384: foreach $award (@$awardref) {
1.135 albertel 385: if ($award eq '') {
386: $result='MISSING_ANSWER';
387: $blankcount++;
388: }
389: }
1.136 albertel 390: if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
1.135 albertel 391: }
1.136 albertel 392: if (defined($result)) { return ($result,$msg); }
393: foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
1.137 albertel 394: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
395: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
396: 'UNIT_FAIL', 'NO_UNIT',
1.136 albertel 397: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
398: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
399: 'MISORDERED_RANK', 'INVALID_FILETYPE',
400: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
1.153 albertel 401: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
1.136 albertel 402: ($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
403: if (defined($result)) { return ($result,$msg); }
1.135 albertel 404: }
1.136 albertel 405: return ('ERROR',undef);
1.9 albertel 406: }
407:
1.10 albertel 408: sub decideoutput {
1.169 albertel 409: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
1.135 albertel 410: my $message='';
411: my $button=0;
412: my $previousmsg;
413: my $bgcolor='orange';
1.148 albertel 414: my $added_computer_text=0;
1.135 albertel 415: my %possiblecolors =
416: ( 'correct' => '#aaffaa',
417: 'charged_try' => '#ffaaaa',
418: 'not_charged_try' => '#ffffaa',
419: 'no_message' => '#fffff',
420: );
1.169 albertel 421:
1.135 albertel 422: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
423:
424: if ($solved =~ /^correct/) {
1.169 albertel 425: $bgcolor=$possiblecolors{'correct'};
1.170 albertel 426: $message=&mt('You are correct.');
427: if ($awarded < 1 && $awarded > 0) {
428: $message=&mt('You are partially correct.');
429: $bgcolor=$possiblecolors{'not_charged_try'};
430: } elsif ($awarded < 1) {
431: $message=&mt('Incorrect.');
432: $bgcolor=$possiblecolors{'charged_try'};
433: }
1.172 albertel 434: if ($env{'request.filename'} =~
435: m|/res/lib/templates/examupload.problem$|) {
436: $message = &mt("A score has been assigned.");
437: $added_computer_text=1;
1.135 albertel 438: } else {
1.172 albertel 439: if ($target eq 'tex') {
440: $message = '\textbf{'.$message.'}';
441: } else {
442: $message = "<b>".$message."</b>";
1.150 albertel 443: $message.=" ".&mt("Computer's answer now shown above.");
1.135 albertel 444: }
1.172 albertel 445: $added_computer_text=1;
1.165 albertel 446: unless ($env{'course.'.
447: $env{'request.course.id'}.
1.135 albertel 448: '.disable_receipt_display'} eq 'yes') {
449: $message.=(($target eq 'web')?'<br />':' ').
450: &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
451: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
452: }
453: }
454: $button=0;
455: $previousmsg='';
456: } elsif ($solved =~ /^excused/) {
457: if ($target eq 'tex') {
458: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
459: } else {
460: $message = "<b>".&mt('You are excused from the problem.')."</b>";
461: }
462: $bgcolor=$possiblecolors{'charged_try'};
463: $button=0;
464: $previousmsg='';
465: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
466: if ($solved =~ /^incorrect/ || $solved eq '') {
1.144 albertel 467: $message = &mt("Incorrect").".";
1.135 albertel 468: $bgcolor=$possiblecolors{'charged_try'};
469: $button=1;
470: } else {
1.144 albertel 471: if ($target eq 'tex') {
472: $message = '\textbf{'.&mt('You are correct.').'}';
473: } else {
474: $message = "<b>".&mt('You are correct.')."</b>";
1.150 albertel 475: $message.=" ".&mt("Computer's answer now shown above.");
1.144 albertel 476: }
1.148 albertel 477: $added_computer_text=1;
1.165 albertel 478: unless ($env{'course.'.
479: $env{'request.course.id'}.
1.135 albertel 480: '.disable_receipt_display'} eq 'yes') {
481: $message.=(($target eq 'web')?'<br />':' ').
482: 'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
483: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
484: }
485: $bgcolor=$possiblecolors{'correct'};
486: $button=0;
487: $previousmsg='';
488: }
489: } elsif ($award eq 'NO_RESPONSE') {
490: $message = '';
491: $bgcolor=$possiblecolors{'no_feedback'};
492: $button=1;
493: } elsif ($award eq 'MISSING_ANSWER') {
494: $message = &mt('Some items were not submitted.');
495: $bgcolor=$possiblecolors{'not_charged_try'};
496: $button = 1;
497: } elsif ($award eq 'ERROR') {
498: $message = &mt('An error occured while grading your answer.');
499: $bgcolor=$possiblecolors{'not_charged_try'};
500: $button = 1;
501: } elsif ($award eq 'TOO_LONG') {
502: $message = &mt("The submitted answer was too long.");
503: $bgcolor=$possiblecolors{'not_charged_try'};
504: $button=1;
505: } elsif ($award eq 'WANTED_NUMERIC') {
506: $message = &mt("This question expects a numeric answer.");
507: $bgcolor=$possiblecolors{'not_charged_try'};
508: $button=1;
509: } elsif ($award eq 'MISORDERED_RANK') {
510: $message = &mt('You have provided an invalid ranking');
511: if ($target ne 'tex') {
1.159 albertel 512: $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
1.135 albertel 513: }
514: $bgcolor=$possiblecolors{'not_charged_try'};
515: $button=1;
516: } elsif ($award eq 'INVALID_FILETYPE') {
1.166 albertel 517: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
1.135 albertel 518: $bgcolor=$possiblecolors{'not_charged_try'};
519: $button=1;
520: } elsif ($award eq 'SIG_FAIL') {
1.145 albertel 521: my ($used,$min,$max)=split(':',$awardmsg);
522: my $word;
523: if ($used < $min) { $word=&mt('more'); }
524: if ($used > $max) { $word=&mt('fewer'); }
525: $message = &mt("Submission not graded. Use [_2] digits.",$used,$word);
1.135 albertel 526: $bgcolor=$possiblecolors{'not_charged_try'};
527: $button=1;
1.137 albertel 528: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
529: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
530: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
531: $bgcolor=$possiblecolors{'not_charged_try'};
532: $button=1;
533: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155 albertel 534: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137 albertel 535: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
536: $bgcolor=$possiblecolors{'not_charged_try'};
537: $button=1;
1.140 matthew 538: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155 albertel 539: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136 albertel 540: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1.135 albertel 541: $bgcolor=$possiblecolors{'not_charged_try'};
542: $button=1;
543: } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155 albertel 544: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.135 albertel 545: $bgcolor=$possiblecolors{'not_charged_try'};
546: $button=1;
547: } elsif ($award eq 'NO_UNIT') {
1.144 albertel 548: $message = &mt("Units required").'.';
1.135 albertel 549: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
550: $bgcolor=$possiblecolors{'not_charged_try'};
551: $button=1;
1.153 albertel 552: } elsif ($award eq 'COMMA_FAIL') {
553: $message = &mt("Proper comma separation is required").'.';
554: $bgcolor=$possiblecolors{'not_charged_try'};
555: $button=1;
1.135 albertel 556: } elsif ($award eq 'BAD_FORMULA') {
557: $message = &mt("Unable to understand formula");
558: $bgcolor=$possiblecolors{'not_charged_try'};
559: $button=1;
560: } elsif ($award eq 'INCORRECT') {
1.144 albertel 561: $message = &mt("Incorrect").'.';
1.135 albertel 562: $bgcolor=$possiblecolors{'charged_try'};
563: $button=1;
564: } elsif ($award eq 'SUBMITTED') {
565: $message = &mt("Your submission has been recorded.");
566: $bgcolor=$possiblecolors{'correct'};
567: $button=1;
568: } elsif ($award eq 'DRAFT') {
1.144 albertel 569: $message = &mt("A draft copy has been saved.");
1.135 albertel 570: $bgcolor=$possiblecolors{'not_charged_try'};
571: $button=1;
572: } elsif ($award eq 'ASSIGNED_SCORE') {
1.144 albertel 573: $message = &mt("A score has been assigned.");
1.135 albertel 574: $bgcolor=$possiblecolors{'correct'};
575: $button=0;
1.144 albertel 576: } elsif ($award eq '') {
577: $bgcolor=$possiblecolors{'not_charged_try'};
578: $button=1;
1.135 albertel 579: } else {
580: $message = &mt("Unknown message").": $award";
581: $button=1;
582: }
583: if (lc($Apache::lonhomework::problemstatus) eq 'no' &&
584: $Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
585: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
586: $bgcolor=$possiblecolors{'correct'};
587: $button=1;
588: }
1.148 albertel 589: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1.150 albertel 590: !$added_computer_text && $target ne 'tex') {
1.149 albertel 591: $message.=" ".&mt("Computer's answer now shown above.");
1.148 albertel 592: $added_computer_text=1;
1.144 albertel 593: }
1.135 albertel 594: return ($button,$bgcolor,$message,$previousmsg);
1.12 albertel 595: }
596:
1.155 albertel 597: sub markup_unit {
598: my ($unit,$target)=@_;
599: if ($target eq 'tex') {
600: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
601: } else {
602: return "<tt>".$unit."</tt>";
603: }
604: }
605:
1.88 albertel 606: sub removealldata {
1.87 albertel 607: my ($id)=@_;
608: foreach my $key (keys(%Apache::lonhomework::results)) {
609: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
610: &Apache::lonxml::debug("Removing $key");
611: delete($Apache::lonhomework::results{$key});
612: }
613: }
614: }
615:
1.142 albertel 616: sub hidealldata {
617: my ($id)=@_;
618: foreach my $key (keys(%Apache::lonhomework::results)) {
619: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
620: &Apache::lonxml::debug("Hidding $key");
621: my $newkey=$key;
622: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
623: $Apache::lonhomework::results{$newkey}=
624: $Apache::lonhomework::results{$key};
625: delete($Apache::lonhomework::results{$key});
626: }
627: }
628: }
629:
1.12 albertel 630: sub setgradedata {
1.136 albertel 631: my ($award,$msg,$id,$previously_used) = @_;
1.154 albertel 632: if ($Apache::lonhomework::scantronmode &&
1.165 albertel 633: &Apache::lonnet::validCODE($env{'form.CODE'})) {
634: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154 albertel 635: } elsif ($Apache::lonhomework::scantronmode &&
1.165 albertel 636: $env{'form.CODE'} eq '' &&
1.154 albertel 637: $Apache::lonhomework::history{"resource.CODE"} ne '') {
638: $Apache::lonhomework::results{"resource.CODE"}='';
1.141 albertel 639: }
1.154 albertel 640:
1.135 albertel 641: if (!$Apache::lonhomework::scantronmode &&
642: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
643: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
644: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87 albertel 645: return '';
1.135 albertel 646: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
647: /^correct/ || $Apache::lonhomework::scantronmode ||
648: lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154 albertel 649: # the student doesn't already have it correct,
650: # or we are in a mode (scantron orno problem status) where a correct
651: # can become incorrect
652: # handle assignment of tries and solved status
1.135 albertel 653: my $solvemsg;
654: if ($Apache::lonhomework::scantronmode) {
655: $solvemsg='correct_by_scantron';
656: } else {
657: $solvemsg='correct_by_student';
658: }
659: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
660: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
661: }
662: if ( $award eq 'ASSIGNED_SCORE') {
663: $Apache::lonhomework::results{"resource.$id.tries"} =
664: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
665: $Apache::lonhomework::results{"resource.$id.solved"} =
666: $solvemsg;
667: my $numawards=scalar(@Apache::inputtags::response);
668: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
669: foreach my $res (@Apache::inputtags::response) {
670: $Apache::lonhomework::results{"resource.$id.awarded"}+=
671: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
672: }
673: if ($numawards > 0) {
674: $Apache::lonhomework::results{"resource.$id.awarded"}/=
675: $numawards;
676: }
677: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
678: $Apache::lonhomework::results{"resource.$id.tries"} =
679: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
680: $Apache::lonhomework::results{"resource.$id.solved"} =
681: $solvemsg;
682: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
683: } elsif ( $award eq 'INCORRECT' ) {
684: $Apache::lonhomework::results{"resource.$id.tries"} =
685: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.152 albertel 686: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
687: $Apache::lonhomework::scantronmode) {
1.135 albertel 688: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
689: }
690: $Apache::lonhomework::results{"resource.$id.solved"} =
691: 'incorrect_attempted';
692: } elsif ( $award eq 'SUBMITTED' ) {
693: $Apache::lonhomework::results{"resource.$id.tries"} =
694: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
695: $Apache::lonhomework::results{"resource.$id.solved"} =
696: 'ungraded_attempted';
697: } elsif ( $award eq 'DRAFT' ) {
698: $Apache::lonhomework::results{"resource.$id.solved"} = '';
699: } elsif ( $award eq 'NO_RESPONSE' ) {
700: #no real response so delete any data that got stored
1.129 albertel 701: &removealldata($id);
702: return '';
703: } else {
1.135 albertel 704: $Apache::lonhomework::results{"resource.$id.solved"} =
705: 'incorrect_attempted';
1.152 albertel 706: if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
707: $Apache::lonhomework::scantronmode) {
1.135 albertel 708: $Apache::lonhomework::results{"resource.$id.tries"} =
709: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
710: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
711: }
712: }
1.136 albertel 713: if (defined($msg)) {
714: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
715: }
1.135 albertel 716: # did either of the overall awards chage? If so ignore the
717: # previous check
718: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
719: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
720: ($Apache::lonhomework::results{"resource.$id.solved"} eq
721: $Apache::lonhomework::history{"resource.$id.solved"})) {
722: # check if this was a previous submission if it was delete the
723: # unneeded data and update the previously_used attribute
724: if ( $previously_used eq 'PREVIOUSLY_USED') {
725: if (lc($Apache::lonhomework::problemstatus) ne 'no') {
726: delete($Apache::lonhomework::results{"resource.$id.tries"});
727: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
728: }
729: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
730: #delete all data as they student didn't do anything, but save
731: #the list of collaborators.
732: &removealldata($id);
733: #and since they didn't do anything we were never here
734: return '';
735: } else {
736: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
737: }
1.101 albertel 738: }
1.135 albertel 739: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
740: /^correct/ ) {
741: #delete all data as they student already has it correct
742: &removealldata($id);
743: #and since they didn't do anything we were never here
744: return '';
1.40 albertel 745: }
1.135 albertel 746: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10 albertel 747: }
748:
1.9 albertel 749: sub grade {
1.135 albertel 750: my ($target) = @_;
751: my $id = $Apache::inputtags::part;
752: my $response='';
1.165 albertel 753: if ( defined $env{'form.submitted'}) {
1.136 albertel 754: my (@awards,@msgs);
1.135 albertel 755: foreach $response (@Apache::inputtags::response) {
756: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
757: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
758: &Apache::lonxml::debug("keeping $value from $response for $id");
759: push (@awards,$value);
1.136 albertel 760: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
761: &Apache::lonxml::debug("got message $value from $response for $id");
762: push (@msgs,$value);
1.135 albertel 763: }
1.136 albertel 764: my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135 albertel 765: my $previously_used;
766: if ( $#Apache::inputtags::previous eq $#awards ) {
767: my $match=0;
768: my @matches;
769: foreach my $versionar (@Apache::inputtags::previous_version) {
770: foreach my $version (@$versionar) {
771: $matches[$version]++;
772: }
773: }
774: foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
775: if ($match) {
776: $previously_used = 'PREVIOUSLY_LAST';
777: foreach my $value (@Apache::inputtags::previous) {
778: if ($value eq 'PREVIOUSLY_USED' ) {
779: $previously_used = $value;
780: last;
781: }
1.75 albertel 782: }
783: }
1.43 albertel 784: }
1.136 albertel 785: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
786: &setgradedata($finalaward,$msg,$id,$previously_used);
1.43 albertel 787: }
1.135 albertel 788: return '';
1.1 albertel 789: }
790:
1.11 albertel 791: sub gradestatus {
1.135 albertel 792: my ($id,$target) = @_;
793: my $showbutton = 1;
794: my $bgcolor = '';
795: my $message = '';
796: my $latemessage = '';
797: my $trystr='';
798: my $button='';
799: my $previousmsg='';
800:
801: my $status = $Apache::inputtags::status['-1'];
802: &Apache::lonxml::debug("gradestatus has :$status:");
803: if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
804: $status ne 'INVALID_ACCESS') {
805: my $award = $Apache::lonhomework::history{"resource.$id.award"};
1.169 albertel 806: my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
1.135 albertel 807: my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
808: my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136 albertel 809: my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
810: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144 albertel 811: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135 albertel 812: &Apache::lonxml::debug('Getting message');
813: ($showbutton,$bgcolor,$message,$previousmsg) =
1.169 albertel 814: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
815: $target);
1.135 albertel 816: if ($target eq 'tex') {
817: $message='\vskip 2 mm '.$message.' ';
818: } else {
819: $message="<td bgcolor=\"$bgcolor\">$message</td>";
820: if ($previousmsg) {
821: $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
822: }
823: }
824: }
825: my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
826: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
827: &Apache::lonxml::debug("got maxtries of :$maxtries:");
828: #if tries are set to negative turn off the Tries/Button and messages
829: if (defined($maxtries) && $maxtries < 0) { return ''; }
830: if ( $tries eq '' ) { $tries = '0'; }
831: if ( $maxtries eq '' ) { $maxtries = '2'; }
832: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
833: my $tries_text=&mt('Tries');
1.164 albertel 834: if ( $Apache::lonhomework::type eq 'survey' ||
835: $Apache::lonhomework::parsing_a_task) {
836: $tries_text=&mt('Submissions');
837: }
1.135 albertel 838: if ( $showbutton ) {
839: if ($target eq 'tex') {
1.165 albertel 840: if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
1.135 albertel 841: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
842: } else {
843: $trystr = '\vskip 0 mm ';
844: }
845: } else {
1.136 albertel 846: $trystr = "<td><nobr>".$tries_text." $tries";
1.164 albertel 847: if ($Apache::lonhomework::parsing_a_task) {
1.165 albertel 848: } elsif($env{'request.state'} ne 'construct') {
1.135 albertel 849: $trystr.="/$maxtries";
850: } else {
851: if (defined($Apache::inputtags::params{'maxtries'})) {
852: $trystr.="/".$Apache::inputtags::params{'maxtries'};
853: }
854: }
1.136 albertel 855: $trystr.="</nobr></td>";
1.135 albertel 856: }
857: }
858: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
859: if ( $showbutton ) {
860: if ($target ne 'tex') {
1.156 albertel 861: $button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135 albertel 862: }
863: }
864: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
865: #last submissions was after due date
1.163 albertel 866: $latemessage=&mt(' The last submission was after the Due Date ');;
867: if ($target eq 'web') {
868: $latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1.135 albertel 869: }
870: }
871: }
872: my $output= $previousmsg.$latemessage.$message.$trystr;
873: if ($output =~ /^\s*$/) {
874: return $button;
1.63 sakharuk 875: } else {
1.135 albertel 876: if ($target eq 'tex') {
877: return $button.' \vskip 0 mm '.$output.' ';
878: } else {
879: return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
880: }
1.63 sakharuk 881: }
1.11 albertel 882: }
1.1 albertel 883: 1;
884: __END__
1.43 albertel 885:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>