1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.348 2019/08/11 16:19:52 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: $result.=&edit_mathresponse_button($input_tag_id);
480: }
481: }
482: if ($Apache::lonhomework::type eq 'exam'
483: && &needs_exam_box($tagstack)) {
484: $result.=&exam_box($target);
485: }
486: } else {
487: #right or wrong don't show what was last typed in.
488: my $count = scalar(@Apache::inputtags::inputlist)-1;
489: $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
490: #$result='';
491: }
492: } elsif ($target eq 'edit') {
493: $result=&Apache::edit::tag_start($target,$token);
494: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
495: &Apache::edit::text_arg('Click-On Texts (comma sep):',
496: 'addchars',$token,10);
497: $result.=&Apache::edit::select_arg('Readonly:','readonly',
498: ['no','yes'],$token);
499: my $spell_langs = &spelling_languages();
500: $result.=&Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
501: $spell_langs, $token);
502: $result.=&Apache::edit::end_row();
503: $result.=&Apache::edit::end_table();
504: } elsif ($target eq 'modified') {
505: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
506: $safeeval,'size',
507: 'addchars','readonly', 'spellcheck');
508: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
509: } elsif ($target eq 'tex'
510: && $Apache::lonhomework::type ne 'exam') {
511: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
512: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
513: if ($env{'form.pdfFormFields'} eq 'yes'
514: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
515: my $fieldname = $env{'request.symb'}.
516: '&part_'. $Apache::inputtags::part.
517: '&textresponse'.
518: '&HWVAL_' . $Apache::inputtags::response['-1'];
519: $result='\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
520: } else {
521: $result='\framebox['.$size.'][s]{\tiny\strut}';
522: }
523: } elsif ($target eq 'tex'
524: && $Apache::lonhomework::type eq 'exam'
525: && &needs_exam_box($tagstack)) {
526: $result.=&exam_box($target);
527: }
528: return $result;
529: }
530:
531: sub end_textline {
532: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
533: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
534: elsif ($target eq 'edit') { return ('','no'); }
535: &end_input();
536: return "";
537: }
538:
539: sub start_hiddenline {
540: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
541: my $result = "";
542: my $input_id = &start_input($parstack,$safeeval);
543: if ($target eq 'web') {
544: $Apache::lonxml::evaluate--;
545: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
546: my $partid=$Apache::inputtags::part;
547: my $id=$Apache::inputtags::response[-1];
548: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
549: if (ref($oldresponse) eq 'ARRAY') {
550: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
551: }
552: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
553:
554: if ($Apache::lonhomework::type ne 'exam') {
555: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
556: $oldresponse.'" />';
557: }
558: }
559: } elsif ($target eq 'edit') {
560: $result=&Apache::edit::tag_start($target,$token);
561: $result.=&Apache::edit::end_table;
562: }
563:
564: if ( ($target eq 'web' || $target eq 'tex')
565: && $Apache::lonhomework::type eq 'exam'
566: && &needs_exam_box($tagstack)) {
567: $result.=&exam_box($target);
568: }
569: return $result;
570: }
571:
572: sub end_hiddenline {
573: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
574: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
575: elsif ($target eq 'edit') { return ('','no'); }
576: &end_input();
577: return "";
578: }
579:
580:
581: sub start_hiddensubmission {
582: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
583: my $result = "";
584: my $input_id = &start_input($parstack,$safeeval);
585: if ($target eq 'web') {
586: $Apache::lonxml::evaluate--;
587: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
588: my $partid=$Apache::inputtags::part;
589: my $id=$Apache::inputtags::response[-1];
590: if ($Apache::lonhomework::type ne 'exam') {
591: my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
592: $value = &HTML::Entities::encode($value,'<>&"');
593: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.$value.'" />';
594: }
595: }
596: } elsif ($target eq 'edit') {
597: $result=&Apache::edit::tag_start($target,$token);
598: $result.=&Apache::edit::text_arg('Value:','value',$token,'15');
599: $result.=&Apache::edit::end_row();
600: $result.=&Apache::edit::end_table();
601: } elsif ($target eq 'modified') {
602: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
603: $safeeval,'value');
604: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
605: }
606:
607: if ( ($target eq 'web' || $target eq 'tex')
608: && $Apache::lonhomework::type eq 'exam'
609: && &needs_exam_box($tagstack)) {
610: $result.=&exam_box($target);
611: }
612: return $result;
613: }
614:
615: sub end_hiddensubmission {
616: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
617: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
618: elsif ($target eq 'edit') { return ('','no'); }
619: &end_input();
620: return "";
621: }
622:
623: =pod
624:
625: =item file_selector()
626:
627: $part -> partid
628: $id -> responseid
629: $uploadefiletypes -> comma seperated list of extensions allowed or * for any
630: $which -> 'uploadonly' -> only newly uploaded files
631: 'portfolioonly' -> only allow files from portfolio
632: 'both' -> allow files from either location
633: $extratext -> additional text to go between the link and the input box
634: $maxfilesize -> maximum cumulative filesize for submitted files (in MB).
635: returns a table row <tr>
636:
637: =cut
638:
639: sub file_selector {
640: my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_;
641: if (!$uploadedfiletypes) { return ''; }
642:
643: my $jspart=$part;
644: $jspart=~s/\./_/g;
645:
646: my $result;
647: my $current_files_display = ¤t_file_submissions($part,$id);
648: my $addfiles;
649: if ($current_files_display) {
650: $result .= &Apache::lonhtmlcommon::row_title(&mt('Files currently selected for submission')).
651: $current_files_display.
652: &Apache::lonhtmlcommon::row_closure();
653: $addfiles = &mt('Submit other file(s)');
654: } else {
655: $addfiles = &mt('Choose file(s) to submit');
656: }
657: $result .= &Apache::lonhtmlcommon::row_title($addfiles);
658: my $constraints;
659: if ($uploadedfiletypes ne '*') {
660: $constraints =
661: &mt('Allowed filetypes: [_1]','<b>'.$uploadedfiletypes.'</b>').'<br />';
662: }
663: if ($maxfilesize) {
664: $constraints .= &mt('Combined size of all files not to exceed: [_1] MB.',
665: '<b>'.$maxfilesize.'</b>').'<br />';
666: }
667: if ($constraints) {
668: $result .= $constraints.'<br />';
669: }
670: if ($which eq 'uploadonly' || $which eq 'both') {
671: my $free_space = $maxfilesize * 1048576;
672: $result .= &mt('Submit a file: (only one file per submission)').
673: ' <br /><input type="file" size="50" name="HWFILE'.$jspart.'_'.$id.
674: '" id="HWFILE'.$jspart.'_'.$id.'" class="LC_flUpload LC_hwkfile" />'.
675: '<input type="hidden" id="LC_free_space_'.$jspart.'_'.$id.'"'.
676: ' value="'.$free_space.'" /><br />';
677: }
678: if ( $which eq 'both') {
679: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
680: }
681: if ($which eq 'portfolioonly' || $which eq 'both') {
682: my $symb = $env{'request.symb'};
683: (undef,undef,my $res)=&Apache::lonnet::decode_symb($symb);
684: my $showsymb;
685: # If resource is a .task and URL is unencrypted, include symb in query string
686: # for url opened in portfolio file selection window. Can be used to override
687: # blocking of portfolio access resulting from an exam event in a different course.
688: if ($res =~ /\.task$/i) {
689: my $encsymb = &Apache::lonenc::check_encrypt($symb);
690: if ($symb eq $encsymb) {
691: $showsymb = $symb;
692: }
693: }
694: $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"))'."'".'>'.
695: &mt('Select Portfolio Files: (one or more files per submission)').'</a><br />'.
696: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
697: '<br />';
698:
699: }
700: $result.=&Apache::lonhtmlcommon::row_closure(1);
701: return $result;
702: }
703:
704: sub current_file_submissions {
705: my ($part,$id) = @_;
706: my $jspart=$part;
707: $jspart=~s/\./_/g;
708: my $uploadedfile=$Apache::lonhomework::history{"resource.$part.$id.uploadedfile"};
709: my $portfiles=$Apache::lonhomework::history{"resource.$part.$id.portfiles"};
710: return if (($uploadedfile eq '') && ($portfiles !~/[^\s]/));
711: my @unversioned;
712: foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) {
713: my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
714: my ($origname,$version,$ext) = &Apache::lonnet::file_name_version_ext($name);
715: unless ($version) {
716: push(@unversioned,$file);
717: }
718: }
719: return if (!@unversioned);
720: my $header = &portpath_popup_js().
721: &Apache::loncommon::start_data_table().
722: &Apache::loncommon::start_data_table_header_row();
723: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
724: $header .= '<th>'.&mt('Delete?').'</th>';
725: }
726: $header .= '<th>'.&mt('File').'</th>'.
727: '<th>'.&mt('Size (MB)').'</th>'.
728: '<th>'.&mt('Last Modified').'</th>'.
729: &Apache::loncommon::end_data_table_header_row();
730: my ($symb,$crsid,$udom,$uname)=&Apache::lonnet::whichuser();
731: my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/);
732: my ($result,$header_shown,%okfiles,%rows,%legacy,@bad_file_list);
733: if ($uploadedfile) {
734: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
735: my $link = &HTML::Entities::encode($url,'<>&"');
736: my ($path,$name) = ($url =~ m{^(/uploaded/\Q$udom\E/\Q$uname\E/essayresponse.*/)([^/]+)$});
737: my ($status,$hashref,$error) =
738: ¤t_file_info($url,$link,$name,$path);
739: if ($status eq 'ok') {
740: push(@{$okfiles{$name}},$url);
741: $rows{$url} = $hashref;
742: $legacy{$url} = 1;
743: &Apache::lonxml::extlink($url);
744: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
745: } else {
746: push(@bad_file_list,$error);
747: }
748: }
749: if (@unversioned > 0) {
750: my $prefix = "/uploaded/$udom/$uname/portfolio";
751: foreach my $file (@unversioned) {
752: my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
753: my $url = $prefix.$path.$name;
754: my $uploadedfile = &HTML::Entities::encode($url,'<>&"');
755: my ($status,$hashref,$error) =
756: ¤t_file_info($url,$uploadedfile,$name,$path);
757: if ($status eq 'ok') {
758: push(@{$okfiles{$name}},$url);
759: $rows{$url} = $hashref;
760: } else {
761: push(@bad_file_list,$error);
762: }
763: }
764: }
765: my $num = 0;
766: foreach my $name (sort(keys(%okfiles))) {
767: if (ref($okfiles{$name}) eq 'ARRAY') {
768: foreach my $url (@{$okfiles{$name}}) {
769: if (ref($rows{$url}) eq 'HASH') {
770: my $link = $rows{$url}{link};
771: my $portfile = $rows{$url}{path}.$rows{$url}{name};
772: $portfile = &HTML::Entities::encode($portfile,'<>&"');
773: if ($link) {
774: my $icon=&Apache::loncommon::icon($url);
775: unless ($header_shown) {
776: $result .= $header;
777: $header_shown = 1;
778: }
779: $result.=
780: &Apache::loncommon::start_data_table_row()."\n";
781: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
782: $result .=
783: '<td valign="bottom"><input type="checkbox" name="HWFILE'.$jspart.'_'.$id.'_delete"'.
784: ' value="'.$portfile.'" id="HWFILE'.$jspart.'_'.$id.'_'.$num.'_delete" /></td>'."\n";
785: $num ++;
786: }
787: my $pathid = 'HWFILE'.$jspart.'_'.$id.'_'.$num.'_path';
788: my $pathidtext = $pathid.'text';
789: my ($showname,$showpath);
790: if ($legacy{$url}) {
791: $showname = $name.' '.&mt('not in portfolio');
792: } else {
793: $showname = $name;
794: $showpath = '<br />'.
795: '<span id="'.$pathidtext.'" class="LC_cusr_subheading">'.
796: '<a href="javascript:showPortPath('."'$pathid','$pathidtext'".');" '.
797: 'class="LC_menubuttons_link">'.
798: &mt('(Show path)').'</a></span>'.
799: '<div id="'.$pathid.'" class="LC_dccid">'.$rows{$url}{path}.$name.
800: '</div>';
801: }
802: $result .=
803: '<td><a href="'.$link.'"><img src="'.$icon.
804: '" border="0" alt="" />'.$showname.'</a>'.$showpath.'</td>'."\n".
805: '<td align="right" valign="bottom">'.$rows{$url}{size}.'</td>'."\n".
806: '<td align="right" valign="bottom">'.$rows{$url}{lastmodified}.'</td>'."\n".
807: &Apache::loncommon::end_data_table_row();
808: }
809: }
810: }
811: }
812: }
813: if ($header_shown) {
814: $result .= &Apache::loncommon::end_data_table();
815: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
816: $result .= '<br /><span class="LC_warning">'.
817: &mt('Exclude existing file(s) from grading by checking the "Delete?" checkbox(es) and clicking "Submit Answer"').'</span>';
818: }
819: }
820: if (@bad_file_list) {
821: my $bad_files = '<span class="LC_filename">'.
822: join('</span>, <span class="LC_filename">',@bad_file_list).
823: '</span>';
824: $result.='<p class="LC_error">'.
825: &mt("These file(s) don't exist: [_1]",$bad_files).
826: '</p>';
827: }
828: return $result;
829: }
830:
831: sub current_file_info {
832: my ($url,$uploadedfile,$name,$path) = @_;
833: my ($status,$error,%info);
834: my @stat = &Apache::lonnet::stat_file($url);
835: if ((@stat) && ($stat[0] ne 'no_such_dir')) {
836: my ($lastmod,$size);
837: if ($stat[9] =~ /^\d+$/) {
838: $lastmod = &Apache::lonlocal::locallocaltime($stat[9]);
839: }
840: $size = $stat[7]/(1024*1024);
841: $size = sprintf("%.3f",$size);
842: %info = (
843: link => $uploadedfile,
844: name => $name,
845: path => $path,
846: size => $size,
847: lastmodified => $lastmod,
848: );
849: $status = 'ok';
850: } else {
851: &Apache::lonnet::logthis("bad file is $url");
852: my $icon=&Apache::loncommon::icon($url);
853: $error = '<a href="'.$url.'"><img src="'.$icon.
854: '" border="0" />'.$uploadedfile.'</a>';
855: }
856: return ($status,\%info,$error);
857: }
858:
859: sub portpath_popup_js {
860: my %lt = &Apache::lonlocal::texthash(
861: show => '(Show path)',
862: hide => '(Hide)',
863: );
864: return <<"END";
865: <script type="text/javascript">
866: // <![CDATA[
867:
868: function showPortPath(id,idtext) {
869: document.getElementById(id).style.display='block';
870: document.getElementById(id).style.textAlign='left';
871: document.getElementById(id).style.textFace='normal';
872: if (document.getElementById(idtext)) {
873: document.getElementById(idtext).innerHTML ='<a href="javascript:hidePortPath(\\''+id+'\\',\\''+idtext+'\\'); '+
874: '"class="LC_menubuttons_link">$lt{'hide'}</a> ';
875: }
876: return;
877: }
878:
879: function hidePortPath(id,idtext) {
880: if (document.getElementById(id)) {
881: document.getElementById(id).style.display='none';
882: }
883: if (document.getElementById(idtext)) {
884: document.getElementById(idtext).innerHTML ='<a href="javascript:showPortPath(\\''+id+'\\',\\''+idtext+'\\');" '+
885: 'class="LC_menubuttons_link">$lt{'show'}</a>';
886: }
887: return;
888: }
889:
890: // ]]>
891: </script>
892:
893: END
894: }
895:
896: sub valid_award {
897: my ($award) =@_;
898: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
899: 'NO_RESPONSE','WRONG_NUMBOXESCHECKED',
900: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
901: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
902: 'UNIT_FAIL', 'NO_UNIT',
903: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
904: 'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT',
905: 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
906: 'MISORDERED_RANK', 'INVALID_FILETYPE',
907: 'EXCESS_FILESIZE', 'FILENAME_INUSE',
908: 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
909: 'ANONYMOUS', 'ANONYMOUS_CREDIT',
910: 'ASSIGNED_SCORE', 'APPROX_ANS',
911: 'EXACT_ANS','COMMA_FAIL') {
912: if ($award eq $possibleaward) { return 1; }
913: }
914: return 0;
915: }
916:
917: {
918: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
919: 'WRONG_NUMBOXESCHECKED','TOO_LONG',
920: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
921: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
922: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA', 'NOT_FUNCTION',
923: 'WRONG_FORMAT', 'INTERNAL_ERROR',
924: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
925: 'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE',
926: 'DRAFT', 'SUBMITTED',
927: 'SUBMITTED_CREDIT', 'ANONYMOUS', 'ANONYMOUS_CREDIT',
928: 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
929: my $i=0;
930: my %fwd_awards = map { ($_,$i++) } @awards;
931: my $max=scalar(@awards);
932: @awards=reverse(@awards);
933: $i=0;
934: my %rev_awards = map { ($_,$i++) } @awards;
935:
936: sub awarddetail_to_awarded {
937: my ($awarddetail) = @_;
938: if ($awarddetail eq 'EXACT_ANS'
939: || $awarddetail eq 'APPROX_ANS') {
940: return 1;
941: }
942: return 0;
943: }
944:
945: sub hide_award {
946: my ($award) = @_;
947: if (&Apache::lonhomework::show_no_problem_status()) {
948: return 1;
949: }
950: if ($award =~
951: /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|SUBMITTED_CREDIT|ANONYMOUS|ANONYMOUS_CREDIT|ASSIGNED_SCORE|INCORRECT)/) {
952: return 1;
953: }
954: return 0;
955: }
956:
957: sub finalizeawards {
958: my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
959: my $result;
960: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
961: if ($result eq '' ) {
962: my $blankcount;
963: foreach my $award (@$awardref) {
964: if ($award eq '') {
965: $result='MISSING_ANSWER';
966: $blankcount++;
967: }
968: }
969: if ($blankcount == ($#$awardref + 1)) {
970: return ('NO_RESPONSE');
971: }
972: }
973:
974: if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; }
975:
976: if (!$final_scantron && defined($result)) { return ($result); }
977:
978: # if in scantron mode, if the award for any response is
979: # assigned score, then the part gets an assigned score
980: if ($final_scantron
981: && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
982: return ('ASSIGNED_SCORE');
983: }
984:
985: # if in scantron mode, if the award for any response is
986: # correct and there are non-correct responses,
987: # then the part gets an assigned score
988: if ($final_scantron
989: && (grep { $_ eq 'EXACT_ANS' ||
990: $_ eq 'APPROX_ANS' } (@$awardref))
991: && (grep { $_ ne 'EXACT_ANS' &&
992: $_ ne 'APPROX_ANS' } (@$awardref))) {
993: return ('ASSIGNED_SCORE');
994: }
995: # these awards are ordered from most important error through best correct
996: my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
997:
998: my $best = $max;
999: my $j=0;
1000: my $which;
1001: foreach my $award (@$awardref) {
1002: if ($awards->{$award} < $best) {
1003: $best = $awards->{$award};
1004: $which = $j;
1005: }
1006: $j++;
1007: }
1008:
1009: # if at least one response item is set to include lenient grading
1010: # and that item is partially correct then overall award reflects
1011: # that, unless an award for one of the other response items does
1012: # not fall within the basic awards for correct or incorrect.
1013: if ($Apache::inputtags::leniency) {
1014: if (($$awardref[$which] eq 'INCORRECT')
1015: && (grep { $_ eq 'EXACT_ANS' ||
1016: $_ eq 'APPROX_ANS' ||
1017: $_ eq 'ASSIGNED_SCORE' } (@$awardref))
1018: && !((grep { $_ ne 'INCORRECT' &&
1019: $_ ne 'EXACT_ANS' &&
1020: $_ ne 'APPROX_ANS' &&
1021: $_ ne 'ASSIGNED_SCORE' } (@$awardref)))) {
1022: return ('ASSIGNED_SCORE');
1023: }
1024: }
1025:
1026: if (defined($which)) {
1027: if (ref($nameref)) {
1028: return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
1029: } else {
1030: return ($$awardref[$which],$$msgref[$which]);
1031: }
1032: }
1033: return ('ERROR',undef);
1034: }
1035: }
1036:
1037: sub decideoutput {
1038: my ($award,$awarded,$awardmsg,$solved,$previous,$target,$nocorrect,$tdclass)=@_;
1039:
1040: my $message='';
1041: my $button=0;
1042: my $previousmsg;
1043: my $css_class='orange';
1044: my $added_computer_text=0;
1045: my %possible_class =
1046: ( 'correct' => 'LC_answer_correct',
1047: 'charged_try' => 'LC_answer_charged_try',
1048: 'not_charged_try' => 'LC_answer_not_charged_try',
1049: 'no_grade' => 'LC_answer_no_grade',
1050: 'no_message' => 'LC_no_message',
1051: );
1052:
1053: my $part = $Apache::inputtags::part;
1054: my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
1055: my $handgrade = ('yes' eq lc($tohandgrade));
1056: #
1057: # Should "Computer's Answer" be displayed?
1058: # Should not be displayed if still answerable,
1059: # if the problem is handgraded,
1060: # or if the problem does not give a correct answer
1061: #
1062:
1063: my $computer = ($handgrade || $nocorrect)? ''
1064: : &mt("Computer's answer now shown above.");
1065: &Apache::lonxml::debug("handgrade has :$handgrade:");
1066:
1067: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
1068:
1069: if ($solved =~ /^correct/) {
1070: $css_class=$possible_class{'correct'};
1071: $message=&mt('You are correct.');
1072: if ($awarded < 1 && $awarded > 0) {
1073: $message=&mt('You are partially correct.');
1074: $css_class=$possible_class{'not_charged_try'};
1075: } elsif ($awarded < 1) {
1076: $message=&mt('Incorrect.');
1077: $css_class=$possible_class{'charged_try'};
1078: }
1079: if ($handgrade ||
1080: ($env{'request.filename'}=~/\/res\/lib\/templates\/(examupload|DropBox).problem$/)) {
1081: $message = &mt("A score has been assigned.");
1082: $added_computer_text=1;
1083: } else {
1084: if ($target eq 'tex') {
1085: $message = '\textbf{'.$message.'}';
1086: } else {
1087: $message = "<b>".$message."</b>";
1088: if ($computer) {
1089: $message = "$computer $message";
1090: }
1091: }
1092: $added_computer_text=1;
1093: if ($awarded > 0) {
1094: my ($symb) = &Apache::lonnet::whichuser();
1095: if (($symb ne '')
1096: &&
1097: ($env{'course.'.$env{'request.course.id'}.
1098: '.disable_receipt_display'} ne 'yes') &&
1099: ($Apache::lonhomework::type ne 'practice')) {
1100: $message.=(($target eq 'web')?'<br />':' ').
1101: &mt('Your receipt no. is [_1]',
1102: (&Apache::lonnet::receipt($Apache::inputtags::part).
1103: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1104: }
1105: }
1106: }
1107: if ($awarded >= 1) {
1108: $button=0;
1109: } elsif (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/i) {
1110: $button=0;
1111: } else {
1112: $button=1;
1113: }
1114: $previousmsg='';
1115: } elsif ($solved =~ /^excused/) {
1116: if ($target eq 'tex') {
1117: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
1118: } else {
1119: $message = "<b>".&mt('You are excused from the problem.')."</b>";
1120: }
1121: $css_class=$possible_class{'charged_try'};
1122: $button=0;
1123: $previousmsg='';
1124: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
1125: if ($solved =~ /^incorrect/ || $solved eq '') {
1126: $message = &mt("Incorrect").".";
1127: $css_class=$possible_class{'charged_try'};
1128: $button=1;
1129: } else {
1130: if ($target eq 'tex') {
1131: $message = '\textbf{'.&mt('You are correct.').'}';
1132: } else {
1133: $message = "<b>".&mt('You are correct.')."</b>";
1134: if ($computer) {
1135: $message = "$computer $message";
1136: }
1137: }
1138: $added_computer_text=1;
1139: if ($awarded > 0
1140: && $env{'course.'.
1141: $env{'request.course.id'}.
1142: '.disable_receipt_display'} ne 'yes') {
1143: $message.=(($target eq 'web')?'<br />':' ').
1144: &mt('Your receipt is [_1]',
1145: (&Apache::lonnet::receipt($Apache::inputtags::part).
1146: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1147: }
1148: $css_class=$possible_class{'correct'};
1149: $button=0;
1150: $previousmsg='';
1151: }
1152: } elsif ($award eq 'NO_RESPONSE') {
1153: $message = '';
1154: $css_class=$possible_class{'no_feedback'};
1155: $button=1;
1156: } elsif ($award eq 'EXTRA_ANSWER') {
1157: $message = &mt('Some extra items were submitted.');
1158: $css_class=$possible_class{'not_charged_try'};
1159: $button = 1;
1160: } elsif ($award eq 'MISSING_ANSWER') {
1161: $message = &mt('Some items were not submitted.');
1162: if ($target ne 'tex') {
1163: $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
1164: }
1165: $css_class=$possible_class{'not_charged_try'};
1166: $button = 1;
1167: } elsif ($award eq 'WRONG_NUMBOXESCHECKED') {
1168: $message = &mt('Number of boxes checked outside permissible range (either too few or too many).');
1169: if ($target ne 'tex') {
1170: $message .= &Apache::loncommon::help_open_topic('Wrong_Num_Boxes_Checked');
1171: }
1172: $css_class=$possible_class{'not_charged_try'};
1173: $button = 1;
1174: } elsif ($award eq 'ERROR') {
1175: $message = &mt('An error occurred while grading your answer.');
1176: $css_class=$possible_class{'not_charged_try'};
1177: $button = 1;
1178: } elsif ($award eq 'TOO_LONG') {
1179: $message = &mt("The submitted answer was too long.");
1180: $css_class=$possible_class{'not_charged_try'};
1181: $button=1;
1182: } elsif ($award eq 'WANTED_NUMERIC') {
1183: $message = &mt("This question expects a numeric answer.");
1184: $css_class=$possible_class{'not_charged_try'};
1185: $button=1;
1186: } elsif ($award eq 'MISORDERED_RANK') {
1187: $message = &mt('You have provided an invalid ranking.');
1188: if ($target ne 'tex') {
1189: $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
1190: }
1191: $css_class=$possible_class{'not_charged_try'};
1192: $button=1;
1193: } elsif ($award eq 'EXCESS_FILESIZE') {
1194: $message = &mt("Submission won't be graded. The combined size of submitted files exceeded the amount allowed.");
1195: $css_class=$possible_class{'not_charged_try'};
1196: $button=1;
1197: } elsif ($award eq 'FILENAME_INUSE') {
1198: $message = &mt('You have already uploaded a file with that filename.');
1199: if ($target eq 'tex') {
1200: $message.= "\\\\\n";
1201: } else {
1202: $message .= '<br />';
1203: }
1204: $message .= &mt('Please use a different filename.');
1205: $css_class=$possible_class{'not_charged_try'};
1206: $button=1;
1207: } elsif ($award eq 'INVALID_FILETYPE') {
1208: $message = &mt("Submission won't be graded. The type of file submitted is not allowed.");
1209: $css_class=$possible_class{'not_charged_try'};
1210: $button=1;
1211: } elsif ($award eq 'SIG_FAIL') {
1212: my ($used,$min,$max)=split(':',$awardmsg);
1213: my $word = ($used < $min) ? 'more' : 'fewer';
1214: $message = &mt("Submission not graded. Use $word digits.",$used);
1215: $css_class=$possible_class{'not_charged_try'};
1216: $button=1;
1217: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
1218: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
1219: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1220: $css_class=$possible_class{'not_charged_try'};
1221: $button=1;
1222: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1223: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1224: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1225: $css_class=$possible_class{'not_charged_try'};
1226: $button=1;
1227: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1228: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1229: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
1230: $css_class=$possible_class{'not_charged_try'};
1231: $button=1;
1232: } elsif ($award eq 'UNIT_NOTNEEDED') {
1233: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1234: $css_class=$possible_class{'not_charged_try'};
1235: $button=1;
1236: } elsif ($award eq 'NO_UNIT') {
1237: $message = &mt("Units required").'.';
1238: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1239: $css_class=$possible_class{'not_charged_try'};
1240: $button=1;
1241: } elsif ($award eq 'COMMA_FAIL') {
1242: $message = &mt("Proper comma separation is required").'.';
1243: $css_class=$possible_class{'not_charged_try'};
1244: $button=1;
1245: } elsif ($award eq 'BAD_FORMULA') {
1246: $message = &mt("Unable to understand formula").'.';
1247: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1248: $css_class=$possible_class{'not_charged_try'};
1249: $button=1;
1250: } elsif ($award eq 'NOT_FUNCTION') {
1251: $message = &mt("Not a function").'.';
1252: $css_class=$possible_class{'not_charged_try'};
1253: $button=1;
1254: } elsif ($award eq 'WRONG_FORMAT') {
1255: $message = &mt("Wrong format").'.';
1256: $css_class=$possible_class{'not_charged_try'};
1257: $button=1;
1258: } elsif ($award eq 'INTERNAL_ERROR') {
1259: $message = &mt("An internal error occurred while processing your answer. Please try again later.");
1260: $css_class=$possible_class{'not_charged_try'};
1261: $button=1;
1262: } elsif ($award eq 'INCORRECT') {
1263: $message = &mt("Incorrect").'.';
1264: $css_class=$possible_class{'charged_try'};
1265: $button=1;
1266: } elsif ($award eq 'SUBMITTED') {
1267: $message = &mt("Your submission has been recorded.");
1268: $css_class=$possible_class{'no_grade'};
1269: $button=1;
1270: } elsif ($award eq 'SUBMITTED_CREDIT') {
1271: $message = &mt("Your submission has been recorded, and credit awarded.");
1272: $css_class=$possible_class{'correct'};
1273: $button=1;
1274: } elsif ($award eq 'ANONYMOUS') {
1275: $message = &mt("Your anonymous submission has been recorded.");
1276: $css_class=$possible_class{'no_grade'};
1277: $button=1;
1278: } elsif ($award eq 'ANONYMOUS_CREDIT') {
1279: $message = &mt("Your anonymous submission has been recorded, and credit awarded.");
1280: $css_class=$possible_class{'correct'};
1281: $button=1;
1282: } elsif ($award eq 'DRAFT') {
1283: $message = &mt("Copy saved but not submitted.");
1284: $css_class=$possible_class{'not_charged_try'};
1285: $button=1;
1286: } elsif ($award eq 'ASSIGNED_SCORE') {
1287: $message = &mt("A score has been assigned.");
1288: $css_class=$possible_class{'correct'};
1289: $button=0;
1290: } elsif ($award eq '') {
1291: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
1292: $message = &mt("Nothing submitted.");
1293: $css_class=$possible_class{'charged_try'};
1294: } else {
1295: $css_class=$possible_class{'not_charged_try'};
1296: }
1297: $button=1;
1298: } else {
1299: $message = &mt("Unknown message").": $award";
1300: $button=1;
1301: }
1302: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1303: foreach my $resid(@Apache::inputtags::response){
1304: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1305: if ($target eq 'tex') {
1306: $message.= "\\\\\n";
1307: } else {
1308: $message.='<br />';
1309: }
1310: my @files = split(/\s*,\s*/,
1311: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
1312: my $file_msg;
1313: foreach my $file (@files) {
1314: if ($target eq 'tex') {
1315: $file_msg.= "\\\\\n".$file;
1316: } else {
1317: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
1318: }
1319: }
1320: $message .= &mt('Returned file(s): [_1]',$file_msg);
1321: if ($target eq 'tex') {
1322: $message.= "\\\\\n";
1323: } else {
1324: $message.='<br />';
1325: }
1326: }
1327: }
1328:
1329: if (&Apache::lonhomework::hide_problem_status()
1330: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
1331: && &hide_award($award)) {
1332: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
1333: my @interval= &Apache::lonnet::EXT("resource.$part.interval");
1334: if ($interval[0] =~ /\d+/) {
1335: my $first_access=&Apache::lonnet::get_first_access($interval[1]);
1336: if (defined($first_access)) {
1337: my $due_date= &Apache::lonnet::EXT("resource.$part.duedate");
1338: my ($timelimit) = ($interval[0] =~ /^(\d+)/);
1339: unless (($due_date) && ($due_date < $first_access + $timelimit)) {
1340: $message = &mt("Answer Submitted: Your final submission will be graded when the time limit is reached.");
1341: }
1342: }
1343: }
1344: $css_class=$possible_class{'no_grade'};
1345: $button=1;
1346: if ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') {
1347: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
1348: $message = 'Answer Submitted';
1349: } else {
1350: undef($message);
1351: }
1352: }
1353: }
1354: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1355: !$added_computer_text && $target ne 'tex') {
1356: if ($computer) {
1357: $message = "$computer $message";
1358: }
1359: $added_computer_text=1;
1360: }
1361: if ($Apache::lonhomework::type eq 'practice') {
1362: if ($target eq 'web') {
1363: $message .= '<br />';
1364: } else {
1365: $message .= ' ';
1366: }
1367: $message.=&mt('Submissions to practice problems are not permanently recorded.');
1368: }
1369: return ($button,$css_class,$message,$previousmsg);
1370: }
1371:
1372: sub markup_unit {
1373: my ($unit,$target)=@_;
1374: if ($target eq 'tex') {
1375: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
1376: } else {
1377: return "<tt>".$unit."</tt>";
1378: }
1379: }
1380:
1381: sub removealldata {
1382: my ($id)=@_;
1383: foreach my $key (keys(%Apache::lonhomework::results)) {
1384: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
1385: &Apache::lonxml::debug("Removing $key");
1386: delete($Apache::lonhomework::results{$key});
1387: }
1388: }
1389: }
1390:
1391: sub hidealldata {
1392: my ($id)=@_;
1393: foreach my $key (keys(%Apache::lonhomework::results)) {
1394: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
1395: &Apache::lonxml::debug("Hidding $key");
1396: my $newkey=$key;
1397: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
1398: $Apache::lonhomework::results{$newkey}=
1399: $Apache::lonhomework::results{$key};
1400: delete($Apache::lonhomework::results{$key});
1401: }
1402: }
1403: }
1404:
1405: sub setgradedata {
1406: my ($award,$msg,$id,$previously_used) = @_;
1407: if ($Apache::lonhomework::scantronmode &&
1408: &Apache::lonnet::validCODE($env{'form.CODE'})) {
1409: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1410: } elsif ($Apache::lonhomework::scantronmode &&
1411: $env{'form.CODE'} eq '' &&
1412: $Apache::lonhomework::history{"resource.CODE"} ne '') {
1413: $Apache::lonhomework::results{"resource.CODE"}='';
1414: }
1415:
1416: if (!$Apache::lonhomework::scantronmode &&
1417: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1418: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
1419: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1420: return '';
1421: } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} < 1
1422: || $Apache::lonhomework::scantronmode
1423: || &Apache::lonhomework::hide_problem_status() ) {
1424: # the student doesn't already have it correct,
1425: # or we are in a mode (scantron orno problem status) where a correct
1426: # can become incorrect
1427: # handle assignment of tries and solved status
1428: my $solvemsg;
1429: if ($Apache::lonhomework::scantronmode) {
1430: $solvemsg='correct_by_scantron';
1431: } else {
1432: $solvemsg='correct_by_student';
1433: }
1434: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
1435: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
1436: }
1437: if ( $award eq 'ASSIGNED_SCORE') {
1438: $Apache::lonhomework::results{"resource.$id.tries"} =
1439: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1440: $Apache::lonhomework::results{"resource.$id.solved"} =
1441: $solvemsg;
1442: my $numawards=scalar(@Apache::inputtags::response);
1443: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1444: foreach my $res (@Apache::inputtags::response) {
1445: if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
1446: $Apache::lonhomework::results{"resource.$id.awarded"}+=
1447: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
1448: } else {
1449: $Apache::lonhomework::results{"resource.$id.awarded"}+=
1450: &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
1451: }
1452: }
1453: if ($numawards > 0) {
1454: $Apache::lonhomework::results{"resource.$id.awarded"}/=
1455: $numawards;
1456: }
1457: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
1458: $Apache::lonhomework::results{"resource.$id.tries"} =
1459: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1460: $Apache::lonhomework::results{"resource.$id.solved"} =
1461: $solvemsg;
1462: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1463: } elsif ( $award eq 'SUBMITTED_CREDIT' ) {
1464: $Apache::lonhomework::results{"resource.$id.tries"} =
1465: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1466: $Apache::lonhomework::results{"resource.$id.solved"} =
1467: 'credit_attempted';
1468: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1469: } elsif ( $award eq 'ANONYMOUS_CREDIT' ) {
1470: $Apache::lonhomework::results{"resource.$id.tries"} =
1471: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1472: $Apache::lonhomework::results{"resource.$id.solved"} =
1473: 'credit_attempted';
1474: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1475: } elsif ( $award eq 'INCORRECT' ) {
1476: $Apache::lonhomework::results{"resource.$id.tries"} =
1477: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1478: if (&Apache::lonhomework::hide_problem_status()
1479: || $Apache::lonhomework::scantronmode) {
1480: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1481: }
1482: $Apache::lonhomework::results{"resource.$id.solved"} =
1483: 'incorrect_attempted';
1484: } elsif ( $award eq 'SUBMITTED' ) {
1485: $Apache::lonhomework::results{"resource.$id.tries"} =
1486: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1487: $Apache::lonhomework::results{"resource.$id.solved"} =
1488: 'ungraded_attempted';
1489: } elsif ( $award eq 'ANONYMOUS' ) {
1490: $Apache::lonhomework::results{"resource.$id.tries"} =
1491: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1492: $Apache::lonhomework::results{"resource.$id.solved"} =
1493: 'ungraded_attempted';
1494: } elsif ( $award eq 'DRAFT' ) {
1495: $Apache::lonhomework::results{"resource.$id.solved"} = '';
1496: } elsif ( $award eq 'NO_RESPONSE' ) {
1497: #no real response so delete any data that got stored
1498: &removealldata($id);
1499: return '';
1500: } else {
1501: $Apache::lonhomework::results{"resource.$id.solved"} =
1502: 'incorrect_attempted';
1503: if (&Apache::lonhomework::show_no_problem_status()
1504: || $Apache::lonhomework::scantronmode) {
1505: $Apache::lonhomework::results{"resource.$id.tries"} =
1506: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1507: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1508: }
1509:
1510: if (&Apache::lonhomework::show_some_problem_status()) {
1511: # clear out the awarded if they had gotten it wrong/right
1512: # and are now in an error mode
1513: $Apache::lonhomework::results{"resource.$id.awarded"} = '';
1514: }
1515: }
1516: if (defined($msg)) {
1517: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
1518: }
1519: # did either of the overall awards chage? If so ignore the
1520: # previous check
1521: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
1522: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
1523: ($Apache::lonhomework::results{"resource.$id.solved"} eq
1524: $Apache::lonhomework::history{"resource.$id.solved"})) {
1525: # check if this was a previous submission if it was delete the
1526: # unneeded data and update the previously_used attribute
1527: if ( $previously_used eq 'PREVIOUSLY_USED') {
1528: if (&Apache::lonhomework::show_problem_status()) {
1529: delete($Apache::lonhomework::results{"resource.$id.tries"});
1530: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
1531: }
1532: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1533: #delete all data as they student didn't do anything, but save
1534: #the list of collaborators.
1535: &removealldata($id);
1536: #and since they didn't do anything we were never here
1537: return '';
1538: } else {
1539: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1540: }
1541: }
1542: } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} == 1 ) {
1543: #delete all data as they student already has it correct
1544: &removealldata($id);
1545: #and since they didn't do anything we were never here
1546: return '';
1547: }
1548: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1549: if ($award eq 'SUBMITTED') {
1550: &Apache::response::add_to_gradingqueue();
1551: }
1552: $Apache::lonhomework::results{"resource.$id.type"} = $Apache::lonhomework::type;
1553: $Apache::lonhomework::results{"resource.$id.duedate"} = &Apache::lonnet::EXT("resource.$id.duedate");
1554: $Apache::lonhomework::results{"resource.$id.hinttries"} = &Apache::lonnet::EXT("resource.$id.hinttries");
1555: $Apache::lonhomework::results{"resource.$id.version"} = &Apache::lonnet::usedversion();
1556: $Apache::lonhomework::results{"resource.$id.maxtries"} = &Apache::lonnet::EXT("resource.$id.maxtries");
1557: }
1558:
1559: sub find_which_previous {
1560: my ($version) = @_;
1561: my $part = $Apache::inputtags::part;
1562: my (@previous_version);
1563: foreach my $resp (@Apache::inputtags::response) {
1564: my $key = "$version:resource.$part.$resp.submission";
1565: my $submission = $Apache::lonhomework::history{$key};
1566: my %previous = &Apache::response::check_for_previous($submission,
1567: $part,$resp,
1568: $version);
1569: push(@previous_version,$previous{'version'});
1570: }
1571: return &previous_match(\@previous_version,
1572: scalar(@Apache::inputtags::response));
1573: }
1574:
1575: sub previous_match {
1576: my ($previous_array,$count) = @_;
1577: my $match = 0;
1578: my @matches;
1579: foreach my $versionar (@$previous_array) {
1580: foreach my $version (@$versionar) {
1581: $matches[$version]++;
1582: }
1583: }
1584: my $which=0;
1585: foreach my $elem (@matches) {
1586: if ($elem eq $count) {
1587: $match=1;
1588: last;
1589: }
1590: $which++;
1591: }
1592: return ($match,$which);
1593: }
1594:
1595: sub grade {
1596: my ($target) = @_;
1597: my $id = $Apache::inputtags::part;
1598: my $response='';
1599: if ( defined $env{'form.submitted'}) {
1600: my (@awards,@msgs);
1601: foreach $response (@Apache::inputtags::response) {
1602: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
1603: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1604: &Apache::lonxml::debug("keeping $value from $response for $id");
1605: push (@awards,$value);
1606: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
1607: &Apache::lonxml::debug("got message $value from $response for $id");
1608: push (@msgs,$value);
1609: }
1610: my ($finalaward,$msg) =
1611: &finalizeawards(\@awards,\@msgs,undef,undef,
1612: $Apache::lonhomework::scantronmode);
1613: my $previously_used;
1614: if ( $#Apache::inputtags::previous eq $#awards ) {
1615: my ($match) =
1616: &previous_match(\@Apache::inputtags::previous_version,
1617: scalar(@Apache::inputtags::response));
1618:
1619: if ($match) {
1620: $previously_used = 'PREVIOUSLY_LAST';
1621: foreach my $value (@Apache::inputtags::previous) {
1622: if ($value eq 'PREVIOUSLY_USED' ) {
1623: $previously_used = $value;
1624: last;
1625: }
1626: }
1627: }
1628: }
1629: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
1630: &setgradedata($finalaward,$msg,$id,$previously_used);
1631: }
1632: return '';
1633: }
1634:
1635: sub get_grade_messages {
1636: my ($id,$prefix,$target,$status,$nocorrect,$tdclass) = @_;
1637: # nocorrect suppresses "Computer's answer now shown above"
1638: my ($message,$latemessage,$trystr,$previousmsg);
1639: my $showbutton = 1;
1640:
1641: my $award = $Apache::lonhomework::history{"$prefix.award"};
1642: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1643: my $solved = $Apache::lonhomework::history{"$prefix.solved"};
1644: my $previous = $Apache::lonhomework::history{"$prefix.previous"};
1645: my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
1646: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1647: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1648: &Apache::lonxml::debug('Getting message');
1649: ($showbutton,my $css_class,$message,$previousmsg) =
1650: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1651: $target,(($status eq 'CAN_ANSWER') || $nocorrect),$tdclass);
1652: if ($target eq 'tex') {
1653: $message='\vskip 2 mm '.$message.' ';
1654: } else {
1655: if ($message) {
1656: $message="<td class=\"$tdclass $css_class\">$message</td>";
1657: } else {
1658: $message="<td class=\"$tdclass\"></td>";
1659: }
1660: if ($previousmsg) {
1661: $previousmsg="<td class=\"$tdclass LC_answer_previous\">$previousmsg</td>";
1662: }
1663: }
1664: }
1665: my $tries = $Apache::lonhomework::history{"$prefix.tries"};
1666: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1667: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1668: #if tries are set to negative turn off the Tries/Button and messages
1669: if (defined($maxtries) && $maxtries < 0) { return ''; }
1670: if ( $tries eq '' ) { $tries = '0'; }
1671: if ( $maxtries eq '' ) { $maxtries = '2'; }
1672: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
1673: my $tries_text= &get_tries_text();
1674: if ($showbutton) {
1675: if ($target eq 'tex') {
1676: if ($env{'request.state'} ne "construct"
1677: && $Apache::lonhomework::type ne 'exam'
1678: && $env{'form.suppress_tries'} ne 'yes') {
1679: $trystr ='{\vskip 1 mm \small '
1680: .&mt('[_1]'.$tries_text.'[_2] [_3]'
1681: ,'\textit{','}',$tries.'/'.$maxtries )
1682: .'} \vskip 2 mm';
1683: } else {
1684: $trystr = '\vskip 0 mm ';
1685: }
1686: } else {
1687: my $trial =$tries;
1688: if ($Apache::lonhomework::parsing_a_task) {
1689: } elsif($env{'request.state'} ne 'construct') {
1690: $trial.="/".&Apache::lonhtmlcommon::direct_parm_link($maxtries,$env{'request.symb'},'maxtries',$id,$target);
1691: } else {
1692: if (defined($Apache::inputtags::params{'maxtries'})) {
1693: $trial.="/".$Apache::inputtags::params{'maxtries'};
1694: }
1695: }
1696:
1697: unless (($env{'request.state'} ne "construct") &&
1698: ($env{'course.'.$env{'request.course.id'}.'.type'} eq 'Placement') &&
1699: (!$env{'request.role.adv'})) {
1700: $trystr = '<span class="LC_nobreak">'.&mt($tries_text.' [_1]',$trial).'</span>';
1701: }
1702: $trystr = '<td class="'.$tdclass.'">'.$trystr.'</td>';
1703: }
1704: }
1705:
1706: if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
1707: #last submissions was after due date
1708: $latemessage=&mt(' The last submission was after the Due Date ');;
1709: if ($target eq 'web') {
1710: $latemessage='<td class="'.$tdclass.' LC_answer_late">'.$latemessage.'</td>';
1711: }
1712: }
1713: return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
1714: }
1715:
1716: sub gradestatus {
1717: my ($id,$target,$no_previous) = @_;
1718: my $showbutton = 1;
1719: my $message = '';
1720: my $latemessage = '';
1721: my $trystr='';
1722: my $button='';
1723: my $previousmsg='';
1724: my $tdclass='';
1725:
1726: my $status = $Apache::inputtags::status['-1'];
1727: &Apache::lonxml::debug("gradestatus has :$status:");
1728: if ( $status ne 'CLOSED'
1729: && $status ne 'UNAVAILABLE'
1730: && $status ne 'INVALID_ACCESS'
1731: && $status ne 'NEEDS_CHECKIN'
1732: && $status ne 'NOT_IN_A_SLOT'
1733: && $status ne 'RESERVABLE'
1734: && $status ne 'RESERVABLE_LATER'
1735: && $status ne 'NOTRESERVABLE'
1736: && $status ne 'NEED_DIFFERENT_IP') {
1737:
1738: if ($status eq 'SHOW_ANSWER') {
1739: $showbutton = 0;
1740: }
1741:
1742: unless (($status eq 'SHOW_ANSWER') || ($status eq 'CANNOT_ANSWER')) {
1743: if ($target ne 'tex') {
1744: $tdclass = 'LC_status_submit_'.$id;
1745: }
1746: }
1747:
1748: ($previousmsg,$latemessage,$message,$trystr) =
1749: &get_grade_messages($id,"resource.$id",$target,$status,
1750: $showbutton,$tdclass);
1751: if ($status eq 'CANNOT_ANSWER') {
1752: $showbutton = 0;
1753: }
1754: if ( $status eq 'SHOW_ANSWER') {
1755: undef($previousmsg);
1756: }
1757: if ( $showbutton ) {
1758: if ($target ne 'tex') {
1759: $button =
1760: '<input onmouseup="javascript:setSubmittedPart(\''.$id.'\');this.form.action+=\'#'.&escape($id).'\';"
1761: type="submit" name="submit_'.$id.'" id="submit_'.$id.'" class="LC_hwk_submit"
1762: value="'.&mt('Submit Answer').'" /> '.
1763: '<div id="msg_submit_'.$id.'" style="display:none">'.
1764: &mt('Processing your submission ...').'</div>';
1765: }
1766: }
1767:
1768: }
1769: my $output= $previousmsg.$latemessage.$message.$trystr;
1770: if ($output =~ /^\s*$/) {
1771: return $button;
1772: } else {
1773: if ($target eq 'tex') {
1774: return $button.' \vskip 0 mm '.$output.' ';
1775: } else {
1776: $output =
1777: '<table><tr><td>'.$button.'</td>'.$output;
1778: if ((!$no_previous) &&
1779: (($env{'course.'.$env{'request.course.id'}.'.type'} ne 'Placement') ||
1780: ($env{'request.role.adv'}))) {
1781: $output.='<td class="'.$tdclass.'">'.&previous_tries($id,$target).'</td>';
1782: }
1783: $output.= '</tr></table>';
1784: return $output;
1785: }
1786: }
1787: }
1788:
1789: sub previous_tries {
1790: my ($id,$target) = @_;
1791: my $output;
1792: my $status = $Apache::inputtags::status['-1'];
1793:
1794: my $count;
1795: my %count_lookup;
1796: my ($lastrndseed,$lasttype);
1797: my $numstamps = 0;
1798:
1799: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1800: my $prefix = $i.":resource.$id";
1801: my $is_anon;
1802: my $curr_type = $Apache::lonhomework::history{"$prefix.type"};
1803: if (defined($env{'form.grade_symb'})) {
1804: if (($curr_type eq 'anonsurvey') || ($curr_type eq 'anonsurveycred')) {
1805: $is_anon = 1;
1806: }
1807: }
1808: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1809: $count++;
1810: $count_lookup{$i} = $count;
1811: my $curr_rndseed = $Apache::lonhomework::history{"$prefix.rndseed"};
1812: my ($previousmsg,$latemessage,$message,$trystr);
1813:
1814: ($previousmsg,$latemessage,$message,$trystr) =
1815: &get_grade_messages($id,"$prefix",$target,$status);
1816:
1817: if ($previousmsg ne '') {
1818: my ($match,$which) = &find_which_previous($i);
1819: $message=$previousmsg;
1820: my $previous = $count_lookup{$which};
1821: $message =~ s{(</td>)}{ as submission \# $previous $1};
1822: } elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1823: if (!(&Apache::lonhomework::hide_problem_status()
1824: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
1825: && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
1826: ) {
1827:
1828: my $txt_correct = &mt('Correct');
1829: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1830: if ($awarded < 1 && $awarded > 0) {
1831: $txt_correct=&mt('Partially Correct');
1832: } elsif ($awarded < 1) {
1833: if ($awarded eq '') {
1834: $txt_correct='';
1835: } else {
1836: $txt_correct=&mt('Incorrect');
1837: }
1838: }
1839: $message =~ s{(<td.*?>)(.*?)(</td>)}
1840: {$1 <strong>$txt_correct</strong>. $3}s;
1841: }
1842: my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1843: if (($curr_rndseed ne '') && ($lastrndseed ne '')) {
1844: if (($curr_rndseed ne $lastrndseed) &&
1845: (($curr_type eq 'randomizetry') || ($lasttype eq 'randomizetry'))) {
1846: $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>';
1847: }
1848: }
1849: $message =~ s{(</td>)}{ $trystr $1};
1850: }
1851: my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
1852: $message =~ s{(<td.*?>)}{<td>};
1853:
1854:
1855: $output .= '<tr class="'.$class.'">'.
1856: '<td align="center">'.$count.'</td>'.$message;
1857: if ((!$is_anon) && ($Apache::lonhomework::history{"$prefix.tries"}) &&
1858: ($Apache::lonhomework::history{"$prefix.award"} ne 'ASSIGNED_SCORE') &&
1859: ($Apache::lonhomework::history{$i.':timestamp'})) {
1860: $output .= '<td>'.&Apache::lonlocal::locallocaltime(
1861: $Apache::lonhomework::history{$i.':timestamp'}).'</td>';
1862: $numstamps ++;
1863: } else {
1864: $output .= '<td></td>';
1865: }
1866: foreach my $resid (@Apache::inputtags::response) {
1867: my $prefix = $prefix.".$resid";
1868: if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
1869: my $submission =
1870: $Apache::inputtags::submission_display{"$prefix.submission"};
1871: if (!defined($submission)) {
1872: $submission =
1873: $Apache::lonhomework::history{"$prefix.submission"};
1874: }
1875: if ($is_anon) {
1876: $output.='<td>'.&mt('(only shown to submitter)').'</td>';
1877: } else {
1878: $output.='<td>'.$submission.'</td>';
1879: }
1880: } else {
1881: $output.='<td></td>';
1882: }
1883: }
1884: $output.=&Apache::loncommon::end_data_table_row()."\n";
1885: $lastrndseed = $curr_rndseed;
1886: $lasttype = $curr_type;
1887: }
1888: return if ($output eq '');
1889: my $headers = '<tr>'.
1890: '<th>'.&mt('Submission #').'</th>'.
1891: '<th>'.&mt('Try').'</th><th>';
1892: if ($numstamps) {
1893: $headers .= &mt('When');
1894: }
1895: $headers .= '</th>';
1896: my $colspan = scalar(@Apache::inputtags::response);
1897: if ($colspan > 1) {
1898: $headers .= '<th colspan="'.$colspan.'">';
1899: } else {
1900: $headers .= '<th>';
1901: }
1902: $headers .= &mt('Submitted Answer').'</th></tr>';
1903: $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1904:
1905: my $tries_text = &get_tries_text('link');
1906: my $prefix = $env{'form.request.prefix'};
1907: $prefix =~ tr{.}{_};
1908: my $function_name = 'LONCAPA_previous_tries_'.$prefix;
1909: if (($env{'request.state'} eq 'construct') || ($id =~ /[._]|[^\w\s\-]/)) {
1910: $function_name .= $Apache::lonxml::curdepth;
1911: } else {
1912: $function_name .= &js_escape($id);
1913: }
1914: $function_name .= '_'.$Apache::lonxml::counter;
1915: my $result = &Apache::loncommon::modal_adhoc_window($function_name,420,410,$output,&mt($tries_text))."<br />";
1916: return $result;
1917: }
1918:
1919: sub get_tries_text {
1920: my ($context) = @_;
1921: my $tries_text;
1922: if ($context eq 'link') {
1923: $tries_text = 'Previous Tries';
1924: } else {
1925: $tries_text = 'Tries';
1926: }
1927: if ( $Apache::lonhomework::type eq 'survey' ||
1928: $Apache::lonhomework::type eq 'surveycred' ||
1929: $Apache::lonhomework::type eq 'anonsurvey' ||
1930: $Apache::lonhomework::type eq 'anonsurveycred' ||
1931: $Apache::lonhomework::parsing_a_task) {
1932: if ($context eq 'link') {
1933: $tries_text = 'Previous Submissions';
1934: } else {
1935: $tries_text = 'Submissions';
1936: }
1937: }
1938: return $tries_text;
1939: }
1940:
1941: sub spelling_languages {
1942: my %langchoices;
1943: foreach my $id (&Apache::loncommon::languageids()) {
1944: my $code = &Apache::loncommon::supportedlanguagecode($id);
1945: if ($code ne '') {
1946: $langchoices{$code} = &Apache::loncommon::plainlanguagedescription($id);
1947: }
1948: }
1949: my @spelllangs = ('none');
1950: foreach my $code ('en','de','he','es','fr','pt','tr') {
1951: push(@spelllangs,[$code,$langchoices{$code}]);
1952: }
1953: return \@spelllangs;
1954: }
1955:
1956: sub edit_mathresponse_button {
1957: my ($field) = @_;
1958: my $eqneditor = 'lcmath';
1959: if ($env{'browser.type'} eq 'safari') {
1960: if ($env{'browser.os'} eq 'mac') {
1961: my ($prefix,$version) = ($env{'browser.version'} =~ /^(\d*)(\d{3})\./);
1962: if ($env{'browser.mobile'}) {
1963: if (($version < 531) || (($prefix eq '') && ($version < 533))) {
1964: $eqneditor = '';
1965: }
1966: } elsif ($version < 533) {
1967: $eqneditor = 'dragmath';
1968: }
1969: } elsif ($env{'browser.os'} eq 'win') {
1970: if ($env{'browser.version'} < 533) {
1971: $eqneditor = 'dragmath';
1972: }
1973: }
1974: } elsif ($env{'browser.type'} eq 'explorer') {
1975: if ($env{'browser.version'} < 9) {
1976: $eqneditor = 'dragmath';
1977: }
1978: } elsif ($env{'browser.type'} eq 'mozilla') {
1979: if ($env{'browser.version'} < 5) {
1980: $eqneditor = 'dragmath';
1981: } else {
1982: if ($env{'browser.info'} =~ /^firefox\-([\d\.]+)/) {
1983: my $firefox = $1;
1984: if ($firefox < 4) {
1985: $eqneditor = 'dragmath';
1986: }
1987: }
1988: }
1989: } elsif ($env{'browser.type'} eq 'chrome') {
1990: if ($env{'browser.version'} < 5) {
1991: $eqneditor = 'dragmath';
1992: }
1993: } elsif ($env{'browser.type'} eq 'opera') {
1994: if ($env{'browser.version'} < 12) {
1995: $eqneditor = 'dragmath';
1996: }
1997: }
1998: if ($eqneditor eq 'lcmath') {
1999: if (($env{'request.course.id'}) && ($env{'request.state'} ne 'construct')) {
2000: if (exists($env{'course.'.$env{'request.course.id'}.'.uselcmath'})) {
2001: if ($env{'course.'.$env{'request.course.id'}.'.uselcmath'} eq '0') {
2002: $eqneditor = 'dragmath';
2003: }
2004: } else {
2005: my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
2006: if ($domdefs{'uselcmath'} eq '0') {
2007: $eqneditor = 'dragmath';
2008: }
2009: }
2010: } else {
2011: my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
2012: if ($domdefs{'uselcmath'} eq '0') {
2013: $eqneditor = 'dragmath';
2014: }
2015: }
2016: }
2017: if ($eqneditor eq 'dragmath') {
2018: # DragMath applet
2019: my $button=&mt('Edit Answer');
2020: # my $helplink=&Apache::loncommon::help_open_topic('Formula_Editor');
2021: my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
2022: return(<<ENDFORMULABUTTON);
2023: <script type="text/javascript" language="JavaScript">
2024: function LC_mathedit_${field} (LCtextline) {
2025: thenumber = LCtextline;
2026: var thedata = '';
2027: if (document.getElementById(LCtextline)) {
2028: thedata = document.getElementById(LCtextline).value;
2029: }
2030: newwin = window.open("/adm/dragmath/MaximaPopup.html","","width=565,height=400,resizable");
2031: }
2032: </script>
2033: <a href="javascript:LC_mathedit_${field}('${field}');void(0);"><img class="stift" src="$iconpath/stift.gif" alt="$button" title="$button" /></a>
2034: ENDFORMULABUTTON
2035: } elsif ($eqneditor eq 'lcmath') {
2036: # LON-CAPA math equation editor
2037: my $mathjaxjs;
2038: unless (lc(&Apache::lontexconvert::tex_engine()) eq 'mathjax') {
2039: $mathjaxjs = <<"MATHJAX_SCRIPT";
2040: var mathjaxscript = document.createElement("script");
2041: mathjaxscript.type = "text/javascript";
2042: mathjaxscript.src = "/adm/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
2043: document.body.appendChild(mathjaxscript);
2044: MATHJAX_SCRIPT
2045: }
2046: return(<<EQ_EDITOR_SCRIPT);
2047: <script type="text/javascript">
2048: var LCmathField = document.getElementById('${field}');
2049: LCmathField.className += ' math'; // note the space
2050: LCmathField.setAttribute('data-implicit_operators', 'true');
2051: var LCMATH_started;
2052: if (typeof LCMATH_started === 'undefined') {
2053: $mathjaxjs
2054: LCMATH_started = true;
2055: var script = document.createElement("script");
2056: script.type = "text/javascript";
2057: script.src = "/adm/LC_math_editor/LC_math_editor.min.js";
2058: document.body.appendChild(script);
2059: window.addEventListener('load', function(e) {
2060: LCMATH.initEditors();
2061: }, false);
2062: }
2063: </script>
2064: EQ_EDITOR_SCRIPT
2065: }
2066: }
2067:
2068: 1;
2069: __END__
2070:
2071: =pod
2072:
2073: =back
2074:
2075: =cut
2076:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>