File:
[LON-CAPA] /
loncom /
homework /
inputtags.pm
Revision
1.358:
download - view:
text,
annotated -
select for diffs
Sun Apr 2 03:16:28 2023 UTC (15 months, 1 week ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Bug 6979
- Shortcut to student's view of problem etc. from 'View As' item in Functions
menu above problem.
- Part of code in &submission() subroutine in grades.pm moved to separate
subroutine -- &show_last_submission() which can be called by &view_as_user(),
which in turn is called from lonhomework.pm
1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.358 2023/04/02 03:16:28 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: =pod
29:
30: =head1 NAME
31:
32: Apache::inputtags
33:
34: =head1 SYNOPSIS
35:
36:
37:
38: This is part of the LearningOnline Network with CAPA project
39: described at http://www.lon-capa.org.
40:
41:
42: =head1 NOTABLE SUBROUTINES
43:
44: =over
45:
46: =item
47:
48: =back
49:
50: =cut
51:
52: package Apache::inputtags;
53: use HTML::Entities();
54: use strict;
55: use Apache::loncommon;
56: use Apache::lonhtmlcommon;
57: use Apache::lonlocal;
58: use Apache::lonnet;
59: use LONCAPA;
60:
61:
62: BEGIN {
63: &Apache::lonxml::register('Apache::inputtags',('hiddensubmission','hiddenline','textfield','textline'));
64: }
65:
66: =pod
67:
68: =item initialize_inputtags()
69:
70: Initializes a set of global variables used during the parse of the problem.
71:
72: @Apache::inputtags::input - List of current input ids.
73: @Apache::inputtags::inputlist - List of all input ids seen this problem.
74: @Apache::inputtags::response - List of all current resopnse ids.
75: @Apache::inputtags::responselist - List of all response ids seen this
76: problem.
77: @Apache::inputtags::hint - List of all hint ids.
78: @Apache::inputtags::hintlist - List of all hint ids seen this problem.
79: @Apache::inputtags::previous - List describing if specific responseds
80: have been used
81: @Apache::inputtags::previous_version - Submission responses were used in.
82: $Apache::inputtags::part - Current part id (valid only in
83: <problem>)
84: 0 if not in a part.
85: @Apache::inputtags::partlist - List of part ids seen in the current
86: <problem>
87: @Apache::inputtags::status - List of problem statuses. First
88: element is the status of the <problem>
89: the remainder are for individual <part>s.
90: %Apache::inputtags::params - Hash of defined parameters for the
91: current response.
92: @Apache::inputtags::import - List of all ids for <import> thes get
93: join()ed and prepended.
94: @Apache::inputtags::importlist - List of all import ids seen.
95: $Apache::inputtags::response_with_no_part
96: - Flag set true if we have seen a response
97: that is not inside a <part>
98: %Apache::inputtags::answertxt - <*response> tags store correct
99: answer strings for display by <textline/>
100: in this hash.
101: %Apache::inputtags::submission_display
102: - <*response> tags store improved display
103: of submission strings for display by part
104: end.
105:
106: =cut
107:
108: sub initialize_inputtags {
109: @Apache::inputtags::input=();
110: @Apache::inputtags::inputlist=();
111: @Apache::inputtags::response=();
112: @Apache::inputtags::responselist=();
113: @Apache::inputtags::hint=();
114: @Apache::inputtags::hintlist=();
115: @Apache::inputtags::previous=();
116: @Apache::inputtags::previous_version=();
117: $Apache::inputtags::part='';
118: @Apache::inputtags::partlist=();
119: @Apache::inputtags::status=();
120: %Apache::inputtags::params=();
121: @Apache::inputtags::import=();
122: @Apache::inputtags::importlist=();
123: $Apache::inputtags::response_with_no_part=0;
124: %Apache::inputtags::answertxt=();
125: %Apache::inputtags::submission_display=();
126: }
127:
128: #
129: # provides the onblur binding for spellchecking. This could be an
130: # empty string if spellchecking was not enabled.
131: # Jquery selector binding is done rather than setting an onblur
132: # attribute because we'll need to set the element's spellcheck language
133: # option dynamically so we need $(this) to be defined.
134: #
135: # @param id - The element id to bind.
136: # @param lang - Language in which spellchecking is desired.
137: # if undef, nothing is generated.
138: # @return string - onblur specification to do the requested spellchecking.
139: #
140: sub spellcheck_onblur {
141: my ($id, $lang) = @_;
142: my $result = '';
143: if ($lang) {
144:
145: $result = <<JAVASCRIPT;
146: <script type="text/javascript">
147: \$('\#$id').blur(function() {
148: doSpellcheck('\#$id', '$lang');
149: });
150: </script>
151:
152: JAVASCRIPT
153:
154:
155: }
156: return $result;
157: }
158:
159: sub check_for_duplicate_ids {
160: my %check;
161: foreach my $id (@Apache::inputtags::partlist,
162: @Apache::inputtags::responselist,
163: @Apache::inputtags::hintlist,
164: @Apache::inputtags::importlist) {
165: $check{$id}++;
166: }
167: my @duplicates;
168: foreach my $id (sort(keys(%check))) {
169: if ($check{$id} > 1) {
170: push(@duplicates,$id);
171: }
172: }
173: if (@duplicates) {
174: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
175: }
176: }
177:
178: sub start_input {
179: my ($parstack,$safeeval)=@_;
180: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
181: push (@Apache::inputtags::input,$id);
182: push (@Apache::inputtags::inputlist,$id);
183: return $id;
184: }
185:
186: sub end_input {
187: pop @Apache::inputtags::input;
188: return '';
189: }
190:
191: sub addchars {
192: my ($fieldid,$addchars)=@_;
193: my $output='';
194: foreach (split(/\,/,$addchars)) {
195: $output.='<a href="javascript:void(document.forms.lonhomework.'.
196: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
197: }
198: return $output;
199: }
200:
201: sub start_textfield {
202: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
203: my $result = "";
204: my $id = &start_input($parstack,$safeeval);
205: my $resid=$Apache::inputtags::response[-1];
206: if ($target eq 'web') {
207: $Apache::lonxml::evaluate--;
208: my $partid=$Apache::inputtags::part;
209: my ($oldresponse,$newvariation);
210: if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
211: ($Apache::lonhomework::type eq 'randomizetry')) &&
212: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
213: if ($env{'form.'.$partid.'.rndseed'} ne
214: $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
215: $newvariation = 1;
216: }
217: }
218: unless ($newvariation) {
219: if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
220: (($env{'form.grade_username'} eq $env{'user.name'}) &&
221: ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
222: $oldresponse = $Apache::lonhomework::history{"resource.$partid.$resid.submission"};
223: } elsif (($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurvey') ||
224: ($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurveycred')) {
225: $oldresponse = '* '.&mt('(only shown to submitter)').' *';
226: } else {
227: $oldresponse = $Apache::lonhomework::history{"resource.$partid.$resid.submission"};
228: }
229: }
230: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
231: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
232: if ( $cols eq '') { $cols = 80; }
233: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
234: if ( $rows eq '') { $rows = 16; }
235: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
236: $result='';
237: my $tagident = 'HWVAL_' . $resid;
238: my $itemid = 'HWVAL_'.$partid.'_'.$resid;
239: if ($addchars) {
240: $result.=&addchars($tagident, $addchars);
241: }
242: my $textareaclass;
243: unless (&Apache::londefdef::is_inside_of($tagstack,
244: 'externalresponse')) {
245: $textareaclass = 'class="LC_richDetectHtml spellchecked"';
246: }
247: $result.= '<textarea wrap="hard" name="'.$tagident.'" id="'.$itemid.'" ' .
248: 'rows="'.$rows.'" cols="'.$cols.'" '.$textareaclass
249: .'>'.
250: &HTML::Entities::encode($oldresponse,'<>&"');
251: if ($oldresponse ne '') {
252:
253: #get rid of any startup text if the user has already responded
254: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
255: }
256: } else {
257: #show past answer in the essayresponse case
258: if ($oldresponse =~ /\S/
259: && &Apache::londefdef::is_inside_of($tagstack,
260: 'essayresponse') ) {
261: $result='<table class="LC_pastsubmission"><tr><td>'.
262: &HTML::Entities::encode($oldresponse,'"<>&').
263: '</td></tr></table>';
264: }
265: #get rid of any startup text
266: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
267: }
268: } elsif ($target eq 'grade') {
269: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
270: $style);
271: if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
272: # if the seed text is still there it wasn't a real submission
273: $env{'form.HWVAL_'.$resid}='';
274: }
275: } elsif ($target eq 'edit') {
276: $result.=&Apache::edit::tag_start($target,$token);
277: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
278: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
279: $result.=&Apache::edit::text_arg
280: ('Click-On Texts (comma sep):','addchars',$token,10);
281: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
282: $style);
283: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
284: my $spell_langs = &spelling_languages();
285: $result .= &Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
286: $spell_langs, $token);
287: } elsif ($target eq 'modified') {
288: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
289: $safeeval,'rows','cols',
290: 'addchars', 'spellcheck');
291: if ($constructtag) {
292: $result = &Apache::edit::rebuild_tag($token);
293: } else {
294: $result=$token->[4];
295: }
296: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
297: } elsif ($target eq 'tex') {
298: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
299: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
300: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
301: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
302: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
303: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
304: } else {
305: if ($env{'form.pdfFormFields'} eq 'yes') {
306: my $fieldname = $env{'request.symb'}.
307: '&part_'. $Apache::inputtags::part.
308: '&textresponse'.
309: '&HWVAL_' . $Apache::inputtags::response['-1'];
310: $result.='\TextField[name='.$fieldname.',multiline=true,height=6\baselineskip,width=270,borderwidth=0,backgroundcolor={.85 .85 .85}]\\';
311: } else {
312: my $TeXwidth=$width_of_box/80;
313: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
314: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
315: $result.='}}}\vskip 2 mm ';
316: }
317: }
318: }
319: return $result;
320: }
321:
322: sub end_textfield {
323: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
324: my $result;
325: if ($target eq 'web') {
326: my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
327: $Apache::lonxml::evaluate++;
328: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
329: my $partid=$Apache::inputtags::part;
330: my $resid = $Apache::inputtags::response[-1];
331: my $itemid = 'HWVAL_' . $partid . '_' . $resid;
332: my $result = "</textarea>";
333: $result .= &spellcheck_onblur($itemid, $spellcheck);
334: return $result;
335: }
336: } elsif ($target eq 'edit') {
337: $result=&Apache::edit::end_table();
338: }
339: &end_input;
340: return $result;
341: }
342:
343: sub exam_score_line {
344: my ($target) = @_;
345:
346: my $result;
347: if ($target eq 'tex') {
348: my $repetition = &Apache::response::repetition();
349: $result.='\begin{enumerate}';
350: if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
351: foreach my $i (0..$repetition-1) {
352: $result.='\item[\textbf{'.
353: ($Apache::lonxml::counter+$i).
354: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
355: }
356: $result.= '\end{enumerate}';
357: }
358:
359: return $result;
360: }
361:
362: sub exam_box {
363: my ($target) = @_;
364: my $result;
365:
366: if ($target eq 'tex') {
367: $result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
368: $result .= &exam_score_line($target);
369: } elsif ($target eq 'web') {
370: my $id=$Apache::inputtags::response[-1];
371: $result.= '<br /><br />
372: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
373: </textarea> <br /><br />';
374: }
375: return $result;
376: }
377:
378: sub needs_exam_box {
379: my ($tagstack) = @_;
380: my @tags = ('formularesponse',
381: 'stringresponse',
382: 'reactionresponse',
383: 'organicresponse',
384: );
385:
386: foreach my $tag (@tags) {
387: if (grep(/\Q$tag\E/,@$tagstack)) {
388: return 1;
389: }
390: }
391: return 0;
392: }
393:
394: sub start_textline {
395: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
396: my $result = "";
397: my $input_id = &start_input($parstack,$safeeval);
398:
399: # The spellcheck attribute
400: # 1. enables spellchecking.
401: # 2. Provides the language code in which the spellchecking will be performed.
402:
403: my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
404: if ($target eq 'web') {
405: $Apache::lonxml::evaluate--;
406: my $partid=$Apache::inputtags::part;
407: my $id=$Apache::inputtags::response[-1];
408: if (!&Apache::response::show_answer()) {
409: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
410: my $maxlength;
411: if ($size eq '') { $size=20; } else {
412: if ($size < 20) {
413: $maxlength = ' maxlength="'.$size.'"';
414: }
415: }
416: my ($oldresponse,$newvariation);
417: if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
418: ($Apache::lonhomework::type eq 'randomizetry')) &&
419: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
420: if ($env{'form.'.$partid.'.rndseed'} ne
421: $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
422: $newvariation = 1;
423: }
424: }
425: unless ($newvariation) {
426: if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
427: (($env{'form.grade_username'} eq $env{'user.name'}) &&
428: ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
429: $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
430: } elsif (($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurvey') ||
431: ($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurveycred') ||
432: ($Apache::lonhomework::type eq 'anonsurvey') ||
433: ($Apache::lonhomework::type eq 'anonsurveycred')) {
434: $oldresponse = '* '.&mt('(only shown to submitter)').' *';
435: } else {
436: $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
437: }
438: &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
439: if (ref($oldresponse) eq 'ARRAY') {
440: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
441: }
442: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
443: $oldresponse =~ s/^\s+//;
444: $oldresponse =~ s/\s+$//;
445: $oldresponse =~ s/\s+/ /g;
446: }
447: if ($Apache::lonhomework::type ne 'exam') {
448: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
449: $result='';
450: if ($addchars) {
451: $result.=&addchars('HWVAL_'.$id,$addchars);
452: }
453: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
454: $safeeval);
455: if (lc($readonly) eq 'yes'
456: || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
457: $readonly=' readonly="readonly" ';
458: } else {
459: $readonly='';
460: }
461: my $name = 'HWVAL_'.$id;
462: my $itemid = 'HWVAL_'.$partid.'_'.$id;
463: # NOTE: the input id should match the one given by defaut_homework input_id().
464: my $input_tag_id = $itemid.'_'.$input_id;
465: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
466: $name = "none";
467: }
468: $result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');"'
469: . ' onfocus="javascript:disableAutoComplete(\''.$input_tag_id.'\');"'
470: . ' type="text" '.$readonly.' name="'. $name . '"'
471: . ' id="' . $input_tag_id . '"'
472: . ' value="'. $oldresponse.'"'
473: . ' class="LC_textline spellchecked" size="'.$size.'"'.$maxlength.' />';
474:
475: $result .= &spellcheck_onblur($itemid, $spellcheck);
476: if (($Apache::inputtags::status['-1'] eq 'CAN_ANSWER') &&
477: (((($tagstack->[-2] eq 'formularesponse') || ($tagstack->[-2] eq 'mathresponse')) &&
478: (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) ||
479: (($tagstack->[-2] eq 'customresponse') &&
480: (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') eq 'no')))) {
481: $result.=&edit_mathresponse_button($input_tag_id);
482: }
483: }
484: if ($Apache::lonhomework::type eq 'exam'
485: && &needs_exam_box($tagstack)) {
486: $result.=&exam_box($target);
487: }
488: } else {
489: #right or wrong don't show what was last typed in.
490: my $count = scalar(@Apache::inputtags::inputlist)-1;
491: $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
492: #$result='';
493: }
494: } elsif ($target eq 'edit') {
495: $result=&Apache::edit::tag_start($target,$token);
496: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
497: &Apache::edit::text_arg('Click-On Texts (comma sep):',
498: 'addchars',$token,10);
499: $result.=&Apache::edit::select_arg('Readonly:','readonly',
500: ['no','yes'],$token);
501: my $spell_langs = &spelling_languages();
502: $result.=&Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
503: $spell_langs, $token);
504: $result.=&Apache::edit::end_row();
505: $result.=&Apache::edit::end_table();
506: } elsif ($target eq 'modified') {
507: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
508: $safeeval,'size',
509: 'addchars','readonly', 'spellcheck');
510: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
511: } elsif ($target eq 'tex'
512: && $Apache::lonhomework::type ne 'exam') {
513: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
514: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
515: if ($env{'form.pdfFormFields'} eq 'yes'
516: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
517: my $fieldname = $env{'request.symb'}.
518: '&part_'. $Apache::inputtags::part.
519: '&textresponse'.
520: '&HWVAL_' . $Apache::inputtags::response['-1'];
521: $result='\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
522: } else {
523: $result='\framebox['.$size.'][s]{\tiny\strut}';
524: }
525: } elsif ($target eq 'tex'
526: && $Apache::lonhomework::type eq 'exam'
527: && &needs_exam_box($tagstack)) {
528: $result.=&exam_box($target);
529: }
530: return $result;
531: }
532:
533: sub end_textline {
534: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
535: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
536: elsif ($target eq 'edit') { return ('','no'); }
537: &end_input();
538: return "";
539: }
540:
541: sub start_hiddenline {
542: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
543: my $result = "";
544: my $input_id = &start_input($parstack,$safeeval);
545: if ($target eq 'web') {
546: $Apache::lonxml::evaluate--;
547: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
548: my $partid=$Apache::inputtags::part;
549: my $id=$Apache::inputtags::response[-1];
550: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
551: if (ref($oldresponse) eq 'ARRAY') {
552: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
553: }
554: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
555:
556: if ($Apache::lonhomework::type ne 'exam') {
557: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
558: $oldresponse.'" />';
559: }
560: }
561: } elsif ($target eq 'edit') {
562: $result=&Apache::edit::tag_start($target,$token);
563: $result.=&Apache::edit::end_table;
564: }
565:
566: if ( ($target eq 'web' || $target eq 'tex')
567: && $Apache::lonhomework::type eq 'exam'
568: && &needs_exam_box($tagstack)) {
569: $result.=&exam_box($target);
570: }
571: return $result;
572: }
573:
574: sub end_hiddenline {
575: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
576: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
577: elsif ($target eq 'edit') { return ('','no'); }
578: &end_input();
579: return "";
580: }
581:
582:
583: sub start_hiddensubmission {
584: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
585: my $result = "";
586: my $input_id = &start_input($parstack,$safeeval);
587: if ($target eq 'web') {
588: $Apache::lonxml::evaluate--;
589: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
590: my $partid=$Apache::inputtags::part;
591: my $id=$Apache::inputtags::response[-1];
592: if ($Apache::lonhomework::type ne 'exam') {
593: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
594: $value = &HTML::Entities::encode($value,'<>&"');
595: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.$value.'" />';
596: }
597: }
598: } elsif ($target eq 'edit') {
599: $result=&Apache::edit::tag_start($target,$token);
600: $result.=&Apache::edit::text_arg('Value:','value',$token,'15');
601: $result.=&Apache::edit::end_row();
602: $result.=&Apache::edit::end_table();
603: } elsif ($target eq 'modified') {
604: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
605: $safeeval,'value');
606: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
607: }
608:
609: if ( ($target eq 'web' || $target eq 'tex')
610: && $Apache::lonhomework::type eq 'exam'
611: && &needs_exam_box($tagstack)) {
612: $result.=&exam_box($target);
613: }
614: return $result;
615: }
616:
617: sub end_hiddensubmission {
618: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
619: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
620: elsif ($target eq 'edit') { return ('','no'); }
621: &end_input();
622: return "";
623: }
624:
625: =pod
626:
627: =item file_selector()
628:
629: $part -> partid
630: $id -> responseid
631: $uploadefiletypes -> comma seperated list of extensions allowed or * for any
632: $which -> 'uploadonly' -> only newly uploaded files
633: 'portfolioonly' -> only allow files from portfolio
634: 'both' -> allow files from either location
635: $extratext -> additional text to go between the link and the input box
636: $maxfilesize -> maximum cumulative filesize for submitted files (in MB).
637: returns a table row <tr>
638:
639: =cut
640:
641: sub file_selector {
642: my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_;
643: if (!$uploadedfiletypes) { return ''; }
644:
645: my $jspart=$part;
646: $jspart=~s/\./_/g;
647:
648: my $result;
649: my $current_files_display = ¤t_file_submissions($part,$id);
650: my $addfiles;
651: if ($current_files_display) {
652: $result .= &Apache::lonhtmlcommon::row_title(&mt('Files currently selected for submission')).
653: $current_files_display.
654: &Apache::lonhtmlcommon::row_closure();
655: $addfiles = &mt('Submit other file(s)');
656: } else {
657: $addfiles = &mt('Choose file(s) to submit');
658: }
659: $result .= &Apache::lonhtmlcommon::row_title($addfiles);
660: my $constraints;
661: if ($uploadedfiletypes ne '*') {
662: my $showtypes = $uploadedfiletypes;
663: if ($showtypes =~ /,/) {
664: my $conjunc = &mt('or');
665: $showtypes =~ s/,([^,]+)$/ $conjunc $1/;
666: }
667: $constraints =
668: &mt('Allowed filetypes: [_1]','<b>'.$showtypes.'</b>').'<br />';
669: }
670: if ($maxfilesize) {
671: $constraints .= &mt('Combined size of all files not to exceed: [_1] MB.',
672: '<b>'.$maxfilesize.'</b>').'<br />';
673: }
674: if ($constraints) {
675: $result .= $constraints.'<br />';
676: }
677: if ($which eq 'uploadonly' || $which eq 'both') {
678: my $free_space = $maxfilesize * 1048576;
679: $result .= &mt('Submit a file: (only one file per submission)').
680: ' <br /><input type="file" size="50" name="HWFILE'.$jspart.'_'.$id.
681: '" id="HWFILE'.$jspart.'_'.$id.'" class="LC_flUpload LC_hwkfile" />'.
682: '<input type="hidden" id="LC_free_space_'.$jspart.'_'.$id.'"'.
683: ' value="'.$free_space.'" /><br />';
684: }
685: if ( $which eq 'both') {
686: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
687: }
688: if ($which eq 'portfolioonly' || $which eq 'both') {
689: my $symb = $env{'request.symb'};
690: (undef,undef,my $res)=&Apache::lonnet::decode_symb($symb);
691: my $showsymb;
692: # If resource is a .task and URL is unencrypted, include symb in query string
693: # for url opened in portfolio file selection window. Can be used to override
694: # blocking of portfolio access resulting from an exam event in a different course.
695: if ($res =~ /\.task$/i) {
696: my $encsymb = &Apache::lonenc::check_encrypt($symb);
697: if ($symb eq $encsymb) {
698: $showsymb = $symb;
699: }
700: }
701: $result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'&symb='.$showsymb.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
702: &mt('Select Portfolio Files: (one or more files per submission)').'</a><br />'.
703: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
704: '<br />';
705: }
706: $result.=&Apache::lonhtmlcommon::row_closure(1);
707: return $result;
708: }
709:
710: sub current_file_submissions {
711: my ($part,$id) = @_;
712: my $jspart=$part;
713: $jspart=~s/\./_/g;
714: my $uploadedfile=$Apache::lonhomework::history{"resource.$part.$id.uploadedfile"};
715: my $portfiles=$Apache::lonhomework::history{"resource.$part.$id.portfiles"};
716: return if (($uploadedfile eq '') && ($portfiles !~/[^\s]/));
717: my @unversioned;
718: foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) {
719: my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
720: my ($origname,$version,$ext) = &Apache::lonnet::file_name_version_ext($name);
721: unless ($version) {
722: push(@unversioned,$file);
723: }
724: }
725: return if (!@unversioned);
726: my $header = &portpath_popup_js().
727: &Apache::loncommon::start_data_table().
728: &Apache::loncommon::start_data_table_header_row();
729: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
730: $header .= '<th>'.&mt('Delete?').'</th>';
731: }
732: $header .= '<th>'.&mt('File').'</th>'.
733: '<th>'.&mt('Size (MB)').'</th>'.
734: '<th>'.&mt('Last Modified').'</th>'.
735: &Apache::loncommon::end_data_table_header_row();
736: my ($symb,$crsid,$udom,$uname)=&Apache::lonnet::whichuser();
737: my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/);
738: my ($result,$header_shown,%okfiles,%rows,%legacy,@bad_file_list);
739: if ($uploadedfile) {
740: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
741: my $link = &HTML::Entities::encode($url,'<>&"');
742: my ($path,$name) = ($url =~ m{^(/uploaded/\Q$udom\E/\Q$uname\E/essayresponse.*/)([^/]+)$});
743: my ($status,$hashref,$error) =
744: ¤t_file_info($url,$link,$name,$path);
745: if ($status eq 'ok') {
746: push(@{$okfiles{$name}},$url);
747: $rows{$url} = $hashref;
748: $legacy{$url} = 1;
749: &Apache::lonxml::extlink($url);
750: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
751: } else {
752: push(@bad_file_list,$error);
753: }
754: }
755: if (@unversioned > 0) {
756: my $prefix = "/uploaded/$udom/$uname/portfolio";
757: foreach my $file (@unversioned) {
758: my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
759: my $url = $prefix.$path.$name;
760: my $uploadedfile = &HTML::Entities::encode($url,'<>&"');
761: my ($status,$hashref,$error) =
762: ¤t_file_info($url,$uploadedfile,$name,$path);
763: if ($status eq 'ok') {
764: push(@{$okfiles{$name}},$url);
765: $rows{$url} = $hashref;
766: } else {
767: push(@bad_file_list,$error);
768: }
769: }
770: }
771: my $num = 0;
772: foreach my $name (sort(keys(%okfiles))) {
773: if (ref($okfiles{$name}) eq 'ARRAY') {
774: foreach my $url (@{$okfiles{$name}}) {
775: if (ref($rows{$url}) eq 'HASH') {
776: my $link = $rows{$url}{link};
777: my $portfile = $rows{$url}{path}.$rows{$url}{name};
778: $portfile = &HTML::Entities::encode($portfile,'<>&"');
779: if ($link) {
780: my $icon=&Apache::loncommon::icon($url);
781: unless ($header_shown) {
782: $result .= $header;
783: $header_shown = 1;
784: }
785: $result.=
786: &Apache::loncommon::start_data_table_row()."\n";
787: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
788: $result .=
789: '<td valign="bottom"><input type="checkbox" name="HWFILE'.$jspart.'_'.$id.'_delete"'.
790: ' value="'.$portfile.'" id="HWFILE'.$jspart.'_'.$id.'_'.$num.'_delete" /></td>'."\n";
791: $num ++;
792: }
793: my $pathid = 'HWFILE'.$jspart.'_'.$id.'_'.$num.'_path';
794: my $pathidtext = $pathid.'text';
795: my ($showname,$showpath);
796: if ($legacy{$url}) {
797: $showname = $name.' '.&mt('not in portfolio');
798: } else {
799: $showname = $name;
800: $showpath = '<br />'.
801: '<span id="'.$pathidtext.'" class="LC_cusr_subheading">'.
802: '<a href="javascript:showPortPath('."'$pathid','$pathidtext'".');" '.
803: 'class="LC_menubuttons_link">'.
804: &mt('(Show path)').'</a></span>'.
805: '<div id="'.$pathid.'" class="LC_dccid">'.$rows{$url}{path}.$name.
806: '</div>';
807: }
808: $result .=
809: '<td><a href="'.$link.'"><img src="'.$icon.
810: '" border="0" alt="" />'.$showname.'</a>'.$showpath.'</td>'."\n".
811: '<td align="right" valign="bottom">'.$rows{$url}{size}.'</td>'."\n".
812: '<td align="right" valign="bottom">'.$rows{$url}{lastmodified}.'</td>'."\n".
813: &Apache::loncommon::end_data_table_row();
814: }
815: }
816: }
817: }
818: }
819: if ($header_shown) {
820: $result .= &Apache::loncommon::end_data_table();
821: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
822: $result .= '<br /><span class="LC_warning">'.
823: &mt('Exclude existing file(s) from grading by checking the "Delete?" checkbox(es) and clicking "Submit Answer"').'</span>';
824: }
825: }
826: if (@bad_file_list) {
827: my $bad_files = '<span class="LC_filename">'.
828: join('</span>, <span class="LC_filename">',@bad_file_list).
829: '</span>';
830: $result.='<p class="LC_error">'.
831: &mt("These file(s) don't exist: [_1]",$bad_files).
832: '</p>';
833: }
834: return $result;
835: }
836:
837: sub current_file_info {
838: my ($url,$uploadedfile,$name,$path) = @_;
839: my ($status,$error,%info);
840: my @stat = &Apache::lonnet::stat_file($url);
841: if ((@stat) && ($stat[0] ne 'no_such_dir')) {
842: my ($lastmod,$size);
843: if ($stat[9] =~ /^\d+$/) {
844: $lastmod = &Apache::lonlocal::locallocaltime($stat[9]);
845: }
846: $size = $stat[7]/(1024*1024);
847: $size = sprintf("%.3f",$size);
848: %info = (
849: link => $uploadedfile,
850: name => $name,
851: path => $path,
852: size => $size,
853: lastmodified => $lastmod,
854: );
855: $status = 'ok';
856: } else {
857: &Apache::lonnet::logthis("bad file is $url");
858: my $icon=&Apache::loncommon::icon($url);
859: $error = '<a href="'.$url.'"><img src="'.$icon.
860: '" border="0" />'.$uploadedfile.'</a>';
861: }
862: return ($status,\%info,$error);
863: }
864:
865: sub portpath_popup_js {
866: my %lt = &Apache::lonlocal::texthash(
867: show => '(Show path)',
868: hide => '(Hide)',
869: );
870: return <<"END";
871: <script type="text/javascript">
872: // <![CDATA[
873:
874: function showPortPath(id,idtext) {
875: document.getElementById(id).style.display='block';
876: document.getElementById(id).style.textAlign='left';
877: document.getElementById(id).style.textFace='normal';
878: if (document.getElementById(idtext)) {
879: document.getElementById(idtext).innerHTML ='<a href="javascript:hidePortPath(\\''+id+'\\',\\''+idtext+'\\'); '+
880: '"class="LC_menubuttons_link">$lt{'hide'}</a> ';
881: }
882: return;
883: }
884:
885: function hidePortPath(id,idtext) {
886: if (document.getElementById(id)) {
887: document.getElementById(id).style.display='none';
888: }
889: if (document.getElementById(idtext)) {
890: document.getElementById(idtext).innerHTML ='<a href="javascript:showPortPath(\\''+id+'\\',\\''+idtext+'\\');" '+
891: 'class="LC_menubuttons_link">$lt{'show'}</a>';
892: }
893: return;
894: }
895:
896: // ]]>
897: </script>
898:
899: END
900: }
901:
902: sub valid_award {
903: my ($award) =@_;
904: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
905: 'NO_RESPONSE','WRONG_NUMBOXESCHECKED',
906: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
907: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
908: 'UNIT_FAIL', 'NO_UNIT',
909: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
910: 'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT',
911: 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
912: 'MISORDERED_RANK', 'INVALID_FILETYPE',
913: 'EXCESS_FILESIZE', 'FILENAME_INUSE',
914: 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
915: 'ANONYMOUS', 'ANONYMOUS_CREDIT',
916: 'ASSIGNED_SCORE', 'APPROX_ANS',
917: 'EXACT_ANS','COMMA_FAIL') {
918: if ($award eq $possibleaward) { return 1; }
919: }
920: return 0;
921: }
922:
923: {
924: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
925: 'WRONG_NUMBOXESCHECKED','TOO_LONG',
926: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
927: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
928: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA', 'NOT_FUNCTION',
929: 'WRONG_FORMAT', 'INTERNAL_ERROR',
930: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
931: 'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE',
932: 'DRAFT', 'SUBMITTED',
933: 'SUBMITTED_CREDIT', 'ANONYMOUS', 'ANONYMOUS_CREDIT',
934: 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
935: my $i=0;
936: my %fwd_awards = map { ($_,$i++) } @awards;
937: my $max=scalar(@awards);
938: @awards=reverse(@awards);
939: $i=0;
940: my %rev_awards = map { ($_,$i++) } @awards;
941:
942: sub awarddetail_to_awarded {
943: my ($awarddetail) = @_;
944: if ($awarddetail eq 'EXACT_ANS'
945: || $awarddetail eq 'APPROX_ANS') {
946: return 1;
947: }
948: return 0;
949: }
950:
951: sub hide_award {
952: my ($award) = @_;
953: if (&Apache::lonhomework::show_no_problem_status()) {
954: return 1;
955: }
956: if ($award =~
957: /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|SUBMITTED_CREDIT|ANONYMOUS|ANONYMOUS_CREDIT|ASSIGNED_SCORE|INCORRECT)/) {
958: return 1;
959: }
960: return 0;
961: }
962:
963: sub finalizeawards {
964: my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
965: my $result;
966: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
967: if ($result eq '' ) {
968: my $blankcount;
969: foreach my $award (@$awardref) {
970: if ($award eq '') {
971: $result='MISSING_ANSWER';
972: $blankcount++;
973: }
974: }
975: if ($blankcount == ($#$awardref + 1)) {
976: return ('NO_RESPONSE');
977: }
978: }
979:
980: if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; }
981:
982: if (!$final_scantron && defined($result)) { return ($result); }
983:
984: # if in scantron mode, if the award for any response is
985: # assigned score, then the part gets an assigned score
986: if ($final_scantron
987: && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
988: return ('ASSIGNED_SCORE');
989: }
990:
991: # if in scantron mode, if the award for any response is
992: # correct and there are non-correct responses,
993: # then the part gets an assigned score
994: if ($final_scantron
995: && (grep { $_ eq 'EXACT_ANS' ||
996: $_ eq 'APPROX_ANS' } (@$awardref))
997: && (grep { $_ ne 'EXACT_ANS' &&
998: $_ ne 'APPROX_ANS' } (@$awardref))) {
999: return ('ASSIGNED_SCORE');
1000: }
1001: # these awards are ordered from most important error through best correct
1002: my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
1003:
1004: my $best = $max;
1005: my $j=0;
1006: my $which;
1007: foreach my $award (@$awardref) {
1008: if ($awards->{$award} < $best) {
1009: $best = $awards->{$award};
1010: $which = $j;
1011: }
1012: $j++;
1013: }
1014:
1015: # if at least one response item is set to include lenient grading
1016: # and that item is partially correct then overall award reflects
1017: # that, unless an award for one of the other response items does
1018: # not fall within the basic awards for correct or incorrect.
1019: if ($Apache::inputtags::leniency) {
1020: if (($$awardref[$which] eq 'INCORRECT')
1021: && (grep { $_ eq 'EXACT_ANS' ||
1022: $_ eq 'APPROX_ANS' ||
1023: $_ eq 'ASSIGNED_SCORE' } (@$awardref))
1024: && !((grep { $_ ne 'INCORRECT' &&
1025: $_ ne 'EXACT_ANS' &&
1026: $_ ne 'APPROX_ANS' &&
1027: $_ ne 'ASSIGNED_SCORE' } (@$awardref)))) {
1028: return ('ASSIGNED_SCORE');
1029: }
1030: }
1031:
1032: if (defined($which)) {
1033: if (ref($nameref)) {
1034: return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
1035: } else {
1036: return ($$awardref[$which],$$msgref[$which]);
1037: }
1038: }
1039: return ('ERROR',undef);
1040: }
1041: }
1042:
1043: sub decideoutput {
1044: my ($award,$awarded,$awardmsg,$solved,$previous,$target,$nocorrect,$tdclass)=@_;
1045:
1046: my $message='';
1047: my $button=0;
1048: my $previousmsg;
1049: my $css_class='orange';
1050: my $added_computer_text=0;
1051: my %possible_class =
1052: ( 'correct' => 'LC_answer_correct',
1053: 'charged_try' => 'LC_answer_charged_try',
1054: 'not_charged_try' => 'LC_answer_not_charged_try',
1055: 'no_grade' => 'LC_answer_no_grade',
1056: 'no_message' => 'LC_no_message',
1057: 'no_charge_warn' => 'LC_answer_warning',
1058: );
1059:
1060: my $part = $Apache::inputtags::part;
1061: my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
1062: my $handgrade = ('yes' eq lc($tohandgrade));
1063: #
1064: # Should "Computer's Answer" be displayed?
1065: # Should not be displayed if still answerable,
1066: # if the problem is handgraded,
1067: # or if the problem does not give a correct answer
1068: #
1069:
1070: my $computer = ($handgrade || $nocorrect)? ''
1071: : &mt("Computer's answer now shown above.");
1072: &Apache::lonxml::debug("handgrade has :$handgrade:");
1073:
1074: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
1075:
1076: if ($solved =~ /^correct/) {
1077: $css_class=$possible_class{'correct'};
1078: $message=&mt('You are correct.');
1079: if ($awarded < 1 && $awarded > 0) {
1080: $message=&mt('You are partially correct.');
1081: $css_class=$possible_class{'not_charged_try'};
1082: } elsif ($awarded < 1) {
1083: $message=&mt('Incorrect.');
1084: $css_class=$possible_class{'charged_try'};
1085: }
1086: if ($handgrade ||
1087: ($env{'request.filename'}=~/\/res\/lib\/templates\/(examupload|DropBox).problem$/)) {
1088: $message = &mt("A score has been assigned.");
1089: $added_computer_text=1;
1090: } else {
1091: if ($target eq 'tex') {
1092: $message = '\textbf{'.$message.'}';
1093: } else {
1094: $message = "<b>".$message."</b>";
1095: if ($computer) {
1096: $message = "$computer $message";
1097: }
1098: }
1099: $added_computer_text=1;
1100: if ($awarded > 0) {
1101: my ($symb) = &Apache::lonnet::whichuser();
1102: if (($symb ne '')
1103: &&
1104: ($env{'course.'.$env{'request.course.id'}.
1105: '.disable_receipt_display'} ne 'yes') &&
1106: ($Apache::lonhomework::type ne 'practice')) {
1107: $message.=(($target eq 'web')?'<br />':' ').
1108: &mt('Your receipt no. is [_1]',
1109: (&Apache::lonnet::receipt($Apache::inputtags::part).
1110: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1111: }
1112: }
1113: }
1114: if ($awarded >= 1) {
1115: $button=0;
1116: } elsif (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/i) {
1117: $button=0;
1118: } else {
1119: $button=1;
1120: }
1121: $previousmsg='';
1122: } elsif ($solved =~ /^excused/) {
1123: if ($target eq 'tex') {
1124: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
1125: } else {
1126: $message = "<b>".&mt('You are excused from the problem.')."</b>";
1127: }
1128: $css_class=$possible_class{'charged_try'};
1129: $button=0;
1130: $previousmsg='';
1131: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
1132: if ($solved =~ /^incorrect/ || $solved eq '') {
1133: $message = &mt("Incorrect").".";
1134: $css_class=$possible_class{'charged_try'};
1135: $button=1;
1136: } else {
1137: if ($target eq 'tex') {
1138: $message = '\textbf{'.&mt('You are correct.').'}';
1139: } else {
1140: $message = "<b>".&mt('You are correct.')."</b>";
1141: if ($computer) {
1142: $message = "$computer $message";
1143: }
1144: }
1145: $added_computer_text=1;
1146: if ($awarded > 0
1147: && $env{'course.'.
1148: $env{'request.course.id'}.
1149: '.disable_receipt_display'} ne 'yes') {
1150: $message.=(($target eq 'web')?'<br />':' ').
1151: &mt('Your receipt is [_1]',
1152: (&Apache::lonnet::receipt($Apache::inputtags::part).
1153: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1154: }
1155: $css_class=$possible_class{'correct'};
1156: $button=0;
1157: $previousmsg='';
1158: }
1159: } elsif ($award eq 'NO_RESPONSE') {
1160: $message = '';
1161: $css_class=$possible_class{'no_feedback'};
1162: $button=1;
1163: } elsif ($award eq 'EXTRA_ANSWER') {
1164: $message = &mt('Some extra items were submitted.');
1165: $css_class=$possible_class{'not_charged_try'};
1166: $button = 1;
1167: } elsif ($award eq 'MISSING_ANSWER') {
1168: $message = &mt('Some items were not submitted.');
1169: if ($target ne 'tex') {
1170: $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
1171: }
1172: if (&Apache::lonhomework::show_some_problem_status()) {
1173: $css_class=$possible_class{'no_charge_warn'};
1174: } else {
1175: $css_class=$possible_class{'not_charged_try'};
1176: }
1177: $button = 1;
1178: } elsif ($award eq 'WRONG_NUMBOXESCHECKED') {
1179: $message = &mt('Number of boxes checked outside permissible range (either too few or too many).');
1180: if ($target ne 'tex') {
1181: $message .= &Apache::loncommon::help_open_topic('Wrong_Num_Boxes_Checked');
1182: }
1183: $css_class=$possible_class{'not_charged_try'};
1184: $button = 1;
1185: } elsif ($award eq 'ERROR') {
1186: $message = &mt('An error occurred while grading your answer.');
1187: $css_class=$possible_class{'not_charged_try'};
1188: $button = 1;
1189: } elsif ($award eq 'TOO_LONG') {
1190: $message = &mt("The submitted answer was too long.");
1191: $css_class=$possible_class{'not_charged_try'};
1192: $button=1;
1193: } elsif ($award eq 'WANTED_NUMERIC') {
1194: $message = &mt("This question expects a numeric answer.");
1195: $css_class=$possible_class{'not_charged_try'};
1196: $button=1;
1197: } elsif ($award eq 'MISORDERED_RANK') {
1198: $message = &mt('You have provided an invalid ranking.');
1199: if ($target ne 'tex') {
1200: $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
1201: }
1202: $css_class=$possible_class{'not_charged_try'};
1203: $button=1;
1204: } elsif ($award eq 'EXCESS_FILESIZE') {
1205: $message = &mt("Submission won't be graded. The combined size of submitted files exceeded the amount allowed.");
1206: $css_class=$possible_class{'not_charged_try'};
1207: $button=1;
1208: } elsif ($award eq 'FILENAME_INUSE') {
1209: $message = &mt('You have already uploaded a file with that filename.');
1210: if ($target eq 'tex') {
1211: $message.= "\\\\\n";
1212: } else {
1213: $message .= '<br />';
1214: }
1215: $message .= &mt('Please use a different filename.');
1216: $css_class=$possible_class{'not_charged_try'};
1217: $button=1;
1218: } elsif ($award eq 'INVALID_FILETYPE') {
1219: $message = &mt("Submission won't be graded. The type of file submitted is not allowed.");
1220: $css_class=$possible_class{'not_charged_try'};
1221: $button=1;
1222: } elsif ($award eq 'SIG_FAIL') {
1223: my ($used,$min,$max)=split(':',$awardmsg);
1224: my $word = ($used < $min) ? 'more' : 'fewer';
1225: $message = &mt("Submission not graded. Use $word significant figures.");
1226: if (&Apache::lonhomework::show_some_problem_status()) {
1227: $css_class=$possible_class{'no_charge_warn'};
1228: } else {
1229: $css_class=$possible_class{'not_charged_try'};
1230: }
1231: $button=1;
1232: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
1233: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
1234: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1235: $css_class=$possible_class{'not_charged_try'};
1236: $button=1;
1237: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1238: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1239: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1240: if (&Apache::lonhomework::show_some_problem_status()) {
1241: $css_class=$possible_class{'no_charge_warn'};
1242: } else {
1243: $css_class=$possible_class{'not_charged_try'};
1244: }
1245: $button=1;
1246: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1247: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1248: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1249: if (&Apache::lonhomework::show_some_problem_status()) {
1250: $css_class=$possible_class{'no_charge_warn'};
1251: } else {
1252: $css_class=$possible_class{'not_charged_try'};
1253: }
1254: $button=1;
1255: } elsif ($award eq 'UNIT_NOTNEEDED') {
1256: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1257: if (&Apache::lonhomework::show_some_problem_status()) {
1258: $css_class=$possible_class{'no_charge_warn'};
1259: } else {
1260: $css_class=$possible_class{'not_charged_try'};
1261: }
1262: $button=1;
1263: } elsif ($award eq 'NO_UNIT') {
1264: $message = &mt("Units required").'.';
1265: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1266: if (&Apache::lonhomework::show_some_problem_status()) {
1267: $css_class=$possible_class{'no_charge_warn'};
1268: } else {
1269: $css_class=$possible_class{'not_charged_try'};
1270: }
1271: $button=1;
1272: } elsif ($award eq 'COMMA_FAIL') {
1273: $message = &mt("Proper comma separation is required").'.';
1274: $css_class=$possible_class{'not_charged_try'};
1275: $button=1;
1276: } elsif ($award eq 'BAD_FORMULA') {
1277: $message = &mt("Unable to understand formula").'.';
1278: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1279: $css_class=$possible_class{'not_charged_try'};
1280: $button=1;
1281: } elsif ($award eq 'NOT_FUNCTION') {
1282: $message = &mt("Not a function").'.';
1283: $css_class=$possible_class{'not_charged_try'};
1284: $button=1;
1285: } elsif ($award eq 'WRONG_FORMAT') {
1286: $message = &mt("Wrong format").'.';
1287: $css_class=$possible_class{'not_charged_try'};
1288: $button=1;
1289: } elsif ($award eq 'INTERNAL_ERROR') {
1290: $message = &mt("An internal error occurred while processing your answer. Please try again later.");
1291: $css_class=$possible_class{'not_charged_try'};
1292: $button=1;
1293: } elsif ($award eq 'INCORRECT') {
1294: $message = &mt("Incorrect").'.';
1295: $css_class=$possible_class{'charged_try'};
1296: $button=1;
1297: } elsif ($award eq 'SUBMITTED') {
1298: $message = &mt("Your submission has been recorded.");
1299: $css_class=$possible_class{'no_grade'};
1300: $button=1;
1301: } elsif ($award eq 'SUBMITTED_CREDIT') {
1302: $message = &mt("Your submission has been recorded, and credit awarded.");
1303: $css_class=$possible_class{'correct'};
1304: $button=1;
1305: } elsif ($award eq 'ANONYMOUS') {
1306: $message = &mt("Your anonymous submission has been recorded.");
1307: $css_class=$possible_class{'no_grade'};
1308: $button=1;
1309: } elsif ($award eq 'ANONYMOUS_CREDIT') {
1310: $message = &mt("Your anonymous submission has been recorded, and credit awarded.");
1311: $css_class=$possible_class{'correct'};
1312: $button=1;
1313: } elsif ($award eq 'DRAFT') {
1314: $message = &mt("Copy saved but not submitted.");
1315: $css_class=$possible_class{'not_charged_try'};
1316: $button=1;
1317: } elsif ($award eq 'ASSIGNED_SCORE') {
1318: $message = &mt("A score has been assigned.");
1319: $css_class=$possible_class{'correct'};
1320: $button=0;
1321: } elsif ($award eq '') {
1322: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
1323: $message = &mt("Nothing submitted.");
1324: $css_class=$possible_class{'charged_try'};
1325: } else {
1326: $css_class=$possible_class{'not_charged_try'};
1327: }
1328: $button=1;
1329: } else {
1330: $message = &mt("Unknown message").": $award";
1331: $button=1;
1332: }
1333: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1334: foreach my $resid(@Apache::inputtags::response){
1335: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1336: if ($target eq 'tex') {
1337: $message.= "\\\\\n";
1338: } else {
1339: $message.='<br />';
1340: }
1341: my @files = split(/\s*,\s*/,
1342: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
1343: my $file_msg;
1344: foreach my $file (@files) {
1345: if ($target eq 'tex') {
1346: $file_msg.= "\\\\\n".$file;
1347: } else {
1348: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
1349: }
1350: }
1351: $message .= &mt('Returned file(s): [_1]',$file_msg);
1352: if ($target eq 'tex') {
1353: $message.= "\\\\\n";
1354: } else {
1355: $message.='<br />';
1356: }
1357: }
1358: }
1359:
1360: if (&Apache::lonhomework::hide_problem_status()
1361: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
1362: && &hide_award($award)) {
1363: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
1364: my @interval= &Apache::lonnet::EXT("resource.$part.interval");
1365: if ($interval[0] =~ /\d+/) {
1366: my $first_access=&Apache::lonnet::get_first_access($interval[1]);
1367: if (defined($first_access)) {
1368: my $due_date= &Apache::lonnet::EXT("resource.$part.duedate");
1369: my ($timelimit) = ($interval[0] =~ /^(\d+)/);
1370: unless (($due_date) && ($due_date < $first_access + $timelimit)) {
1371: $message = &mt("Answer Submitted: Your final submission will be graded when the time limit is reached.");
1372: }
1373: }
1374: }
1375: $css_class=$possible_class{'no_grade'};
1376: $button=1;
1377: if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
1378: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
1379: $message = 'Answer Submitted';
1380: } else {
1381: undef($message);
1382: }
1383: }
1384: }
1385: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1386: !$added_computer_text && $target ne 'tex') {
1387: if ($computer) {
1388: $message = "$computer $message";
1389: }
1390: $added_computer_text=1;
1391: }
1392: if ($Apache::lonhomework::type eq 'practice') {
1393: if ($target eq 'web') {
1394: $message .= '<br />';
1395: } else {
1396: $message .= ' ';
1397: }
1398: $message.=&mt('Submissions to practice problems are not permanently recorded.');
1399: }
1400: return ($button,$css_class,$message,$previousmsg);
1401: }
1402:
1403: sub markup_unit {
1404: my ($unit,$target)=@_;
1405: if ($target eq 'tex') {
1406: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
1407: } else {
1408: return "<tt>".$unit."</tt>";
1409: }
1410: }
1411:
1412: sub removealldata {
1413: my ($id)=@_;
1414: foreach my $key (keys(%Apache::lonhomework::results)) {
1415: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
1416: &Apache::lonxml::debug("Removing $key");
1417: delete($Apache::lonhomework::results{$key});
1418: }
1419: }
1420: }
1421:
1422: sub hidealldata {
1423: my ($id)=@_;
1424: foreach my $key (keys(%Apache::lonhomework::results)) {
1425: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
1426: &Apache::lonxml::debug("Hidding $key");
1427: my $newkey=$key;
1428: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
1429: $Apache::lonhomework::results{$newkey}=
1430: $Apache::lonhomework::results{$key};
1431: delete($Apache::lonhomework::results{$key});
1432: }
1433: }
1434: }
1435:
1436: sub setgradedata {
1437: my ($award,$msg,$id,$previously_used) = @_;
1438: if ($Apache::lonhomework::scantronmode &&
1439: &Apache::lonnet::validCODE($env{'form.CODE'})) {
1440: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1441: } elsif ($Apache::lonhomework::scantronmode &&
1442: $env{'form.CODE'} eq '' &&
1443: $Apache::lonhomework::history{"resource.CODE"} ne '') {
1444: $Apache::lonhomework::results{"resource.CODE"}='';
1445: }
1446:
1447: if (!$Apache::lonhomework::scantronmode &&
1448: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1449: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
1450: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1451: return '';
1452: } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} < 1
1453: || $Apache::lonhomework::scantronmode
1454: || &Apache::lonhomework::hide_problem_status() ) {
1455: # the student doesn't already have it correct,
1456: # or we are in a mode (scantron orno problem status) where a correct
1457: # can become incorrect
1458: # handle assignment of tries and solved status
1459: my $solvemsg;
1460: if ($Apache::lonhomework::scantronmode) {
1461: $solvemsg='correct_by_scantron';
1462: } else {
1463: $solvemsg='correct_by_student';
1464: }
1465: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
1466: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
1467: }
1468: if ( $award eq 'ASSIGNED_SCORE') {
1469: $Apache::lonhomework::results{"resource.$id.tries"} =
1470: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1471: $Apache::lonhomework::results{"resource.$id.solved"} =
1472: $solvemsg;
1473: my $numawards=scalar(@Apache::inputtags::response);
1474: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1475: foreach my $res (@Apache::inputtags::response) {
1476: if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
1477: $Apache::lonhomework::results{"resource.$id.awarded"}+=
1478: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
1479: } else {
1480: $Apache::lonhomework::results{"resource.$id.awarded"}+=
1481: &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
1482: }
1483: }
1484: if ($numawards > 0) {
1485: $Apache::lonhomework::results{"resource.$id.awarded"}/=
1486: $numawards;
1487: }
1488: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
1489: $Apache::lonhomework::results{"resource.$id.tries"} =
1490: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1491: $Apache::lonhomework::results{"resource.$id.solved"} =
1492: $solvemsg;
1493: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1494: } elsif ( $award eq 'SUBMITTED_CREDIT' ) {
1495: $Apache::lonhomework::results{"resource.$id.tries"} =
1496: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1497: $Apache::lonhomework::results{"resource.$id.solved"} =
1498: 'credit_attempted';
1499: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1500: } elsif ( $award eq 'ANONYMOUS_CREDIT' ) {
1501: $Apache::lonhomework::results{"resource.$id.tries"} =
1502: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1503: $Apache::lonhomework::results{"resource.$id.solved"} =
1504: 'credit_attempted';
1505: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1506: } elsif ( $award eq 'INCORRECT' ) {
1507: $Apache::lonhomework::results{"resource.$id.tries"} =
1508: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1509: if (&Apache::lonhomework::hide_problem_status()
1510: || $Apache::lonhomework::scantronmode) {
1511: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1512: }
1513: $Apache::lonhomework::results{"resource.$id.solved"} =
1514: 'incorrect_attempted';
1515: } elsif ( $award eq 'SUBMITTED' ) {
1516: $Apache::lonhomework::results{"resource.$id.tries"} =
1517: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1518: $Apache::lonhomework::results{"resource.$id.solved"} =
1519: 'ungraded_attempted';
1520: } elsif ( $award eq 'ANONYMOUS' ) {
1521: $Apache::lonhomework::results{"resource.$id.tries"} =
1522: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1523: $Apache::lonhomework::results{"resource.$id.solved"} =
1524: 'ungraded_attempted';
1525: } elsif ( $award eq 'DRAFT' ) {
1526: $Apache::lonhomework::results{"resource.$id.solved"} = '';
1527: } elsif ( $award eq 'NO_RESPONSE' ) {
1528: #no real response so delete any data that got stored
1529: &removealldata($id);
1530: return '';
1531: } else {
1532: $Apache::lonhomework::results{"resource.$id.solved"} =
1533: 'incorrect_attempted';
1534: if (&Apache::lonhomework::show_no_problem_status()
1535: || $Apache::lonhomework::scantronmode) {
1536: $Apache::lonhomework::results{"resource.$id.tries"} =
1537: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1538: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1539: }
1540:
1541: if (&Apache::lonhomework::show_some_problem_status()) {
1542: # clear out the awarded if they had gotten it wrong/right
1543: # and are now in an error mode
1544: $Apache::lonhomework::results{"resource.$id.awarded"} = '';
1545: }
1546: }
1547: if (defined($msg)) {
1548: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
1549: }
1550: # did either of the overall awards chage? If so ignore the
1551: # previous check
1552: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
1553: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
1554: ($Apache::lonhomework::results{"resource.$id.solved"} eq
1555: $Apache::lonhomework::history{"resource.$id.solved"})) {
1556: # check if this was a previous submission if it was delete the
1557: # unneeded data and update the previously_used attribute
1558: if ( $previously_used eq 'PREVIOUSLY_USED') {
1559: if (&Apache::lonhomework::show_problem_status()) {
1560: delete($Apache::lonhomework::results{"resource.$id.tries"});
1561: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
1562: }
1563: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1564: #delete all data as they student didn't do anything, but save
1565: #the list of collaborators.
1566: &removealldata($id);
1567: #and since they didn't do anything we were never here
1568: return '';
1569: } else {
1570: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1571: }
1572: }
1573: } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} == 1 ) {
1574: #delete all data as they student already has it correct
1575: &removealldata($id);
1576: #and since they didn't do anything we were never here
1577: return '';
1578: }
1579: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1580: if ($award eq 'SUBMITTED') {
1581: &Apache::response::add_to_gradingqueue();
1582: }
1583: $Apache::lonhomework::results{"resource.$id.type"} = $Apache::lonhomework::type;
1584: $Apache::lonhomework::results{"resource.$id.duedate"} = &Apache::lonnet::EXT("resource.$id.duedate");
1585: $Apache::lonhomework::results{"resource.$id.hinttries"} = &Apache::lonnet::EXT("resource.$id.hinttries");
1586: $Apache::lonhomework::results{"resource.$id.version"} = &Apache::lonnet::usedversion();
1587: $Apache::lonhomework::results{"resource.$id.maxtries"} = &Apache::lonnet::EXT("resource.$id.maxtries");
1588: }
1589:
1590: sub find_which_previous {
1591: my ($version) = @_;
1592: my $part = $Apache::inputtags::part;
1593: my (@previous_version);
1594: foreach my $resp (@Apache::inputtags::response) {
1595: my $key = "$version:resource.$part.$resp.submission";
1596: my $submission = $Apache::lonhomework::history{$key};
1597: my %previous = &Apache::response::check_for_previous($submission,
1598: $part,$resp,
1599: $version);
1600: push(@previous_version,$previous{'version'});
1601: }
1602: return &previous_match(\@previous_version,
1603: scalar(@Apache::inputtags::response));
1604: }
1605:
1606: sub previous_match {
1607: my ($previous_array,$count) = @_;
1608: my $match = 0;
1609: my @matches;
1610: foreach my $versionar (@$previous_array) {
1611: foreach my $version (@$versionar) {
1612: $matches[$version]++;
1613: }
1614: }
1615: my $which=0;
1616: foreach my $elem (@matches) {
1617: if ($elem eq $count) {
1618: $match=1;
1619: last;
1620: }
1621: $which++;
1622: }
1623: return ($match,$which);
1624: }
1625:
1626: sub grade {
1627: my ($target) = @_;
1628: my $id = $Apache::inputtags::part;
1629: my $response='';
1630: if ( defined $env{'form.submitted'}) {
1631: my (@awards,@msgs);
1632: foreach $response (@Apache::inputtags::response) {
1633: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
1634: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1635: &Apache::lonxml::debug("keeping $value from $response for $id");
1636: push (@awards,$value);
1637: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
1638: &Apache::lonxml::debug("got message $value from $response for $id");
1639: push (@msgs,$value);
1640: }
1641: my ($finalaward,$msg) =
1642: &finalizeawards(\@awards,\@msgs,undef,undef,
1643: $Apache::lonhomework::scantronmode);
1644: my $previously_used;
1645: if ( $#Apache::inputtags::previous eq $#awards ) {
1646: my ($match) =
1647: &previous_match(\@Apache::inputtags::previous_version,
1648: scalar(@Apache::inputtags::response));
1649:
1650: if ($match) {
1651: $previously_used = 'PREVIOUSLY_LAST';
1652: foreach my $value (@Apache::inputtags::previous) {
1653: if ($value eq 'PREVIOUSLY_USED' ) {
1654: $previously_used = $value;
1655: last;
1656: }
1657: }
1658: }
1659: }
1660: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
1661: &setgradedata($finalaward,$msg,$id,$previously_used);
1662: }
1663: return '';
1664: }
1665:
1666: sub get_grade_messages {
1667: my ($id,$prefix,$target,$status,$nocorrect,$tdclass) = @_;
1668: # nocorrect suppresses "Computer's answer now shown above"
1669: my ($message,$latemessage,$trystr,$previousmsg);
1670: my $showbutton = 1;
1671:
1672: my $award = $Apache::lonhomework::history{"$prefix.award"};
1673: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1674: my $solved = $Apache::lonhomework::history{"$prefix.solved"};
1675: my $previous = $Apache::lonhomework::history{"$prefix.previous"};
1676: my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
1677: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1678: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1679: &Apache::lonxml::debug('Getting message');
1680: ($showbutton,my $css_class,$message,$previousmsg) =
1681: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1682: $target,(($status eq 'CAN_ANSWER') || $nocorrect),$tdclass);
1683: if ($target eq 'tex') {
1684: $message='\vskip 2 mm '.$message.' ';
1685: } else {
1686: if ($message) {
1687: $message="<td class=\"$tdclass $css_class\">$message</td>";
1688: } else {
1689: $message="<td class=\"$tdclass\"></td>";
1690: }
1691: if ($previousmsg) {
1692: $previousmsg="<td class=\"$tdclass LC_answer_previous\">$previousmsg</td>";
1693: }
1694: }
1695: }
1696: my $tries = $Apache::lonhomework::history{"$prefix.tries"};
1697: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1698: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1699: #if tries are set to negative turn off the Tries/Button and messages
1700: if (defined($maxtries) && $maxtries < 0) { return ''; }
1701: if ( $tries eq '' ) { $tries = '0'; }
1702: if ( $maxtries eq '' ) { $maxtries = '2'; }
1703: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
1704: my $tries_text= &get_tries_text();
1705: if ($showbutton) {
1706: if ($target eq 'tex') {
1707: if ($env{'request.state'} ne "construct"
1708: && $Apache::lonhomework::type ne 'exam'
1709: && $env{'form.suppress_tries'} ne 'yes') {
1710: $trystr ='{\vskip 1 mm \small '
1711: .&mt('[_1]'.$tries_text.'[_2] [_3]'
1712: ,'\textit{','}',$tries.'/'.$maxtries )
1713: .'} \vskip 2 mm';
1714: } else {
1715: $trystr = '\vskip 0 mm ';
1716: }
1717: } else {
1718: my $trial =$tries;
1719: if ($Apache::lonhomework::parsing_a_task) {
1720: } elsif($env{'request.state'} ne 'construct') {
1721: $trial.="/".&Apache::lonhtmlcommon::direct_parm_link($maxtries,$env{'request.symb'},'maxtries',$id,$target);
1722: } else {
1723: if (defined($Apache::inputtags::params{'maxtries'})) {
1724: $trial.="/".$Apache::inputtags::params{'maxtries'};
1725: }
1726: }
1727:
1728: unless (($env{'request.state'} ne "construct") &&
1729: ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') &&
1730: (!$env{'request.role.adv'})) {
1731: $trystr = '<span class="LC_nobreak">'.&mt($tries_text.' [_1]',$trial).'</span>';
1732: }
1733: $trystr = '<td class="'.$tdclass.'">'.$trystr.'</td>';
1734: }
1735: }
1736:
1737: if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
1738: #last submissions was after due date
1739: $latemessage=&mt(' The last submission was after the Due Date ');;
1740: if ($target eq 'web') {
1741: $latemessage='<td class="'.$tdclass.' LC_answer_late">'.$latemessage.'</td>';
1742: }
1743: }
1744: return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
1745: }
1746:
1747: sub gradestatus {
1748: my ($id,$target,$no_previous) = @_;
1749: my $showbutton = 1;
1750: my $message = '';
1751: my $latemessage = '';
1752: my $trystr='';
1753: my $button='';
1754: my $previousmsg='';
1755: my $tdclass='';
1756:
1757: my $status = $Apache::inputtags::status['-1'];
1758: &Apache::lonxml::debug("gradestatus has :$status:");
1759: if ( $status ne 'CLOSED'
1760: && $status ne 'UNAVAILABLE'
1761: && $status ne 'INVALID_ACCESS'
1762: && $status ne 'NEEDS_CHECKIN'
1763: && $status ne 'NOT_IN_A_SLOT'
1764: && $status ne 'RESERVABLE'
1765: && $status ne 'RESERVABLE_LATER'
1766: && $status ne 'NOTRESERVABLE'
1767: && $status ne 'NEED_DIFFERENT_IP') {
1768:
1769: if ($status eq 'SHOW_ANSWER') {
1770: $showbutton = 0;
1771: }
1772:
1773: unless (($status eq 'SHOW_ANSWER') || ($status eq 'CANNOT_ANSWER')) {
1774: if ($target ne 'tex') {
1775: $tdclass = 'LC_status_submit_'.$id;
1776: }
1777: }
1778:
1779: ($previousmsg,$latemessage,$message,$trystr) =
1780: &get_grade_messages($id,"resource.$id",$target,$status,
1781: $showbutton,$tdclass);
1782: if ($status eq 'CANNOT_ANSWER') {
1783: $showbutton = 0;
1784: }
1785: if ( $status eq 'SHOW_ANSWER') {
1786: undef($previousmsg);
1787: }
1788: if ( $showbutton ) {
1789: if ($target ne 'tex') {
1790: if ($env{'form.disable_submit'}) {
1791: $button = '<input type="submit" name="submit_'.$id.'" id="submit_'.$id.'" class="LC_hwk_submit" value="'.&mt('Submit Answer').'" disabled="disabled" /> '.
1792: '<div id="msg_submit_'.$id.'" style="display:none"></div>';
1793: } else {
1794: $button =
1795: '<input onmouseup="javascript:setSubmittedPart(\''.$id.'\');this.form.action+=\'#'.&escape($id).'\';"
1796: type="submit" name="submit_'.$id.'" id="submit_'.$id.'" class="LC_hwk_submit"
1797: value="'.&mt('Submit Answer').'" /> '.
1798: '<div id="msg_submit_'.$id.'" style="display:none">'.
1799: &mt('Processing your submission ...').'</div>';
1800: }
1801: }
1802: }
1803:
1804: }
1805: my $output= $previousmsg.$latemessage.$message.$trystr;
1806: if ($output =~ /^\s*$/) {
1807: return $button;
1808: } else {
1809: if ($target eq 'tex') {
1810: return $button.' \vskip 0 mm '.$output.' ';
1811: } else {
1812: $output =
1813: '<table><tr><td>'.$button.'</td>'.$output;
1814: if ((!$no_previous) &&
1815: (($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement') ||
1816: ($env{'request.role.adv'}))) {
1817: $output.='<td class="'.$tdclass.'">'.&previous_tries($id,$target).'</td>';
1818: }
1819: $output.= '</tr></table>';
1820: return $output;
1821: }
1822: }
1823: }
1824:
1825: sub previous_tries {
1826: my ($id,$target) = @_;
1827: my $output;
1828: my $status = $Apache::inputtags::status['-1'];
1829:
1830: my $count;
1831: my %count_lookup;
1832: my ($lastrndseed,$lasttype);
1833: my $numstamps = 0;
1834:
1835: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1836: my $prefix = $i.":resource.$id";
1837: my $is_anon;
1838: my $curr_type = $Apache::lonhomework::history{"$prefix.type"};
1839: if (defined($env{'form.grade_symb'})) {
1840: if (($curr_type eq 'anonsurvey') || ($curr_type eq 'anonsurveycred')) {
1841: $is_anon = 1;
1842: }
1843: }
1844: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1845: $count++;
1846: $count_lookup{$i} = $count;
1847: my $curr_rndseed = $Apache::lonhomework::history{"$prefix.rndseed"};
1848: my ($previousmsg,$latemessage,$message,$trystr);
1849:
1850: ($previousmsg,$latemessage,$message,$trystr) =
1851: &get_grade_messages($id,"$prefix",$target,$status);
1852:
1853: if ($previousmsg ne '') {
1854: my ($match,$which) = &find_which_previous($i);
1855: $message=$previousmsg;
1856: my $previous = $count_lookup{$which};
1857: $message =~ s{(</td>)}{ as submission \# $previous $1};
1858: } elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1859: if (!(&Apache::lonhomework::hide_problem_status()
1860: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
1861: && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
1862: ) {
1863:
1864: my $txt_correct = &mt('Correct');
1865: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1866: if ($awarded < 1 && $awarded > 0) {
1867: $txt_correct=&mt('Partially Correct');
1868: } elsif ($awarded < 1) {
1869: if ($awarded eq '') {
1870: $txt_correct='';
1871: } else {
1872: $txt_correct=&mt('Incorrect');
1873: }
1874: }
1875: $message =~ s{(<td.*?>)(.*?)(</td>)}
1876: {$1 <strong>$txt_correct</strong>. $3}s;
1877: }
1878: my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1879: if (($curr_rndseed ne '') && ($lastrndseed ne '')) {
1880: if (($curr_rndseed ne $lastrndseed) &&
1881: (($curr_type eq 'randomizetry') || ($lasttype eq 'randomizetry'))) {
1882: $trystr .= '<br /><span style="color: green; white-space: nowrap; font-style: italic; font-weight: bold; font-size: 80%;">'.&mt('New problem variation this try.').'</span>';
1883: }
1884: }
1885: $message =~ s{(</td>)}{ $trystr $1};
1886: }
1887: my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
1888: $message =~ s{(<td.*?>)}{<td>};
1889:
1890:
1891: $output .= '<tr class="'.$class.'">'.
1892: '<td align="center">'.$count.'</td>'.$message;
1893: if ((!$is_anon) && ($Apache::lonhomework::history{"$prefix.tries"}) &&
1894: ($Apache::lonhomework::history{"$prefix.award"} ne 'ASSIGNED_SCORE') &&
1895: ($Apache::lonhomework::history{$i.':timestamp'})) {
1896: $output .= '<td>'.&Apache::lonlocal::locallocaltime(
1897: $Apache::lonhomework::history{$i.':timestamp'}).'</td>';
1898: $numstamps ++;
1899: } else {
1900: $output .= '<td></td>';
1901: }
1902: foreach my $resid (@Apache::inputtags::response) {
1903: my $prefix = $prefix.".$resid";
1904: if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
1905: my $submission =
1906: $Apache::inputtags::submission_display{"$prefix.submission"};
1907: if (!defined($submission)) {
1908: $submission =
1909: $Apache::lonhomework::history{"$prefix.submission"};
1910: }
1911: if ($is_anon) {
1912: $output.='<td>'.&mt('(only shown to submitter)').'</td>';
1913: } else {
1914: $output.='<td>'.$submission.'</td>';
1915: }
1916: } else {
1917: $output.='<td></td>';
1918: }
1919: }
1920: $output.=&Apache::loncommon::end_data_table_row()."\n";
1921: $lastrndseed = $curr_rndseed;
1922: $lasttype = $curr_type;
1923: }
1924: return if ($output eq '');
1925: my $headers = '<tr>'.
1926: '<th>'.&mt('Submission #').'</th>'.
1927: '<th>'.&mt('Try').'</th><th>';
1928: if ($numstamps) {
1929: $headers .= &mt('When');
1930: }
1931: $headers .= '</th>';
1932: my $colspan = scalar(@Apache::inputtags::response);
1933: if ($colspan > 1) {
1934: $headers .= '<th colspan="'.$colspan.'">';
1935: } else {
1936: $headers .= '<th>';
1937: }
1938: $headers .= &mt('Submitted Answer').'</th></tr>';
1939: $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1940:
1941: my $tries_text = &get_tries_text('link');
1942: my $prefix = $env{'form.request.prefix'};
1943: $prefix =~ tr{.}{_};
1944: my $function_name = 'LONCAPA_previous_tries_'.$prefix;
1945: if (($env{'request.state'} eq 'construct') || ($id =~ /\W/)) {
1946: $function_name .= $Apache::lonxml::curdepth;
1947: } else {
1948: $function_name .= $id;
1949: }
1950: $function_name .= '_'.$Apache::lonxml::counter;
1951: my $possmathjax = 1;
1952: my $result = &Apache::loncommon::modal_adhoc_window($function_name,420,410,$output,
1953: &mt($tries_text),$possmathjax)."<br />";
1954: return $result;
1955: }
1956:
1957: sub get_tries_text {
1958: my ($context) = @_;
1959: my $tries_text;
1960: if ($context eq 'link') {
1961: $tries_text = 'Previous Tries';
1962: } else {
1963: $tries_text = 'Tries';
1964: }
1965: if ( $Apache::lonhomework::type eq 'survey' ||
1966: $Apache::lonhomework::type eq 'surveycred' ||
1967: $Apache::lonhomework::type eq 'anonsurvey' ||
1968: $Apache::lonhomework::type eq 'anonsurveycred' ||
1969: $Apache::lonhomework::parsing_a_task) {
1970: if ($context eq 'link') {
1971: $tries_text = 'Previous Submissions';
1972: } else {
1973: $tries_text = 'Submissions';
1974: }
1975: }
1976: return $tries_text;
1977: }
1978:
1979: sub spelling_languages {
1980: my %langchoices;
1981: foreach my $id (&Apache::loncommon::languageids()) {
1982: my $code = &Apache::loncommon::supportedlanguagecode($id);
1983: if ($code ne '') {
1984: $langchoices{$code} = &Apache::loncommon::plainlanguagedescription($id);
1985: }
1986: }
1987: my @spelllangs = ('none');
1988: foreach my $code ('en','de','he','es','fr','pt','tr') {
1989: push(@spelllangs,[$code,$langchoices{$code}]);
1990: }
1991: return \@spelllangs;
1992: }
1993:
1994: sub edit_mathresponse_button {
1995: my ($field) = @_;
1996: my $eqneditor = 'lcmath';
1997: if ($env{'browser.type'} eq 'safari') {
1998: if ($env{'browser.os'} eq 'mac') {
1999: my ($prefix,$version) = ($env{'browser.version'} =~ /^(\d*)(\d{3})\./);
2000: if ($env{'browser.mobile'}) {
2001: if (($version < 531) || (($prefix eq '') && ($version < 533))) {
2002: $eqneditor = '';
2003: }
2004: } elsif ($version < 533) {
2005: $eqneditor = 'dragmath';
2006: }
2007: } elsif ($env{'browser.os'} eq 'win') {
2008: if ($env{'browser.version'} < 533) {
2009: $eqneditor = 'dragmath';
2010: }
2011: }
2012: } elsif ($env{'browser.type'} eq 'explorer') {
2013: if ($env{'browser.version'} < 9) {
2014: $eqneditor = 'dragmath';
2015: }
2016: } elsif ($env{'browser.type'} eq 'mozilla') {
2017: if ($env{'browser.version'} < 5) {
2018: $eqneditor = 'dragmath';
2019: } else {
2020: if ($env{'browser.info'} =~ /^firefox\-([\d\.]+)/) {
2021: my $firefox = $1;
2022: if ($firefox < 4) {
2023: $eqneditor = 'dragmath';
2024: }
2025: }
2026: }
2027: } elsif ($env{'browser.type'} eq 'chrome') {
2028: if ($env{'browser.version'} < 5) {
2029: $eqneditor = 'dragmath';
2030: }
2031: } elsif ($env{'browser.type'} eq 'opera') {
2032: if ($env{'browser.version'} < 12) {
2033: $eqneditor = 'dragmath';
2034: }
2035: }
2036: if ($eqneditor eq 'lcmath') {
2037: if (($env{'request.course.id'}) && ($env{'request.state'} ne 'construct')) {
2038: if (exists($env{'course.'.$env{'request.course.id'}.'.uselcmath'})) {
2039: if ($env{'course.'.$env{'request.course.id'}.'.uselcmath'} eq '0') {
2040: $eqneditor = 'dragmath';
2041: }
2042: } else {
2043: my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
2044: if ($domdefs{'uselcmath'} eq '0') {
2045: $eqneditor = 'dragmath';
2046: }
2047: }
2048: } else {
2049: my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
2050: if ($domdefs{'uselcmath'} eq '0') {
2051: $eqneditor = 'dragmath';
2052: }
2053: }
2054: }
2055: if ($eqneditor eq 'dragmath') {
2056: # DragMath applet
2057: my $button=&mt('Edit Answer');
2058: # my $helplink=&Apache::loncommon::help_open_topic('Formula_Editor');
2059: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
2060: return(<<ENDFORMULABUTTON);
2061: <script type="text/javascript" language="JavaScript">
2062: function LC_mathedit_${field} (LCtextline) {
2063: thenumber = LCtextline;
2064: var thedata = '';
2065: if (document.getElementById(LCtextline)) {
2066: thedata = document.getElementById(LCtextline).value;
2067: }
2068: newwin = window.open("/adm/dragmath/MaximaPopup.html","","width=565,height=400,resizable");
2069: }
2070: </script>
2071: <a href="javascript:LC_mathedit_${field}('${field}');void(0);"><img class="stift" src="$iconpath/stift.gif" alt="$button" title="$button" /></a>
2072: ENDFORMULABUTTON
2073: } elsif ($eqneditor eq 'lcmath') {
2074: # LON-CAPA math equation editor
2075: my $mathjaxjs;
2076: unless (lc(&Apache::lontexconvert::tex_engine()) eq 'mathjax') {
2077: $mathjaxjs = <<"MATHJAX_SCRIPT";
2078: var mathjaxscript = document.createElement("script");
2079: mathjaxscript.type = "text/javascript";
2080: mathjaxscript.src = "/adm/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
2081: document.body.appendChild(mathjaxscript);
2082: MATHJAX_SCRIPT
2083: }
2084: return(<<EQ_EDITOR_SCRIPT);
2085: <script type="text/javascript">
2086: var LCmathField = document.getElementById('${field}');
2087: LCmathField.className += ' math'; // note the space
2088: LCmathField.setAttribute('data-implicit_operators', 'true');
2089: var LCMATH_started;
2090: if (typeof LCMATH_started === 'undefined') {
2091: $mathjaxjs
2092: LCMATH_started = true;
2093: var script = document.createElement("script");
2094: script.type = "text/javascript";
2095: script.src = "/adm/LC_math_editor/LC_math_editor.min.js";
2096: document.body.appendChild(script);
2097: window.addEventListener('load', function(e) {
2098: LCMATH.initEditors();
2099: }, false);
2100: }
2101: </script>
2102: EQ_EDITOR_SCRIPT
2103: }
2104: }
2105:
2106: 1;
2107: __END__
2108:
2109: =pod
2110:
2111: =back
2112:
2113: =cut
2114:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>