File:
[LON-CAPA] /
loncom /
homework /
inputtags.pm
Revision
1.248:
download - view:
text,
annotated -
select for diffs
Sun Nov 16 02:46:25 2008 UTC (15 years, 7 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_8_X,
HEAD,
GCI_1
Bug 5719 Size limits for File Upload in Essay response.
- Max. cumulative size for submitted files applies to total for all files submitted to a particular essayresponse item. Default is 100 MB.
- In course context, &lonacc:get_posted_cgi() will now no longer add file contents to %env if size exceeds max. for item.
- Additional checks in essayresponse::file_submission() will assign award as "EXCESS_FILESIZE" if total of all files being submitted for sinle essayresponse item exceed max set via
PARM.
- Modification to $Apache::lonhomework::results{"resource.$part.$id.$which"} to now include a comma separated list of only those submitted files which passed both the filetype and size tests.
1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.248 2008/11/16 02:46:25 raeburn Exp $
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:
28: package Apache::inputtags;
29: use HTML::Entities();
30: use strict;
31: use Apache::loncommon;
32: use Apache::lonlocal;
33: use Apache::lonnet;
34: use LONCAPA;
35:
36:
37: BEGIN {
38: &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
39: }
40:
41: # Initializes a set of global variables used during the parse of the problem.
42: #
43: # @Apache::inputtags::input - List of current input ids.
44: # @Apache::inputtags::inputlist - List of all input ids seen this problem.
45: # @Apache::inputtags::response - List of all current resopnse ids.
46: # @Apache::inputtags::responselist - List of all response ids seen this
47: # problem.
48: # @Apache::inputtags::hint - List of all hint ids.
49: # @Apache::inputtags::hintlist - List of all hint ids seen this problem.
50: # @Apache::inputtags::previous - List describing if specific responseds
51: # have been used
52: # @Apache::inputtags::previous_version - Submission responses were used in.
53: # $Apache::inputtags::part - Current part id (valid only in
54: # <problem>)
55: # 0 if not in a part.
56: # @Apache::inputtags::partlist - List of part ids seen in the current
57: # <problem>
58: # @Apache::inputtags::status - List of problem statuses. First
59: # element is the status of the <problem>
60: # the remainder are for individual <part>s.
61: # %Apache::inputtags::params - Hash of defined parameters for the
62: # current response.
63: # @Apache::inputtags::import - List of all ids for <import> thes get
64: # join()ed and prepended.
65: # @Apache::inputtags::importlist - List of all import ids seen.
66: # $Apache::inputtags::response_with_no_part
67: # - Flag set true if we have seen a response
68: # that is not inside a <part>
69: # %Apache::inputtags::answertxt - <*response> tags store correct
70: # answer strings for display by <textline/>
71: # in this hash.
72: # %Apache::inputtags::submission_display
73: # - <*response> tags store improved display
74: # of submission strings for display by part
75: # end.
76:
77: sub initialize_inputtags {
78: @Apache::inputtags::input=();
79: @Apache::inputtags::inputlist=();
80: @Apache::inputtags::response=();
81: @Apache::inputtags::responselist=();
82: @Apache::inputtags::hint=();
83: @Apache::inputtags::hintlist=();
84: @Apache::inputtags::previous=();
85: @Apache::inputtags::previous_version=();
86: $Apache::inputtags::part='';
87: @Apache::inputtags::partlist=();
88: @Apache::inputtags::status=();
89: %Apache::inputtags::params=();
90: @Apache::inputtags::import=();
91: @Apache::inputtags::importlist=();
92: $Apache::inputtags::response_with_no_part=0;
93: %Apache::inputtags::answertxt=();
94: %Apache::inputtags::submission_display=();
95: }
96:
97: sub check_for_duplicate_ids {
98: my %check;
99: foreach my $id (@Apache::inputtags::partlist,
100: @Apache::inputtags::responselist,
101: @Apache::inputtags::hintlist,
102: @Apache::inputtags::importlist) {
103: $check{$id}++;
104: }
105: my @duplicates;
106: foreach my $id (sort(keys(%check))) {
107: if ($check{$id} > 1) {
108: push(@duplicates,$id);
109: }
110: }
111: if (@duplicates) {
112: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
113: }
114: }
115:
116: sub start_input {
117: my ($parstack,$safeeval)=@_;
118: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
119: push (@Apache::inputtags::input,$id);
120: push (@Apache::inputtags::inputlist,$id);
121: return $id;
122: }
123:
124: sub end_input {
125: pop @Apache::inputtags::input;
126: return '';
127: }
128:
129: sub addchars {
130: my ($fieldid,$addchars)=@_;
131: my $output='';
132: foreach (split(/\,/,$addchars)) {
133: $output.='<a href="javascript:void(document.forms.lonhomework.'.
134: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
135: }
136: return $output;
137: }
138:
139: sub start_textfield {
140: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
141: my $result = "";
142: my $id = &start_input($parstack,$safeeval);
143: my $resid=$Apache::inputtags::response[-1];
144: if ($target eq 'web') {
145: $Apache::lonxml::evaluate--;
146: my $partid=$Apache::inputtags::part;
147: my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
148: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
149: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
150: if ( $cols eq '') { $cols = 80; }
151: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
152: if ( $rows eq '') { $rows = 16; }
153: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
154: $result='';
155: if ($addchars) {
156: $result.=&addchars('HWVAL_'.$resid,$addchars);
157: }
158: &Apache::lonhtmlcommon::add_htmlareafields('HWVAL_'.$resid);
159: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
160: "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
161: if ($oldresponse ne '') {
162:
163: #get rid of any startup text if the user has already responded
164: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
165: }
166: } else {
167: #show past answer in the essayresponse case
168: if ($oldresponse =~ /\S/
169: && &Apache::londefdef::is_inside_of($tagstack,
170: 'essayresponse') ) {
171: $result='<table class="LC_pastsubmission"><tr><td>'.
172: $oldresponse.'</td></tr></table>';
173: }
174: #get rid of any startup text
175: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
176: }
177: } elsif ($target eq 'grade') {
178: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
179: $style);
180: if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
181: # if the seed text is still there it wasn't a real submission
182: $env{'form.HWVAL_'.$resid}='';
183: }
184: } elsif ($target eq 'edit') {
185: $result.=&Apache::edit::tag_start($target,$token);
186: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
187: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
188: $result.=&Apache::edit::text_arg
189: ('Click-On Texts (comma sep):','addchars',$token,10);
190: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
191: $style);
192: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
193: } elsif ($target eq 'modified') {
194: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
195: $safeeval,'rows','cols',
196: 'addchars');
197: if ($constructtag) {
198: $result = &Apache::edit::rebuild_tag($token);
199: } else {
200: $result=$token->[4];
201: }
202: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
203: } elsif ($target eq 'tex') {
204: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
205: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
206: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
207: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
208: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
209: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
210: } else {
211: my $TeXwidth=$width_of_box/80;
212: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
213: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
214: $result.='}}}\vskip 2 mm ';
215: }
216: }
217: return $result;
218: }
219:
220: sub end_textfield {
221: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
222: my $result;
223: if ($target eq 'web') {
224: $Apache::lonxml::evaluate++;
225: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
226: return "</textarea>";
227: }
228: } elsif ($target eq 'edit') {
229: $result=&Apache::edit::end_table();
230: }
231: &end_input;
232: return $result;
233: }
234:
235: sub exam_score_line {
236: my ($target) = @_;
237:
238: my $result;
239: if ($target eq 'tex') {
240: my $repetition = &Apache::response::repetition();
241: $result.='\begin{enumerate}';
242: if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
243: foreach my $i (0..$repetition-1) {
244: $result.='\item[\textbf{'.
245: ($Apache::lonxml::counter+$i).
246: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
247: }
248: $result.= '\end{enumerate}';
249: }
250:
251: return $result;
252: }
253:
254: sub exam_box {
255: my ($target) = @_;
256: my $result;
257:
258: if ($target eq 'tex') {
259: $result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
260: $result .= &exam_score_line($target);
261: } elsif ($target eq 'web') {
262: my $id=$Apache::inputtags::response[-1];
263: $result.= '<br /><br />
264: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
265: </textarea> <br /><br />';
266: }
267: return $result;
268: }
269:
270: sub needs_exam_box {
271: my ($tagstack) = @_;
272: my @tags = ('formularesponse',
273: 'stringresponse',
274: 'reactionresponse',
275: 'organicresponse',
276: );
277:
278: foreach my $tag (@tags) {
279: if (grep(/\Q$tag\E/,@$tagstack)) {
280: return 1;
281: }
282: }
283: return 0;
284: }
285:
286: sub start_textline {
287: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
288: my $result = "";
289: my $input_id = &start_input($parstack,$safeeval);
290: if ($target eq 'web') {
291: $Apache::lonxml::evaluate--;
292: my $partid=$Apache::inputtags::part;
293: my $id=$Apache::inputtags::response[-1];
294: if (!&Apache::response::show_answer()) {
295: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
296: my $maxlength;
297: if ($size eq '') { $size=20; } else {
298: if ($size < 20) {
299: $maxlength = ' maxlength="'.$size.'"';
300: }
301: }
302: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
303: &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
304:
305: if (ref($oldresponse) eq 'ARRAY') {
306: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
307: }
308: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
309: $oldresponse =~ s/^\s+//;
310: $oldresponse =~ s/\s+$//;
311: $oldresponse =~ s/\s+/ /g;
312: if ($Apache::lonhomework::type ne 'exam') {
313: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
314: $result='';
315: if ($addchars) {
316: $result.=&addchars('HWVAL_'.$id,$addchars);
317: }
318: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
319: $safeeval);
320: if (lc($readonly) eq 'yes'
321: || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
322: $readonly=' readonly="readonly" ';
323: } else {
324: $readonly='';
325: }
326: my $name = 'HWVAL_'.$id;
327: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
328: $name = "none";
329: }
330: $result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');" type="text" '.$readonly.' name="'.$name.'" value="'.
331: $oldresponse.'" size="'.$size.'"'.$maxlength.' />';
332: }
333: if ($Apache::lonhomework::type eq 'exam'
334: && &needs_exam_box($tagstack)) {
335: $result.=&exam_box($target);
336: }
337: } else {
338: #right or wrong don't show what was last typed in.
339: my $count = scalar(@Apache::inputtags::inputlist)-1;
340: $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
341: #$result='';
342: }
343: } elsif ($target eq 'edit') {
344: $result=&Apache::edit::tag_start($target,$token);
345: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
346: &Apache::edit::text_arg('Click-On Texts (comma sep):',
347: 'addchars',$token,10);
348: $result.=&Apache::edit::select_arg('Readonly:','readonly',
349: ['no','yes'],$token);
350: $result.=&Apache::edit::end_row();
351: $result.=&Apache::edit::end_table();
352: } elsif ($target eq 'modified') {
353: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
354: $safeeval,'size',
355: 'addchars','readonly');
356: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
357: } elsif ($target eq 'tex'
358: && $Apache::lonhomework::type ne 'exam') {
359: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
360: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
361: if ($env{'form.pdfFormFields'} eq 'yes') {
362: my $fieldname = $env{'request.symb'}.
363: '&part_'. $Apache::inputtags::part.
364: '&textresponse'.
365: '&HWVAL_' . $Apache::inputtags::response['-1'];
366: $result="\n\\\\\n".'\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
367: } else {
368: $result='\framebox['.$size.'][s]{\tiny\strut}';
369: }
370: } elsif ($target eq 'tex'
371: && $Apache::lonhomework::type eq 'exam'
372: && &needs_exam_box($tagstack)) {
373: $result.=&exam_box($target);
374: }
375: return $result;
376: }
377:
378: sub end_textline {
379: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
380: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
381: elsif ($target eq 'edit') { return ('','no'); }
382: &end_input();
383: return "";
384: }
385:
386: sub start_hiddenline {
387: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
388: my $result = "";
389: my $input_id = &start_input($parstack,$safeeval);
390: if ($target eq 'web') {
391: $Apache::lonxml::evaluate--;
392: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
393: my $partid=$Apache::inputtags::part;
394: my $id=$Apache::inputtags::response[-1];
395: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
396: if (ref($oldresponse) eq 'ARRAY') {
397: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
398: }
399: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
400:
401: if ($Apache::lonhomework::type ne 'exam') {
402: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
403: $oldresponse.'" />';
404: }
405: }
406: } elsif ($target eq 'edit') {
407: $result=&Apache::edit::tag_start($target,$token);
408: $result.=&Apache::edit::end_table;
409: }
410:
411: if ( ($target eq 'web' || $target eq 'tex')
412: && $Apache::lonhomework::type eq 'exam'
413: && &needs_exam_box($tagstack)) {
414: $result.=&exam_box($target);
415: }
416: return $result;
417: }
418:
419: sub end_hiddenline {
420: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
421: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
422: elsif ($target eq 'edit') { return ('','no'); }
423: &end_input();
424: return "";
425: }
426:
427: # $part -> partid
428: # $id -> responseid
429: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
430: # $which -> 'uploadedonly' -> only newly uploaded files
431: # 'portfolioonly' -> only allow files from portfolio
432: # 'both' -> allow files from either location
433: # $extratext -> additional text to go between the link and the input box
434: # returns a table row <tr>
435: sub file_selector {
436: my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
437: if (!$uploadedfiletypes) { return ''; }
438:
439: my $jspart=$part;
440: $jspart=~s/\./_/g;
441:
442: my $result;
443:
444: $result.='<tr><td>';
445: if ($uploadedfiletypes ne '*') {
446: $result.=
447: &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
448: }
449: if ($which eq 'uploadonly' || $which eq 'both') {
450: $result.=&mt('Submit a file: (only one file can be uploaded)').
451: ' <br /><input type="file" size="50" name="HWFILE'.
452: $jspart.'_'.$id.'" /><br />';
453: $result .= &show_past_file_submission($part,$id);
454: }
455: if ( $which eq 'both') {
456: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
457: }
458: if ($which eq 'portfolioonly' || $which eq 'both') {
459: $result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
460: &mt('Select Portfolio Files').'</a><br />'.
461: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
462: '<br />';
463: $result .= &show_past_portfile_submission($part,$id);
464:
465: }
466: $result.='</td></tr>';
467: return $result;
468: }
469:
470: sub show_past_file_submission {
471: my ($part,$id) = @_;
472: my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
473:
474: return if (!$uploadedfile);
475:
476: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
477: &Apache::lonxml::extlink($url);
478: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
479: my $icon=&Apache::loncommon::icon($url);
480: my $curfile='<a href="'.$url.'"><img src="'.$icon.
481: '" border="0" />'.$uploadedfile.'</a>';
482: return &mt('Currently submitted: <tt>[_1]</tt>',$curfile);
483:
484: }
485:
486: sub show_past_portfile_submission {
487: my ($part,$id) = @_;
488: if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}!~/[^\s]/){
489: return;
490: }
491: my (@file_list,@bad_file_list);
492: foreach my $file (split(/\s*,\s*/,&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
493: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
494: my $url="/uploaded/$domain/$user/portfolio$file";
495: my $icon=&Apache::loncommon::icon($url);
496: push(@file_list,'<a href="'.$url.'"><img src="'.$icon.
497: '" border="0" />'.$file.'</a>');
498: if (! &Apache::lonnet::stat_file($url)) {
499: &Apache::lonnet::logthis("bad file is $url");
500: push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
501: '" border="0" />'.$file.'</a>');
502: }
503: }
504: my $files = '<span class="LC_filename">'.
505: join('</span>, <span class="LC_filename">',@file_list).
506: '</span>';
507: my $result = &mt("Portfolio files previously selected: [_1]",$files);
508: if (@bad_file_list) {
509: my $bad_files = '<span class="LC_filename">'.
510: join('</span>, <span class="LC_filename">',@bad_file_list).
511: '</span>';
512: $result.='<br />'.&mt('<span class="LC_error">These file(s) don\'t exist:</span> [_1]',$bad_files);
513: }
514: return $result;
515:
516: }
517:
518: sub valid_award {
519: my ($award) =@_;
520: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
521: 'NO_RESPONSE',
522: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
523: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
524: 'UNIT_FAIL', 'NO_UNIT',
525: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
526: 'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT',
527: 'MISORDERED_RANK', 'INVALID_FILETYPE',
528: 'EXCESS_FILESIZE', 'DRAFT',
529: 'SUBMITTED', 'ASSIGNED_SCORE',
530: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
531: if ($award eq $possibleaward) { return 1; }
532: }
533: return 0;
534: }
535:
536: {
537: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
538: 'TOO_LONG',
539: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
540: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
541: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
542: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
543: 'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'DRAFT', 'SUBMITTED',
544: 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
545: my $i=0;
546: my %fwd_awards = map { ($_,$i++) } @awards;
547: my $max=scalar(@awards);
548: @awards=reverse(@awards);
549: $i=0;
550: my %rev_awards = map { ($_,$i++) } @awards;
551:
552: sub awarddetail_to_awarded {
553: my ($awarddetail) = @_;
554: if ($awarddetail eq 'EXACT_ANS'
555: || $awarddetail eq 'APPROX_ANS') {
556: return 1;
557: }
558: return 0;
559: }
560:
561: sub hide_award {
562: my ($award) = @_;
563: if (&Apache::lonhomework::show_no_problem_status()) {
564: return 1;
565: }
566: if ($award =~
567: /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) {
568: return 1;
569: }
570: return 0;
571: }
572:
573: sub finalizeawards {
574: my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
575: my $result;
576: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
577: if ($result eq '' ) {
578: my $blankcount;
579: foreach my $award (@$awardref) {
580: if ($award eq '') {
581: $result='MISSING_ANSWER';
582: $blankcount++;
583: }
584: }
585: if ($blankcount == ($#$awardref + 1)) {
586: return ('NO_RESPONSE');
587: }
588: }
589: if (!$final_scantron && defined($result)) { return ($result); }
590:
591: # if in scantron mode, if the award for any response is
592: # assigned score, then the part gets an assigned score
593: if ($final_scantron
594: && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
595: return ('ASSIGNED_SCORE');
596: }
597:
598: # if in scantron mode, if the award for any response is
599: # correct and there are non-correct responses,
600: # then the part gets an assigned score
601: if ($final_scantron
602: && (grep { $_ eq 'EXACT_ANS' ||
603: $_ eq 'APPROX_ANS' } (@$awardref))
604: && (grep { $_ ne 'EXACT_ANS' &&
605: $_ ne 'APPROX_ANS' } (@$awardref))) {
606: return ('ASSIGNED_SCORE');
607: }
608: # these awards are ordered from most important error through best correct
609: my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
610:
611: my $best = $max;
612: my $j=0;
613: my $which;
614: foreach my $award (@$awardref) {
615: if ($awards->{$award} < $best) {
616: $best = $awards->{$award};
617: $which = $j;
618: }
619: $j++;
620: }
621:
622: if (defined($which)) {
623: if (ref($nameref)) {
624: return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
625: } else {
626: return ($$awardref[$which],$$msgref[$which]);
627: }
628: }
629: return ('ERROR',undef);
630: }
631: }
632:
633: sub decideoutput {
634: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
635: my $message='';
636: my $button=0;
637: my $previousmsg;
638: my $css_class='orange';
639: my $added_computer_text=0;
640: my %possible_class =
641: ( 'correct' => 'LC_answer_correct',
642: 'charged_try' => 'LC_answer_charged_try',
643: 'not_charged_try' => 'LC_answer_not_charged_try',
644: 'no_grade' => 'LC_answer_no_grade',
645: 'no_message' => 'LC_no_message',
646: );
647:
648: my $part = $Apache::inputtags::part;
649: my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
650: my $handgrade = ('yes' eq lc($tohandgrade));
651:
652: my $computer = ($handgrade)? ''
653: : " ".&mt("Computer's answer now shown above.");
654: &Apache::lonxml::debug("handgrade has :$handgrade:");
655:
656: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
657:
658: if ($solved =~ /^correct/) {
659: $css_class=$possible_class{'correct'};
660: $message=&mt('You are correct.');
661: if ($awarded < 1 && $awarded > 0) {
662: $message=&mt('You are partially correct.');
663: $css_class=$possible_class{'not_charged_try'};
664: } elsif ($awarded < 1) {
665: $message=&mt('Incorrect.');
666: $css_class=$possible_class{'charged_try'};
667: }
668: if ($env{'request.filename'} =~
669: m|/res/lib/templates/examupload.problem$|) {
670: $message = &mt("A score has been assigned.");
671: $added_computer_text=1;
672: } else {
673: if ($target eq 'tex') {
674: $message = '\textbf{'.$message.'}';
675: } else {
676: $message = "<b>".$message."</b>";
677: $message.= $computer;
678: }
679: $added_computer_text=1;
680: if ($awarded > 0) {
681: my ($symb) = &Apache::lonnet::whichuser();
682: if (($symb ne '')
683: &&
684: ($env{'course.'.$env{'request.course.id'}.
685: '.disable_receipt_display'} ne 'yes') &&
686: ($Apache::lonhomework::type ne 'practice')) {
687: $message.=(($target eq 'web')?'<br />':' ').
688: &mt('Your receipt is [_1]',
689: (&Apache::lonnet::receipt($Apache::inputtags::part).
690: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
691: }
692: }
693: }
694: $button=0;
695: $previousmsg='';
696: } elsif ($solved =~ /^excused/) {
697: if ($target eq 'tex') {
698: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
699: } else {
700: $message = "<b>".&mt('You are excused from the problem.')."</b>";
701: }
702: $css_class=$possible_class{'charged_try'};
703: $button=0;
704: $previousmsg='';
705: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
706: if ($solved =~ /^incorrect/ || $solved eq '') {
707: $message = &mt("Incorrect").".";
708: $css_class=$possible_class{'charged_try'};
709: $button=1;
710: } else {
711: if ($target eq 'tex') {
712: $message = '\textbf{'.&mt('You are correct.').'}';
713: } else {
714: $message = "<b>".&mt('You are correct.')."</b>";
715: $message.= $computer;
716: }
717: $added_computer_text=1;
718: if ($awarded > 0
719: && $env{'course.'.
720: $env{'request.course.id'}.
721: '.disable_receipt_display'} ne 'yes') {
722: $message.=(($target eq 'web')?'<br />':' ').
723: &mt('Your receipt is [_1]',
724: (&Apache::lonnet::receipt($Apache::inputtags::part).
725: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
726: }
727: $css_class=$possible_class{'correct'};
728: $button=0;
729: $previousmsg='';
730: }
731: } elsif ($award eq 'NO_RESPONSE') {
732: $message = '';
733: $css_class=$possible_class{'no_feedback'};
734: $button=1;
735: } elsif ($award eq 'EXTRA_ANSWER') {
736: $message = &mt('Some extra items were submitted.');
737: $css_class=$possible_class{'not_charged_try'};
738: $button = 1;
739: } elsif ($award eq 'MISSING_ANSWER') {
740: $message = &mt('Some items were not submitted.');
741: if ($target ne 'tex') {
742: $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
743: }
744: $css_class=$possible_class{'not_charged_try'};
745: $button = 1;
746: } elsif ($award eq 'ERROR') {
747: $message = &mt('An error occurred while grading your answer.');
748: $css_class=$possible_class{'not_charged_try'};
749: $button = 1;
750: } elsif ($award eq 'TOO_LONG') {
751: $message = &mt("The submitted answer was too long.");
752: $css_class=$possible_class{'not_charged_try'};
753: $button=1;
754: } elsif ($award eq 'WANTED_NUMERIC') {
755: $message = &mt("This question expects a numeric answer.");
756: $css_class=$possible_class{'not_charged_try'};
757: $button=1;
758: } elsif ($award eq 'MISORDERED_RANK') {
759: $message = &mt('You have provided an invalid ranking.');
760: if ($target ne 'tex') {
761: $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
762: }
763: $css_class=$possible_class{'not_charged_try'};
764: $button=1;
765: } elsif ($award eq 'EXCESS_FILESIZE') {
766: $message = &mt('Submission won\'t be graded. The combined size of submitted files exceeded the amount allowed.');
767: $css_class=$possible_class{'not_charged_try'};
768: $button=1;
769:
770: } elsif ($award eq 'INVALID_FILETYPE') {
771: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
772: $css_class=$possible_class{'not_charged_try'};
773: $button=1;
774: } elsif ($award eq 'SIG_FAIL') {
775: my ($used,$min,$max)=split(':',$awardmsg);
776: my $word = ($used < $min) ? 'more' : 'fewer';
777: $message = &mt("Submission not graded. Use $word digits.",$used);
778: $css_class=$possible_class{'not_charged_try'};
779: $button=1;
780: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
781: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
782: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
783: $css_class=$possible_class{'not_charged_try'};
784: $button=1;
785: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
786: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
787: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
788: $css_class=$possible_class{'not_charged_try'};
789: $button=1;
790: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
791: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
792: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
793: $css_class=$possible_class{'not_charged_try'};
794: $button=1;
795: } elsif ($award eq 'UNIT_NOTNEEDED') {
796: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
797: $css_class=$possible_class{'not_charged_try'};
798: $button=1;
799: } elsif ($award eq 'NO_UNIT') {
800: $message = &mt("Units required").'.';
801: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
802: $css_class=$possible_class{'not_charged_try'};
803: $button=1;
804: } elsif ($award eq 'COMMA_FAIL') {
805: $message = &mt("Proper comma separation is required").'.';
806: $css_class=$possible_class{'not_charged_try'};
807: $button=1;
808: } elsif ($award eq 'BAD_FORMULA') {
809: $message = &mt("Unable to understand formula").'.';
810: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
811: $css_class=$possible_class{'not_charged_try'};
812: $button=1;
813: } elsif ($award eq 'INCORRECT') {
814: $message = &mt("Incorrect").'.';
815: $css_class=$possible_class{'charged_try'};
816: $button=1;
817: } elsif ($award eq 'SUBMITTED') {
818: $message = &mt("Your submission has been recorded.");
819: $css_class=$possible_class{'no_grade'};
820: $button=1;
821: } elsif ($award eq 'DRAFT') {
822: $message = &mt("A draft copy has been saved.");
823: $css_class=$possible_class{'not_charged_try'};
824: $button=1;
825: } elsif ($award eq 'ASSIGNED_SCORE') {
826: $message = &mt("A score has been assigned.");
827: $css_class=$possible_class{'correct'};
828: $button=0;
829: } elsif ($award eq '') {
830: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
831: $message = &mt("Nothing submitted.");
832: $css_class=$possible_class{'charged_try'};
833: } else {
834: $css_class=$possible_class{'not_charged_try'};
835: }
836: $button=1;
837: } else {
838: $message = &mt("Unknown message").": $award";
839: $button=1;
840: }
841: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
842: foreach my $resid(@Apache::inputtags::response){
843: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
844: $message.='<br />';
845: my @files = split(/\s*,\s*/,
846: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
847: my $file_msg;
848: foreach my $file (@files) {
849: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
850: }
851: $message .= &mt('Returned file(s): [_1]',$file_msg);
852: }
853: }
854:
855: if (&Apache::lonhomework::hide_problem_status()
856: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
857: && &hide_award($award)) {
858: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
859: $css_class=$possible_class{'no_grade'};
860: $button=1;
861: }
862: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
863: !$added_computer_text && $target ne 'tex') {
864: $message.= $computer;
865: $added_computer_text=1;
866: }
867: if ($Apache::lonhomework::type eq 'practice') {
868: if ($target eq 'web') {
869: $message .= '<br />';
870: } else {
871: $message .= ' ';
872: }
873: $message.=&mt('Submissions to practice problems are not permanently recorded.');
874: }
875:
876: return ($button,$css_class,$message,$previousmsg);
877: }
878:
879: sub markup_unit {
880: my ($unit,$target)=@_;
881: if ($target eq 'tex') {
882: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
883: } else {
884: return "<tt>".$unit."</tt>";
885: }
886: }
887:
888: sub removealldata {
889: my ($id)=@_;
890: foreach my $key (keys(%Apache::lonhomework::results)) {
891: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
892: &Apache::lonxml::debug("Removing $key");
893: delete($Apache::lonhomework::results{$key});
894: }
895: }
896: }
897:
898: sub hidealldata {
899: my ($id)=@_;
900: foreach my $key (keys(%Apache::lonhomework::results)) {
901: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
902: &Apache::lonxml::debug("Hidding $key");
903: my $newkey=$key;
904: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
905: $Apache::lonhomework::results{$newkey}=
906: $Apache::lonhomework::results{$key};
907: delete($Apache::lonhomework::results{$key});
908: }
909: }
910: }
911:
912: sub setgradedata {
913: my ($award,$msg,$id,$previously_used) = @_;
914: if ($Apache::lonhomework::scantronmode &&
915: &Apache::lonnet::validCODE($env{'form.CODE'})) {
916: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
917: } elsif ($Apache::lonhomework::scantronmode &&
918: $env{'form.CODE'} eq '' &&
919: $Apache::lonhomework::history{"resource.CODE"} ne '') {
920: $Apache::lonhomework::results{"resource.CODE"}='';
921: }
922:
923: if (!$Apache::lonhomework::scantronmode &&
924: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
925: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
926: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
927: return '';
928: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
929: /^correct/
930: || $Apache::lonhomework::scantronmode
931: || &Apache::lonhomework::hide_problem_status() ) {
932: # the student doesn't already have it correct,
933: # or we are in a mode (scantron orno problem status) where a correct
934: # can become incorrect
935: # handle assignment of tries and solved status
936: my $solvemsg;
937: if ($Apache::lonhomework::scantronmode) {
938: $solvemsg='correct_by_scantron';
939: } else {
940: $solvemsg='correct_by_student';
941: }
942: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
943: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
944: }
945: if ( $award eq 'ASSIGNED_SCORE') {
946: $Apache::lonhomework::results{"resource.$id.tries"} =
947: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
948: $Apache::lonhomework::results{"resource.$id.solved"} =
949: $solvemsg;
950: my $numawards=scalar(@Apache::inputtags::response);
951: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
952: foreach my $res (@Apache::inputtags::response) {
953: if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
954: $Apache::lonhomework::results{"resource.$id.awarded"}+=
955: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
956: } else {
957: $Apache::lonhomework::results{"resource.$id.awarded"}+=
958: &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
959: }
960: }
961: if ($numawards > 0) {
962: $Apache::lonhomework::results{"resource.$id.awarded"}/=
963: $numawards;
964: }
965: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
966: $Apache::lonhomework::results{"resource.$id.tries"} =
967: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
968: $Apache::lonhomework::results{"resource.$id.solved"} =
969: $solvemsg;
970: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
971: } elsif ( $award eq 'INCORRECT' ) {
972: $Apache::lonhomework::results{"resource.$id.tries"} =
973: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
974: if (&Apache::lonhomework::hide_problem_status()
975: || $Apache::lonhomework::scantronmode) {
976: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
977: }
978: $Apache::lonhomework::results{"resource.$id.solved"} =
979: 'incorrect_attempted';
980: } elsif ( $award eq 'SUBMITTED' ) {
981: $Apache::lonhomework::results{"resource.$id.tries"} =
982: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
983: $Apache::lonhomework::results{"resource.$id.solved"} =
984: 'ungraded_attempted';
985: } elsif ( $award eq 'DRAFT' ) {
986: $Apache::lonhomework::results{"resource.$id.solved"} = '';
987: } elsif ( $award eq 'NO_RESPONSE' ) {
988: #no real response so delete any data that got stored
989: &removealldata($id);
990: return '';
991: } else {
992: $Apache::lonhomework::results{"resource.$id.solved"} =
993: 'incorrect_attempted';
994: if (&Apache::lonhomework::show_no_problem_status()
995: || $Apache::lonhomework::scantronmode) {
996: $Apache::lonhomework::results{"resource.$id.tries"} =
997: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
998: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
999: }
1000:
1001: if (&Apache::lonhomework::show_some_problem_status()) {
1002: # clear out the awarded if they had gotten it wrong/right
1003: # and are now in an error mode
1004: $Apache::lonhomework::results{"resource.$id.awarded"} = '';
1005: }
1006: }
1007: if (defined($msg)) {
1008: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
1009: }
1010: # did either of the overall awards chage? If so ignore the
1011: # previous check
1012: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
1013: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
1014: ($Apache::lonhomework::results{"resource.$id.solved"} eq
1015: $Apache::lonhomework::history{"resource.$id.solved"})) {
1016: # check if this was a previous submission if it was delete the
1017: # unneeded data and update the previously_used attribute
1018: if ( $previously_used eq 'PREVIOUSLY_USED') {
1019: if (&Apache::lonhomework::show_problem_status()) {
1020: delete($Apache::lonhomework::results{"resource.$id.tries"});
1021: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
1022: }
1023: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1024: #delete all data as they student didn't do anything, but save
1025: #the list of collaborators.
1026: &removealldata($id);
1027: #and since they didn't do anything we were never here
1028: return '';
1029: } else {
1030: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1031: }
1032: }
1033: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
1034: /^correct/ ) {
1035: #delete all data as they student already has it correct
1036: &removealldata($id);
1037: #and since they didn't do anything we were never here
1038: return '';
1039: }
1040: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1041: if ($award eq 'SUBMITTED') {
1042: &Apache::response::add_to_gradingqueue();
1043: }
1044: }
1045:
1046: sub find_which_previous {
1047: my ($version) = @_;
1048: my $part = $Apache::inputtags::part;
1049: my (@previous_version);
1050: foreach my $resp (@Apache::inputtags::response) {
1051: my $key = "$version:resource.$part.$resp.submission";
1052: my $submission = $Apache::lonhomework::history{$key};
1053: my %previous = &Apache::response::check_for_previous($submission,
1054: $part,$resp,
1055: $version);
1056: push(@previous_version,$previous{'version'});
1057: }
1058: return &previous_match(\@previous_version,
1059: scalar(@Apache::inputtags::response));
1060: }
1061:
1062: sub previous_match {
1063: my ($previous_array,$count) = @_;
1064: my $match = 0;
1065: my @matches;
1066: foreach my $versionar (@$previous_array) {
1067: foreach my $version (@$versionar) {
1068: $matches[$version]++;
1069: }
1070: }
1071: my $which=0;
1072: foreach my $elem (@matches) {
1073: if ($elem eq $count) {
1074: $match=1;
1075: last;
1076: }
1077: $which++;
1078: }
1079: return ($match,$which);
1080: }
1081:
1082: sub grade {
1083: my ($target) = @_;
1084: my $id = $Apache::inputtags::part;
1085: my $response='';
1086: if ( defined $env{'form.submitted'}) {
1087: my (@awards,@msgs);
1088: foreach $response (@Apache::inputtags::response) {
1089: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
1090: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1091: &Apache::lonxml::debug("keeping $value from $response for $id");
1092: push (@awards,$value);
1093: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
1094: &Apache::lonxml::debug("got message $value from $response for $id");
1095: push (@msgs,$value);
1096: }
1097: my ($finalaward,$msg) =
1098: &finalizeawards(\@awards,\@msgs,undef,undef,
1099: $Apache::lonhomework::scantronmode);
1100: my $previously_used;
1101: if ( $#Apache::inputtags::previous eq $#awards ) {
1102: my ($match) =
1103: &previous_match(\@Apache::inputtags::previous_version,
1104: scalar(@Apache::inputtags::response));
1105:
1106: if ($match) {
1107: $previously_used = 'PREVIOUSLY_LAST';
1108: foreach my $value (@Apache::inputtags::previous) {
1109: if ($value eq 'PREVIOUSLY_USED' ) {
1110: $previously_used = $value;
1111: last;
1112: }
1113: }
1114: }
1115: }
1116: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
1117: &setgradedata($finalaward,$msg,$id,$previously_used);
1118: }
1119: return '';
1120: }
1121:
1122: sub get_grade_messages {
1123: my ($id,$prefix,$target,$status) = @_;
1124:
1125: my ($message,$latemessage,$trystr,$previousmsg);
1126: my $showbutton = 1;
1127:
1128: my $award = $Apache::lonhomework::history{"$prefix.award"};
1129: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1130: my $solved = $Apache::lonhomework::history{"$prefix.solved"};
1131: my $previous = $Apache::lonhomework::history{"$prefix.previous"};
1132: my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
1133: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1134: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1135: &Apache::lonxml::debug('Getting message');
1136: ($showbutton,my $css_class,$message,$previousmsg) =
1137: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1138: $target);
1139: if ($target eq 'tex') {
1140: $message='\vskip 2 mm '.$message.' ';
1141: } else {
1142: $message="<td class=\"$css_class\">$message</td>";
1143: if ($previousmsg) {
1144: $previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1145: }
1146: }
1147: }
1148: my $tries = $Apache::lonhomework::history{"$prefix.tries"};
1149: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1150: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1151: #if tries are set to negative turn off the Tries/Button and messages
1152: if (defined($maxtries) && $maxtries < 0) { return ''; }
1153: if ( $tries eq '' ) { $tries = '0'; }
1154: if ( $maxtries eq '' ) { $maxtries = '2'; }
1155: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
1156: my $tries_text=&mt('Tries');
1157: if ( $Apache::lonhomework::type eq 'survey' ||
1158: $Apache::lonhomework::parsing_a_task) {
1159: $tries_text=&mt('Submissions');
1160: }
1161:
1162: if ($showbutton) {
1163: if ($target eq 'tex') {
1164: if ($env{'request.state'} ne "construct"
1165: && $Apache::lonhomework::type ne 'exam'
1166: && $env{'form.suppress_tries'} ne 'yes') {
1167: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.
1168: $tries.'/'.$maxtries.'} \vskip 2 mm ';
1169: } else {
1170: $trystr = '\vskip 0 mm ';
1171: }
1172: } else {
1173: $trystr = "<td><nobr>".$tries_text." $tries";
1174: if ($Apache::lonhomework::parsing_a_task) {
1175: } elsif($env{'request.state'} ne 'construct') {
1176: $trystr.="/$maxtries";
1177: } else {
1178: if (defined($Apache::inputtags::params{'maxtries'})) {
1179: $trystr.="/".$Apache::inputtags::params{'maxtries'};
1180: }
1181: }
1182: $trystr.="</nobr></td>";
1183: }
1184: }
1185:
1186: if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
1187: #last submissions was after due date
1188: $latemessage=&mt(' The last submission was after the Due Date ');;
1189: if ($target eq 'web') {
1190: $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1191: }
1192: }
1193: return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
1194: }
1195:
1196: sub gradestatus {
1197: my ($id,$target,$no_previous) = @_;
1198: my $showbutton = 1;
1199: my $message = '';
1200: my $latemessage = '';
1201: my $trystr='';
1202: my $button='';
1203: my $previousmsg='';
1204:
1205: my $status = $Apache::inputtags::status['-1'];
1206: &Apache::lonxml::debug("gradestatus has :$status:");
1207: if ( $status ne 'CLOSED'
1208: && $status ne 'UNAVAILABLE'
1209: && $status ne 'INVALID_ACCESS'
1210: && $status ne 'NEEDS_CHECKIN'
1211: && $status ne 'NOT_IN_A_SLOT') {
1212:
1213: ($previousmsg,$latemessage,$message,$trystr) =
1214: &get_grade_messages($id,"resource.$id",$target,$status,
1215: $showbutton);
1216: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1217: $showbutton = 0;
1218: }
1219: if ( $status eq 'SHOW_ANSWER') {
1220: undef($previousmsg);
1221: }
1222: if ( $showbutton ) {
1223: if ($target ne 'tex') {
1224: $button =
1225: '<input
1226: onmouseup="javascript:setSubmittedPart(\''.$id.'\')"
1227: onsubmit="javascript:setSubmittedPart(\''.$id.'\')"
1228: type="submit" name="submit_'.$id.'"
1229: value="'.&mt('Submit Answer').'" />';
1230: }
1231: }
1232:
1233: }
1234: my $output= $previousmsg.$latemessage.$message.$trystr;
1235: if ($output =~ /^\s*$/) {
1236: return $button;
1237: } else {
1238: if ($target eq 'tex') {
1239: return $button.' \vskip 0 mm '.$output.' ';
1240: } else {
1241: $output =
1242: '<table><tr><td>'.$button.'</td>'.$output;
1243: if (!$no_previous) {
1244: $output.='<td>'.&previous_tries($id,$target).'</td>';
1245: }
1246: $output.= '</tr></table>';
1247: return $output;
1248: }
1249: }
1250: }
1251:
1252: sub previous_tries {
1253: my ($id,$target) = @_;
1254: my $output;
1255: my $status = $Apache::inputtags::status['-1'];
1256:
1257: my $count;
1258: my %count_lookup;
1259:
1260: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1261: my $prefix = $i.":resource.$id";
1262:
1263: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1264: $count++;
1265: $count_lookup{$i} = $count;
1266:
1267: my ($previousmsg,$latemessage,$message,$trystr);
1268:
1269: ($previousmsg,$latemessage,$message,$trystr) =
1270: &get_grade_messages($id,"$prefix",$target,$status);
1271:
1272: if ($previousmsg ne '') {
1273: my ($match,$which) = &find_which_previous($i);
1274: $message=$previousmsg;
1275: my $previous = $count_lookup{$which};
1276: $message =~ s{(</td>)}{ as submission \# $previous $1};
1277: } elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1278: if (!(&Apache::lonhomework::hide_problem_status()
1279: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
1280: && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
1281: ) {
1282:
1283: my $txt_correct = &mt('Correct');
1284: $message =~ s{(<td.*?>)(.*?)(</td>)}
1285: {$1 <strong>$txt_correct</strong>. $3}s;
1286: }
1287: my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1288: $message =~ s{(</td>)}{ $trystr $1};
1289: }
1290: my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
1291: $message =~ s{(<td.*?>)}{<td>};
1292:
1293:
1294: $output.='<tr class="'.$class.'">';
1295: $output.='<td align="center">'.$count.'</td>';
1296: $output.=$message;
1297:
1298: foreach my $resid (@Apache::inputtags::response) {
1299: my $prefix = $prefix.".$resid";
1300: if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
1301: my $submission =
1302: $Apache::inputtags::submission_display{"$prefix.submission"};
1303: if (!defined($submission)) {
1304: $submission =
1305: $Apache::lonhomework::history{"$prefix.submission"};
1306: }
1307: $output.='<td>'.$submission.'</td>';
1308: } else {
1309: $output.='<td></td>';
1310: }
1311: }
1312: $output.=&Apache::loncommon::end_data_table_row()."\n";
1313: }
1314: return if ($output eq '');
1315: my $headers =
1316: '<tr>'.'<th>'.&mt('Submission #').'</th><th>'.&mt('Try').
1317: '</th><th colspan="'.scalar(@Apache::inputtags::response).'">'.
1318: &mt('Submitted Answer').'</th>';
1319: $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1320: #return $output;
1321: $output = &Apache::loncommon::js_ready($output);
1322: $output.='<br /><form action=""><center><input type="button" name="close" value="'.&mt('Close Window').'" onClick="window.close()" /></center></form>';
1323:
1324: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1325: my $start_page =
1326: &Apache::loncommon::start_page('Previous Tries', undef,
1327: {'only_body' => 1,
1328: 'bgcolor' => '#FFFFFF',
1329: 'js_ready' => 1,
1330: 'inherit_jsmath' => 1, });
1331: my $end_page =
1332: &Apache::loncommon::end_page({'js_ready' => 1,});
1333: my $prefix = $env{'form.request.prefix'};
1334: $prefix =~ tr{.}{_};
1335: my $function_name = "LONCAPA_previous_tries_".$prefix.
1336: $Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1337: my $result ="<script type=\"text/javascript\">
1338: // <![CDATA[
1339: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page $output $end_page');newWindow.document.close();newWindow.focus()}
1340: // ]]>
1341: </script><a href=\"javascript:$function_name();void(0);\">".&mt("Previous Tries")."</a><br />";
1342: #use Data::Dumper;
1343: #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display));
1344: return $result;
1345: }
1346:
1347: 1;
1348: __END__
1349:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>