1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.247 2008/09/11 14:47:17 bisitz 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: 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
529: 'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
530: if ($award eq $possibleaward) { return 1; }
531: }
532: return 0;
533: }
534:
535: {
536: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
537: 'TOO_LONG',
538: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
539: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
540: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
541: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
542: 'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
543: 'APPROX_ANS', 'EXACT_ANS');
544: my $i=0;
545: my %fwd_awards = map { ($_,$i++) } @awards;
546: my $max=scalar(@awards);
547: @awards=reverse(@awards);
548: $i=0;
549: my %rev_awards = map { ($_,$i++) } @awards;
550:
551: sub awarddetail_to_awarded {
552: my ($awarddetail) = @_;
553: if ($awarddetail eq 'EXACT_ANS'
554: || $awarddetail eq 'APPROX_ANS') {
555: return 1;
556: }
557: return 0;
558: }
559:
560: sub hide_award {
561: my ($award) = @_;
562: if (&Apache::lonhomework::show_no_problem_status()) {
563: return 1;
564: }
565: if ($award =~
566: /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) {
567: return 1;
568: }
569: return 0;
570: }
571:
572: sub finalizeawards {
573: my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
574: my $result;
575: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
576: if ($result eq '' ) {
577: my $blankcount;
578: foreach my $award (@$awardref) {
579: if ($award eq '') {
580: $result='MISSING_ANSWER';
581: $blankcount++;
582: }
583: }
584: if ($blankcount == ($#$awardref + 1)) {
585: return ('NO_RESPONSE');
586: }
587: }
588: if (!$final_scantron && defined($result)) { return ($result); }
589:
590: # if in scantron mode, if the award for any response is
591: # assigned score, then the part gets an assigned score
592: if ($final_scantron
593: && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
594: return ('ASSIGNED_SCORE');
595: }
596:
597: # if in scantron mode, if the award for any response is
598: # correct and there are non-correct responses,
599: # then the part gets an assigned score
600: if ($final_scantron
601: && (grep { $_ eq 'EXACT_ANS' ||
602: $_ eq 'APPROX_ANS' } (@$awardref))
603: && (grep { $_ ne 'EXACT_ANS' &&
604: $_ ne 'APPROX_ANS' } (@$awardref))) {
605: return ('ASSIGNED_SCORE');
606: }
607: # these awards are ordered from most important error through best correct
608: my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
609:
610: my $best = $max;
611: my $j=0;
612: my $which;
613: foreach my $award (@$awardref) {
614: if ($awards->{$award} < $best) {
615: $best = $awards->{$award};
616: $which = $j;
617: }
618: $j++;
619: }
620:
621: if (defined($which)) {
622: if (ref($nameref)) {
623: return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
624: } else {
625: return ($$awardref[$which],$$msgref[$which]);
626: }
627: }
628: return ('ERROR',undef);
629: }
630: }
631:
632: sub decideoutput {
633: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
634: my $message='';
635: my $button=0;
636: my $previousmsg;
637: my $css_class='orange';
638: my $added_computer_text=0;
639: my %possible_class =
640: ( 'correct' => 'LC_answer_correct',
641: 'charged_try' => 'LC_answer_charged_try',
642: 'not_charged_try' => 'LC_answer_not_charged_try',
643: 'no_grade' => 'LC_answer_no_grade',
644: 'no_message' => 'LC_no_message',
645: );
646:
647: my $part = $Apache::inputtags::part;
648: my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
649: my $handgrade = ('yes' eq lc($tohandgrade));
650:
651: my $computer = ($handgrade)? ''
652: : " ".&mt("Computer's answer now shown above.");
653: &Apache::lonxml::debug("handgrade has :$handgrade:");
654:
655: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
656:
657: if ($solved =~ /^correct/) {
658: $css_class=$possible_class{'correct'};
659: $message=&mt('You are correct.');
660: if ($awarded < 1 && $awarded > 0) {
661: $message=&mt('You are partially correct.');
662: $css_class=$possible_class{'not_charged_try'};
663: } elsif ($awarded < 1) {
664: $message=&mt('Incorrect.');
665: $css_class=$possible_class{'charged_try'};
666: }
667: if ($env{'request.filename'} =~
668: m|/res/lib/templates/examupload.problem$|) {
669: $message = &mt("A score has been assigned.");
670: $added_computer_text=1;
671: } else {
672: if ($target eq 'tex') {
673: $message = '\textbf{'.$message.'}';
674: } else {
675: $message = "<b>".$message."</b>";
676: $message.= $computer;
677: }
678: $added_computer_text=1;
679: if ($awarded > 0) {
680: my ($symb) = &Apache::lonnet::whichuser();
681: if (($symb ne '')
682: &&
683: ($env{'course.'.$env{'request.course.id'}.
684: '.disable_receipt_display'} ne 'yes') &&
685: ($Apache::lonhomework::type ne 'practice')) {
686: $message.=(($target eq 'web')?'<br />':' ').
687: &mt('Your receipt is [_1]',
688: (&Apache::lonnet::receipt($Apache::inputtags::part).
689: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
690: }
691: }
692: }
693: $button=0;
694: $previousmsg='';
695: } elsif ($solved =~ /^excused/) {
696: if ($target eq 'tex') {
697: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
698: } else {
699: $message = "<b>".&mt('You are excused from the problem.')."</b>";
700: }
701: $css_class=$possible_class{'charged_try'};
702: $button=0;
703: $previousmsg='';
704: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
705: if ($solved =~ /^incorrect/ || $solved eq '') {
706: $message = &mt("Incorrect").".";
707: $css_class=$possible_class{'charged_try'};
708: $button=1;
709: } else {
710: if ($target eq 'tex') {
711: $message = '\textbf{'.&mt('You are correct.').'}';
712: } else {
713: $message = "<b>".&mt('You are correct.')."</b>";
714: $message.= $computer;
715: }
716: $added_computer_text=1;
717: if ($awarded > 0
718: && $env{'course.'.
719: $env{'request.course.id'}.
720: '.disable_receipt_display'} ne 'yes') {
721: $message.=(($target eq 'web')?'<br />':' ').
722: &mt('Your receipt is [_1]',
723: (&Apache::lonnet::receipt($Apache::inputtags::part).
724: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
725: }
726: $css_class=$possible_class{'correct'};
727: $button=0;
728: $previousmsg='';
729: }
730: } elsif ($award eq 'NO_RESPONSE') {
731: $message = '';
732: $css_class=$possible_class{'no_feedback'};
733: $button=1;
734: } elsif ($award eq 'EXTRA_ANSWER') {
735: $message = &mt('Some extra items were submitted.');
736: $css_class=$possible_class{'not_charged_try'};
737: $button = 1;
738: } elsif ($award eq 'MISSING_ANSWER') {
739: $message = &mt('Some items were not submitted.');
740: if ($target ne 'tex') {
741: $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
742: }
743: $css_class=$possible_class{'not_charged_try'};
744: $button = 1;
745: } elsif ($award eq 'ERROR') {
746: $message = &mt('An error occurred while grading your answer.');
747: $css_class=$possible_class{'not_charged_try'};
748: $button = 1;
749: } elsif ($award eq 'TOO_LONG') {
750: $message = &mt("The submitted answer was too long.");
751: $css_class=$possible_class{'not_charged_try'};
752: $button=1;
753: } elsif ($award eq 'WANTED_NUMERIC') {
754: $message = &mt("This question expects a numeric answer.");
755: $css_class=$possible_class{'not_charged_try'};
756: $button=1;
757: } elsif ($award eq 'MISORDERED_RANK') {
758: $message = &mt('You have provided an invalid ranking.');
759: if ($target ne 'tex') {
760: $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
761: }
762: $css_class=$possible_class{'not_charged_try'};
763: $button=1;
764: } elsif ($award eq 'INVALID_FILETYPE') {
765: $message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
766: $css_class=$possible_class{'not_charged_try'};
767: $button=1;
768: } elsif ($award eq 'SIG_FAIL') {
769: my ($used,$min,$max)=split(':',$awardmsg);
770: my $word = ($used < $min) ? 'more' : 'fewer';
771: $message = &mt("Submission not graded. Use $word digits.",$used);
772: $css_class=$possible_class{'not_charged_try'};
773: $button=1;
774: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
775: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
776: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
777: $css_class=$possible_class{'not_charged_try'};
778: $button=1;
779: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
780: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
781: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
782: $css_class=$possible_class{'not_charged_try'};
783: $button=1;
784: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
785: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
786: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
787: $css_class=$possible_class{'not_charged_try'};
788: $button=1;
789: } elsif ($award eq 'UNIT_NOTNEEDED') {
790: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
791: $css_class=$possible_class{'not_charged_try'};
792: $button=1;
793: } elsif ($award eq 'NO_UNIT') {
794: $message = &mt("Units required").'.';
795: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
796: $css_class=$possible_class{'not_charged_try'};
797: $button=1;
798: } elsif ($award eq 'COMMA_FAIL') {
799: $message = &mt("Proper comma separation is required").'.';
800: $css_class=$possible_class{'not_charged_try'};
801: $button=1;
802: } elsif ($award eq 'BAD_FORMULA') {
803: $message = &mt("Unable to understand formula").'.';
804: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
805: $css_class=$possible_class{'not_charged_try'};
806: $button=1;
807: } elsif ($award eq 'INCORRECT') {
808: $message = &mt("Incorrect").'.';
809: $css_class=$possible_class{'charged_try'};
810: $button=1;
811: } elsif ($award eq 'SUBMITTED') {
812: $message = &mt("Your submission has been recorded.");
813: $css_class=$possible_class{'no_grade'};
814: $button=1;
815: } elsif ($award eq 'DRAFT') {
816: $message = &mt("A draft copy has been saved.");
817: $css_class=$possible_class{'not_charged_try'};
818: $button=1;
819: } elsif ($award eq 'ASSIGNED_SCORE') {
820: $message = &mt("A score has been assigned.");
821: $css_class=$possible_class{'correct'};
822: $button=0;
823: } elsif ($award eq '') {
824: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
825: $message = &mt("Nothing submitted.");
826: $css_class=$possible_class{'charged_try'};
827: } else {
828: $css_class=$possible_class{'not_charged_try'};
829: }
830: $button=1;
831: } else {
832: $message = &mt("Unknown message").": $award";
833: $button=1;
834: }
835: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
836: foreach my $resid(@Apache::inputtags::response){
837: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
838: $message.='<br />';
839: my @files = split(/\s*,\s*/,
840: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
841: my $file_msg;
842: foreach my $file (@files) {
843: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
844: }
845: $message .= &mt('Returned file(s): [_1]',$file_msg);
846: }
847: }
848:
849: if (&Apache::lonhomework::hide_problem_status()
850: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
851: && &hide_award($award)) {
852: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
853: $css_class=$possible_class{'no_grade'};
854: $button=1;
855: }
856: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
857: !$added_computer_text && $target ne 'tex') {
858: $message.= $computer;
859: $added_computer_text=1;
860: }
861: if ($Apache::lonhomework::type eq 'practice') {
862: if ($target eq 'web') {
863: $message .= '<br />';
864: } else {
865: $message .= ' ';
866: }
867: $message.=&mt('Submissions to practice problems are not permanently recorded.');
868: }
869:
870: return ($button,$css_class,$message,$previousmsg);
871: }
872:
873: sub markup_unit {
874: my ($unit,$target)=@_;
875: if ($target eq 'tex') {
876: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
877: } else {
878: return "<tt>".$unit."</tt>";
879: }
880: }
881:
882: sub removealldata {
883: my ($id)=@_;
884: foreach my $key (keys(%Apache::lonhomework::results)) {
885: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
886: &Apache::lonxml::debug("Removing $key");
887: delete($Apache::lonhomework::results{$key});
888: }
889: }
890: }
891:
892: sub hidealldata {
893: my ($id)=@_;
894: foreach my $key (keys(%Apache::lonhomework::results)) {
895: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
896: &Apache::lonxml::debug("Hidding $key");
897: my $newkey=$key;
898: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
899: $Apache::lonhomework::results{$newkey}=
900: $Apache::lonhomework::results{$key};
901: delete($Apache::lonhomework::results{$key});
902: }
903: }
904: }
905:
906: sub setgradedata {
907: my ($award,$msg,$id,$previously_used) = @_;
908: if ($Apache::lonhomework::scantronmode &&
909: &Apache::lonnet::validCODE($env{'form.CODE'})) {
910: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
911: } elsif ($Apache::lonhomework::scantronmode &&
912: $env{'form.CODE'} eq '' &&
913: $Apache::lonhomework::history{"resource.CODE"} ne '') {
914: $Apache::lonhomework::results{"resource.CODE"}='';
915: }
916:
917: if (!$Apache::lonhomework::scantronmode &&
918: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
919: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
920: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
921: return '';
922: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
923: /^correct/
924: || $Apache::lonhomework::scantronmode
925: || &Apache::lonhomework::hide_problem_status() ) {
926: # the student doesn't already have it correct,
927: # or we are in a mode (scantron orno problem status) where a correct
928: # can become incorrect
929: # handle assignment of tries and solved status
930: my $solvemsg;
931: if ($Apache::lonhomework::scantronmode) {
932: $solvemsg='correct_by_scantron';
933: } else {
934: $solvemsg='correct_by_student';
935: }
936: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
937: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
938: }
939: if ( $award eq 'ASSIGNED_SCORE') {
940: $Apache::lonhomework::results{"resource.$id.tries"} =
941: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
942: $Apache::lonhomework::results{"resource.$id.solved"} =
943: $solvemsg;
944: my $numawards=scalar(@Apache::inputtags::response);
945: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
946: foreach my $res (@Apache::inputtags::response) {
947: if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
948: $Apache::lonhomework::results{"resource.$id.awarded"}+=
949: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
950: } else {
951: $Apache::lonhomework::results{"resource.$id.awarded"}+=
952: &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
953: }
954: }
955: if ($numawards > 0) {
956: $Apache::lonhomework::results{"resource.$id.awarded"}/=
957: $numawards;
958: }
959: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
960: $Apache::lonhomework::results{"resource.$id.tries"} =
961: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
962: $Apache::lonhomework::results{"resource.$id.solved"} =
963: $solvemsg;
964: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
965: } elsif ( $award eq 'INCORRECT' ) {
966: $Apache::lonhomework::results{"resource.$id.tries"} =
967: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
968: if (&Apache::lonhomework::hide_problem_status()
969: || $Apache::lonhomework::scantronmode) {
970: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
971: }
972: $Apache::lonhomework::results{"resource.$id.solved"} =
973: 'incorrect_attempted';
974: } elsif ( $award eq 'SUBMITTED' ) {
975: $Apache::lonhomework::results{"resource.$id.tries"} =
976: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
977: $Apache::lonhomework::results{"resource.$id.solved"} =
978: 'ungraded_attempted';
979: } elsif ( $award eq 'DRAFT' ) {
980: $Apache::lonhomework::results{"resource.$id.solved"} = '';
981: } elsif ( $award eq 'NO_RESPONSE' ) {
982: #no real response so delete any data that got stored
983: &removealldata($id);
984: return '';
985: } else {
986: $Apache::lonhomework::results{"resource.$id.solved"} =
987: 'incorrect_attempted';
988: if (&Apache::lonhomework::show_no_problem_status()
989: || $Apache::lonhomework::scantronmode) {
990: $Apache::lonhomework::results{"resource.$id.tries"} =
991: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
992: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
993: }
994:
995: if (&Apache::lonhomework::show_some_problem_status()) {
996: # clear out the awarded if they had gotten it wrong/right
997: # and are now in an error mode
998: $Apache::lonhomework::results{"resource.$id.awarded"} = '';
999: }
1000: }
1001: if (defined($msg)) {
1002: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
1003: }
1004: # did either of the overall awards chage? If so ignore the
1005: # previous check
1006: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
1007: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
1008: ($Apache::lonhomework::results{"resource.$id.solved"} eq
1009: $Apache::lonhomework::history{"resource.$id.solved"})) {
1010: # check if this was a previous submission if it was delete the
1011: # unneeded data and update the previously_used attribute
1012: if ( $previously_used eq 'PREVIOUSLY_USED') {
1013: if (&Apache::lonhomework::show_problem_status()) {
1014: delete($Apache::lonhomework::results{"resource.$id.tries"});
1015: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
1016: }
1017: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1018: #delete all data as they student didn't do anything, but save
1019: #the list of collaborators.
1020: &removealldata($id);
1021: #and since they didn't do anything we were never here
1022: return '';
1023: } else {
1024: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1025: }
1026: }
1027: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
1028: /^correct/ ) {
1029: #delete all data as they student already has it correct
1030: &removealldata($id);
1031: #and since they didn't do anything we were never here
1032: return '';
1033: }
1034: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1035: if ($award eq 'SUBMITTED') {
1036: &Apache::response::add_to_gradingqueue();
1037: }
1038: }
1039:
1040: sub find_which_previous {
1041: my ($version) = @_;
1042: my $part = $Apache::inputtags::part;
1043: my (@previous_version);
1044: foreach my $resp (@Apache::inputtags::response) {
1045: my $key = "$version:resource.$part.$resp.submission";
1046: my $submission = $Apache::lonhomework::history{$key};
1047: my %previous = &Apache::response::check_for_previous($submission,
1048: $part,$resp,
1049: $version);
1050: push(@previous_version,$previous{'version'});
1051: }
1052: return &previous_match(\@previous_version,
1053: scalar(@Apache::inputtags::response));
1054: }
1055:
1056: sub previous_match {
1057: my ($previous_array,$count) = @_;
1058: my $match = 0;
1059: my @matches;
1060: foreach my $versionar (@$previous_array) {
1061: foreach my $version (@$versionar) {
1062: $matches[$version]++;
1063: }
1064: }
1065: my $which=0;
1066: foreach my $elem (@matches) {
1067: if ($elem eq $count) {
1068: $match=1;
1069: last;
1070: }
1071: $which++;
1072: }
1073: return ($match,$which);
1074: }
1075:
1076: sub grade {
1077: my ($target) = @_;
1078: my $id = $Apache::inputtags::part;
1079: my $response='';
1080: if ( defined $env{'form.submitted'}) {
1081: my (@awards,@msgs);
1082: foreach $response (@Apache::inputtags::response) {
1083: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
1084: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1085: &Apache::lonxml::debug("keeping $value from $response for $id");
1086: push (@awards,$value);
1087: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
1088: &Apache::lonxml::debug("got message $value from $response for $id");
1089: push (@msgs,$value);
1090: }
1091: my ($finalaward,$msg) =
1092: &finalizeawards(\@awards,\@msgs,undef,undef,
1093: $Apache::lonhomework::scantronmode);
1094: my $previously_used;
1095: if ( $#Apache::inputtags::previous eq $#awards ) {
1096: my ($match) =
1097: &previous_match(\@Apache::inputtags::previous_version,
1098: scalar(@Apache::inputtags::response));
1099:
1100: if ($match) {
1101: $previously_used = 'PREVIOUSLY_LAST';
1102: foreach my $value (@Apache::inputtags::previous) {
1103: if ($value eq 'PREVIOUSLY_USED' ) {
1104: $previously_used = $value;
1105: last;
1106: }
1107: }
1108: }
1109: }
1110: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
1111: &setgradedata($finalaward,$msg,$id,$previously_used);
1112: }
1113: return '';
1114: }
1115:
1116: sub get_grade_messages {
1117: my ($id,$prefix,$target,$status) = @_;
1118:
1119: my ($message,$latemessage,$trystr,$previousmsg);
1120: my $showbutton = 1;
1121:
1122: my $award = $Apache::lonhomework::history{"$prefix.award"};
1123: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1124: my $solved = $Apache::lonhomework::history{"$prefix.solved"};
1125: my $previous = $Apache::lonhomework::history{"$prefix.previous"};
1126: my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
1127: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1128: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1129: &Apache::lonxml::debug('Getting message');
1130: ($showbutton,my $css_class,$message,$previousmsg) =
1131: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1132: $target);
1133: if ($target eq 'tex') {
1134: $message='\vskip 2 mm '.$message.' ';
1135: } else {
1136: $message="<td class=\"$css_class\">$message</td>";
1137: if ($previousmsg) {
1138: $previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1139: }
1140: }
1141: }
1142: my $tries = $Apache::lonhomework::history{"$prefix.tries"};
1143: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1144: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1145: #if tries are set to negative turn off the Tries/Button and messages
1146: if (defined($maxtries) && $maxtries < 0) { return ''; }
1147: if ( $tries eq '' ) { $tries = '0'; }
1148: if ( $maxtries eq '' ) { $maxtries = '2'; }
1149: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
1150: my $tries_text=&mt('Tries');
1151: if ( $Apache::lonhomework::type eq 'survey' ||
1152: $Apache::lonhomework::parsing_a_task) {
1153: $tries_text=&mt('Submissions');
1154: }
1155:
1156: if ($showbutton) {
1157: if ($target eq 'tex') {
1158: if ($env{'request.state'} ne "construct"
1159: && $Apache::lonhomework::type ne 'exam'
1160: && $env{'form.suppress_tries'} ne 'yes') {
1161: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.
1162: $tries.'/'.$maxtries.'} \vskip 2 mm ';
1163: } else {
1164: $trystr = '\vskip 0 mm ';
1165: }
1166: } else {
1167: $trystr = "<td><nobr>".$tries_text." $tries";
1168: if ($Apache::lonhomework::parsing_a_task) {
1169: } elsif($env{'request.state'} ne 'construct') {
1170: $trystr.="/$maxtries";
1171: } else {
1172: if (defined($Apache::inputtags::params{'maxtries'})) {
1173: $trystr.="/".$Apache::inputtags::params{'maxtries'};
1174: }
1175: }
1176: $trystr.="</nobr></td>";
1177: }
1178: }
1179:
1180: if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
1181: #last submissions was after due date
1182: $latemessage=&mt(' The last submission was after the Due Date ');;
1183: if ($target eq 'web') {
1184: $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1185: }
1186: }
1187: return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
1188: }
1189:
1190: sub gradestatus {
1191: my ($id,$target,$no_previous) = @_;
1192: my $showbutton = 1;
1193: my $message = '';
1194: my $latemessage = '';
1195: my $trystr='';
1196: my $button='';
1197: my $previousmsg='';
1198:
1199: my $status = $Apache::inputtags::status['-1'];
1200: &Apache::lonxml::debug("gradestatus has :$status:");
1201: if ( $status ne 'CLOSED'
1202: && $status ne 'UNAVAILABLE'
1203: && $status ne 'INVALID_ACCESS'
1204: && $status ne 'NEEDS_CHECKIN'
1205: && $status ne 'NOT_IN_A_SLOT') {
1206:
1207: ($previousmsg,$latemessage,$message,$trystr) =
1208: &get_grade_messages($id,"resource.$id",$target,$status,
1209: $showbutton);
1210: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1211: $showbutton = 0;
1212: }
1213: if ( $status eq 'SHOW_ANSWER') {
1214: undef($previousmsg);
1215: }
1216: if ( $showbutton ) {
1217: if ($target ne 'tex') {
1218: $button =
1219: '<input
1220: onmouseup="javascript:setSubmittedPart(\''.$id.'\')"
1221: onsubmit="javascript:setSubmittedPart(\''.$id.'\')"
1222: type="submit" name="submit_'.$id.'"
1223: value="'.&mt('Submit Answer').'" />';
1224: }
1225: }
1226:
1227: }
1228: my $output= $previousmsg.$latemessage.$message.$trystr;
1229: if ($output =~ /^\s*$/) {
1230: return $button;
1231: } else {
1232: if ($target eq 'tex') {
1233: return $button.' \vskip 0 mm '.$output.' ';
1234: } else {
1235: $output =
1236: '<table><tr><td>'.$button.'</td>'.$output;
1237: if (!$no_previous) {
1238: $output.='<td>'.&previous_tries($id,$target).'</td>';
1239: }
1240: $output.= '</tr></table>';
1241: return $output;
1242: }
1243: }
1244: }
1245:
1246: sub previous_tries {
1247: my ($id,$target) = @_;
1248: my $output;
1249: my $status = $Apache::inputtags::status['-1'];
1250:
1251: my $count;
1252: my %count_lookup;
1253:
1254: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1255: my $prefix = $i.":resource.$id";
1256:
1257: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1258: $count++;
1259: $count_lookup{$i} = $count;
1260:
1261: my ($previousmsg,$latemessage,$message,$trystr);
1262:
1263: ($previousmsg,$latemessage,$message,$trystr) =
1264: &get_grade_messages($id,"$prefix",$target,$status);
1265:
1266: if ($previousmsg ne '') {
1267: my ($match,$which) = &find_which_previous($i);
1268: $message=$previousmsg;
1269: my $previous = $count_lookup{$which};
1270: $message =~ s{(</td>)}{ as submission \# $previous $1};
1271: } elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1272: if (!(&Apache::lonhomework::hide_problem_status()
1273: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
1274: && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
1275: ) {
1276:
1277: my $txt_correct = &mt('Correct');
1278: $message =~ s{(<td.*?>)(.*?)(</td>)}
1279: {$1 <strong>$txt_correct</strong>. $3}s;
1280: }
1281: my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1282: $message =~ s{(</td>)}{ $trystr $1};
1283: }
1284: my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
1285: $message =~ s{(<td.*?>)}{<td>};
1286:
1287:
1288: $output.='<tr class="'.$class.'">';
1289: $output.='<td align="center">'.$count.'</td>';
1290: $output.=$message;
1291:
1292: foreach my $resid (@Apache::inputtags::response) {
1293: my $prefix = $prefix.".$resid";
1294: if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
1295: my $submission =
1296: $Apache::inputtags::submission_display{"$prefix.submission"};
1297: if (!defined($submission)) {
1298: $submission =
1299: $Apache::lonhomework::history{"$prefix.submission"};
1300: }
1301: $output.='<td>'.$submission.'</td>';
1302: } else {
1303: $output.='<td></td>';
1304: }
1305: }
1306: $output.=&Apache::loncommon::end_data_table_row()."\n";
1307: }
1308: return if ($output eq '');
1309: my $headers =
1310: '<tr>'.'<th>'.&mt('Submission #').'</th><th>'.&mt('Try').
1311: '</th><th colspan="'.scalar(@Apache::inputtags::response).'">'.
1312: &mt('Submitted Answer').'</th>';
1313: $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1314: #return $output;
1315: $output = &Apache::loncommon::js_ready($output);
1316: $output.='<br /><form action=""><center><input type="button" name="close" value="'.&mt('Close Window').'" onClick="window.close()" /></center></form>';
1317:
1318: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1319: my $start_page =
1320: &Apache::loncommon::start_page('Previous Tries', undef,
1321: {'only_body' => 1,
1322: 'bgcolor' => '#FFFFFF',
1323: 'js_ready' => 1,
1324: 'inherit_jsmath' => 1, });
1325: my $end_page =
1326: &Apache::loncommon::end_page({'js_ready' => 1,});
1327: my $prefix = $env{'form.request.prefix'};
1328: $prefix =~ tr{.}{_};
1329: my $function_name = "LONCAPA_previous_tries_".$prefix.
1330: $Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1331: my $result ="<script type=\"text/javascript\">
1332: // <![CDATA[
1333: 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()}
1334: // ]]>
1335: </script><a href=\"javascript:$function_name();void(0);\">".&mt("Previous Tries")."</a><br />";
1336: #use Data::Dumper;
1337: #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display));
1338: return $result;
1339: }
1340:
1341: 1;
1342: __END__
1343:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>