1: # The LearningOnline Network with CAPA
2: # input definitons
3: #
4: # $Id: inputtags.pm,v 1.271.2.2 2010/12/20 19:34:47 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',('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: sub check_for_duplicate_ids {
129: my %check;
130: foreach my $id (@Apache::inputtags::partlist,
131: @Apache::inputtags::responselist,
132: @Apache::inputtags::hintlist,
133: @Apache::inputtags::importlist) {
134: $check{$id}++;
135: }
136: my @duplicates;
137: foreach my $id (sort(keys(%check))) {
138: if ($check{$id} > 1) {
139: push(@duplicates,$id);
140: }
141: }
142: if (@duplicates) {
143: &Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
144: }
145: }
146:
147: sub start_input {
148: my ($parstack,$safeeval)=@_;
149: my $id = &Apache::lonxml::get_id($parstack,$safeeval);
150: push (@Apache::inputtags::input,$id);
151: push (@Apache::inputtags::inputlist,$id);
152: return $id;
153: }
154:
155: sub end_input {
156: pop @Apache::inputtags::input;
157: return '';
158: }
159:
160: sub addchars {
161: my ($fieldid,$addchars)=@_;
162: my $output='';
163: foreach (split(/\,/,$addchars)) {
164: $output.='<a href="javascript:void(document.forms.lonhomework.'.
165: $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
166: }
167: return $output;
168: }
169:
170: sub start_textfield {
171: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
172: my $result = "";
173: my $id = &start_input($parstack,$safeeval);
174: my $resid=$Apache::inputtags::response[-1];
175: if ($target eq 'web') {
176: $Apache::lonxml::evaluate--;
177: my $partid=$Apache::inputtags::part;
178: if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
179: ($Apache::lonhomework::type eq 'randomizetry')) &&
180: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
181: if ($env{'form.'.$partid.'.rndseed'} ne
182: $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
183: $newvariation = 1;
184: }
185: }
186: unless ($newvariation) {
187: $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
188: }
189: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
190: my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
191: if ( $cols eq '') { $cols = 80; }
192: my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
193: if ( $rows eq '') { $rows = 16; }
194: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
195: $result='';
196: if ($addchars) {
197: $result.=&addchars('HWVAL_'.$resid,$addchars);
198: }
199: my $textareaclass = 'class="LC_richDetectHtml"';
200: $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
201: 'rows="'.$rows.'" cols="'.$cols.'" '.$textareaclass.'>'.
202: $oldresponse;
203: if ($oldresponse ne '') {
204:
205: #get rid of any startup text if the user has already responded
206: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
207: }
208: } else {
209: #show past answer in the essayresponse case
210: if ($oldresponse =~ /\S/
211: && &Apache::londefdef::is_inside_of($tagstack,
212: 'essayresponse') ) {
213: $result='<table class="LC_pastsubmission"><tr><td>'.
214: $oldresponse.'</td></tr></table>';
215: }
216: #get rid of any startup text
217: &Apache::lonxml::get_all_text("/textfield",$parser,$style);
218: }
219: } elsif ($target eq 'grade') {
220: my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
221: $style);
222: if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
223: # if the seed text is still there it wasn't a real submission
224: $env{'form.HWVAL_'.$resid}='';
225: }
226: } elsif ($target eq 'edit') {
227: $result.=&Apache::edit::tag_start($target,$token);
228: $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
229: $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
230: $result.=&Apache::edit::text_arg
231: ('Click-On Texts (comma sep):','addchars',$token,10);
232: my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
233: $style);
234: $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
235: } elsif ($target eq 'modified') {
236: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
237: $safeeval,'rows','cols',
238: 'addchars');
239: if ($constructtag) {
240: $result = &Apache::edit::rebuild_tag($token);
241: } else {
242: $result=$token->[4];
243: }
244: $result.=&Apache::edit::modifiedfield("/textfield",$parser);
245: } elsif ($target eq 'tex') {
246: my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
247: my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
248: if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
249: $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
250: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
251: $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
252: } else {
253: my $TeXwidth=$width_of_box/80;
254: $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
255: for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
256: $result.='}}}\vskip 2 mm ';
257: }
258: }
259: return $result;
260: }
261:
262: sub end_textfield {
263: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
264: my $result;
265: if ($target eq 'web') {
266: $Apache::lonxml::evaluate++;
267: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
268: return "</textarea>";
269: }
270: } elsif ($target eq 'edit') {
271: $result=&Apache::edit::end_table();
272: }
273: &end_input;
274: return $result;
275: }
276:
277: sub exam_score_line {
278: my ($target) = @_;
279:
280: my $result;
281: if ($target eq 'tex') {
282: my $repetition = &Apache::response::repetition();
283: $result.='\begin{enumerate}';
284: if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
285: foreach my $i (0..$repetition-1) {
286: $result.='\item[\textbf{'.
287: ($Apache::lonxml::counter+$i).
288: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
289: }
290: $result.= '\end{enumerate}';
291: }
292:
293: return $result;
294: }
295:
296: sub exam_box {
297: my ($target) = @_;
298: my $result;
299:
300: if ($target eq 'tex') {
301: $result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
302: $result .= &exam_score_line($target);
303: } elsif ($target eq 'web') {
304: my $id=$Apache::inputtags::response[-1];
305: $result.= '<br /><br />
306: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
307: </textarea> <br /><br />';
308: }
309: return $result;
310: }
311:
312: sub needs_exam_box {
313: my ($tagstack) = @_;
314: my @tags = ('formularesponse',
315: 'stringresponse',
316: 'reactionresponse',
317: 'organicresponse',
318: );
319:
320: foreach my $tag (@tags) {
321: if (grep(/\Q$tag\E/,@$tagstack)) {
322: return 1;
323: }
324: }
325: return 0;
326: }
327:
328: sub start_textline {
329: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
330: my $result = "";
331: my $input_id = &start_input($parstack,$safeeval);
332: if ($target eq 'web') {
333: $Apache::lonxml::evaluate--;
334: my $partid=$Apache::inputtags::part;
335: my $id=$Apache::inputtags::response[-1];
336: if (!&Apache::response::show_answer()) {
337: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
338: my $maxlength;
339: if ($size eq '') { $size=20; } else {
340: if ($size < 20) {
341: $maxlength = ' maxlength="'.$size.'"';
342: }
343: }
344: my ($oldresponse,$newvariation);
345: if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
346: ($Apache::lonhomework::type eq 'randomizetry')) &&
347: ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
348: if ($env{'form.'.$partid.'.rndseed'} ne
349: $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
350: $newvariation = 1;
351: }
352: }
353: unless ($newvariation) {
354: $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
355: &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
356:
357: if (ref($oldresponse) eq 'ARRAY') {
358: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
359: }
360: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
361: $oldresponse =~ s/^\s+//;
362: $oldresponse =~ s/\s+$//;
363: $oldresponse =~ s/\s+/ /g;
364: }
365: if ($Apache::lonhomework::type ne 'exam') {
366: my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
367: $result='';
368: if ($addchars) {
369: $result.=&addchars('HWVAL_'.$id,$addchars);
370: }
371: my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
372: $safeeval);
373: if (lc($readonly) eq 'yes'
374: || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
375: $readonly=' readonly="readonly" ';
376: } else {
377: $readonly='';
378: }
379: my $name = 'HWVAL_'.$id;
380: if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
381: $name = "none";
382: }
383: $result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');" type="text" '.$readonly.' name="'.$name.'" value="'.
384: $oldresponse.'" size="'.$size.'"'.$maxlength.' />';
385: }
386: if ($Apache::lonhomework::type eq 'exam'
387: && &needs_exam_box($tagstack)) {
388: $result.=&exam_box($target);
389: }
390: } else {
391: #right or wrong don't show what was last typed in.
392: my $count = scalar(@Apache::inputtags::inputlist)-1;
393: $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
394: #$result='';
395: }
396: } elsif ($target eq 'edit') {
397: $result=&Apache::edit::tag_start($target,$token);
398: $result.=&Apache::edit::text_arg('Size:','size',$token,'5').
399: &Apache::edit::text_arg('Click-On Texts (comma sep):',
400: 'addchars',$token,10);
401: $result.=&Apache::edit::select_arg('Readonly:','readonly',
402: ['no','yes'],$token);
403: $result.=&Apache::edit::end_row();
404: $result.=&Apache::edit::end_table();
405: } elsif ($target eq 'modified') {
406: my $constructtag=&Apache::edit::get_new_args($token,$parstack,
407: $safeeval,'size',
408: 'addchars','readonly');
409: if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
410: } elsif ($target eq 'tex'
411: && $Apache::lonhomework::type ne 'exam') {
412: my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
413: if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
414: if ($env{'form.pdfFormFields'} eq 'yes'
415: && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
416: my $fieldname = $env{'request.symb'}.
417: '&part_'. $Apache::inputtags::part.
418: '&textresponse'.
419: '&HWVAL_' . $Apache::inputtags::response['-1'];
420: $result='\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
421: } else {
422: $result='\framebox['.$size.'][s]{\tiny\strut}';
423: }
424: } elsif ($target eq 'tex'
425: && $Apache::lonhomework::type eq 'exam'
426: && &needs_exam_box($tagstack)) {
427: $result.=&exam_box($target);
428: }
429: return $result;
430: }
431:
432: sub end_textline {
433: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
434: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
435: elsif ($target eq 'edit') { return ('','no'); }
436: &end_input();
437: return "";
438: }
439:
440: sub start_hiddenline {
441: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
442: my $result = "";
443: my $input_id = &start_input($parstack,$safeeval);
444: if ($target eq 'web') {
445: $Apache::lonxml::evaluate--;
446: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
447: my $partid=$Apache::inputtags::part;
448: my $id=$Apache::inputtags::response[-1];
449: my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
450: if (ref($oldresponse) eq 'ARRAY') {
451: $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
452: }
453: $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
454:
455: if ($Apache::lonhomework::type ne 'exam') {
456: $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
457: $oldresponse.'" />';
458: }
459: }
460: } elsif ($target eq 'edit') {
461: $result=&Apache::edit::tag_start($target,$token);
462: $result.=&Apache::edit::end_table;
463: }
464:
465: if ( ($target eq 'web' || $target eq 'tex')
466: && $Apache::lonhomework::type eq 'exam'
467: && &needs_exam_box($tagstack)) {
468: $result.=&exam_box($target);
469: }
470: return $result;
471: }
472:
473: sub end_hiddenline {
474: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
475: if ($target eq 'web') { $Apache::lonxml::evaluate++; }
476: elsif ($target eq 'edit') { return ('','no'); }
477: &end_input();
478: return "";
479: }
480:
481: =pod
482:
483: =item file_selector()
484:
485: $part -> partid
486: $id -> responseid
487: $uploadefiletypes -> comma seperated list of extensions allowed or * for any
488: $which -> 'uploadonly' -> only newly uploaded files
489: 'portfolioonly' -> only allow files from portfolio
490: 'both' -> allow files from either location
491: $extratext -> additional text to go between the link and the input box
492: $maxfilesize -> maximum cumulative filesize for submitted files (in MB).
493: returns a table row <tr>
494:
495: =cut
496:
497: sub file_selector {
498: my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_;
499: if (!$uploadedfiletypes) { return ''; }
500:
501: my $jspart=$part;
502: $jspart=~s/\./_/g;
503:
504: my $result;
505: my $current_files_display = ¤t_file_submissions($part,$id);
506: my $addfiles;
507: if ($current_files_display) {
508: $result .= &Apache::lonhtmlcommon::row_title(&mt('Currently submitted files')).
509: $current_files_display.
510: &Apache::lonhtmlcommon::row_closure();
511: $addfiles = &mt('Submit other file(s)');
512: } else {
513: $addfiles = &mt('Choose file(s) to submit');
514: }
515: $result .= &Apache::lonhtmlcommon::row_title($addfiles);
516: my $constraints;
517: if ($uploadedfiletypes ne '*') {
518: $constraints =
519: &mt('Allowed filetypes: [_1]','<b>'.$uploadedfiletypes.'</b>').'<br />';
520: }
521: if ($maxfilesize) {
522: $constraints .= &mt('Combined size of all files not to exceed: [_1] MB[_2].',
523: '<b>'.$maxfilesize.'</b>').'<br />';
524: }
525: if ($constraints) {
526: $result .= $constraints.'<br />';
527: }
528: if ($which eq 'uploadonly' || $which eq 'both') {
529: $result.=&mt('Submit a file: (only one file per submission)').
530: ' <br /><input type="file" size="50" name="HWFILE'.
531: $jspart.'_'.$id.'" /><br />';
532: }
533: if ( $which eq 'both') {
534: $result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
535: }
536: if ($which eq 'portfolioonly' || $which eq 'both') {
537: $result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
538: &mt('Select Portfolio Files: (one or more files per submission)').'</a><br />'.
539: '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
540: '<br />';
541:
542: }
543: $result.=&Apache::lonhtmlcommon::row_closure(1);
544: return $result;
545: }
546:
547: sub current_file_submissions {
548: my ($part,$id) = @_;
549: my $jspart=$part;
550: $jspart=~s/\./_/g;
551: my $uploadedfile=$Apache::lonhomework::history{"resource.$part.$id.uploadedfile"};
552: my $portfiles=$Apache::lonhomework::history{"resource.$part.$id.portfiles"};
553: return if (($uploadedfile eq '') && ($portfiles !~/[^\s]/));
554: my $header = &Apache::loncommon::start_data_table().
555: &Apache::loncommon::start_data_table_header_row();
556: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
557: $header .= '<th>'.&mt('Delete?').'</th>';
558: }
559: $header .= '<th>'.&mt('File').'</th>'.
560: '<th>'.&mt('Size (MB)').'</th>'.
561: '<th>'.&mt('Last Modified').'</th>'.
562: &Apache::loncommon::end_data_table_header_row();
563: my (undef,$crsid,$udom,$uname)=&Apache::lonnet::whichuser();
564: my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/);
565: my ($result,$header_shown,%okfiles,%rows,%legacy,@bad_file_list);
566: if ($uploadedfile) {
567: my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
568: my $link = &HTML::Entities::encode($url,'<>&"');
569: my ($path,$name) = ($url =~ m{^(/uploaded/\Q$udom\E/\Q$uname\E/essayresponse.*/)([^/]+)$});
570: my ($status,$hashref,$error) =
571: ¤t_file_info($url,$link,$name,$path);
572: if ($status eq 'ok') {
573: push(@{$okfiles{$name}},$url);
574: $rows{$url} = $hashref;
575: $legacy{$url} = 1;
576: &Apache::lonxml::extlink($url);
577: &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
578: } else {
579: push(@bad_file_list,$error);
580: }
581: }
582: if ($portfiles =~ /[^\s]/) {
583: my $prefix = "/uploaded/$udom/$uname/portfolio";
584: foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) {
585: my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
586: my $url = $prefix.$path.$name;
587: my $uploadedfile = &HTML::Entities::encode($url,'<>&"');
588: my ($status,$hashref,$error) =
589: ¤t_file_info($url,$uploadedfile,$name,$path);
590: if ($status eq 'ok') {
591: push(@{$okfiles{$name}},$url);
592: $rows{$url} = $hashref;
593: } else {
594: push(@bad_file_list,$error);
595: }
596: }
597: }
598: foreach my $name (sort(keys(%okfiles))) {
599: if (ref($okfiles{$name}) eq 'ARRAY') {
600: my $num = 0;
601: foreach my $url (@{$okfiles{$name}}) {
602: if (ref($rows{$url}) eq 'HASH') {
603: my $link = $rows{$url}{link};
604: my $portfile = $rows{$url}{path}.$rows{$url}{name};
605: $portfile = &HTML::Entities::encode($portfile,'<>&"');
606: if ($link) {
607: my $icon=&Apache::loncommon::icon($url);
608: unless ($header_shown) {
609: $result .= $header;
610: $header_shown = 1;
611: }
612: $result.=
613: &Apache::loncommon::start_data_table_row()."\n";
614: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
615: $result .=
616: '<td valign="bottom"><input type="checkbox" name="HWFILE'.$jspart.'_'.$id.'_delete"'.
617: ' value="'.$portfile.'" id="HWFILE'.$jspart.'_'.$id.'_'.$num.'_delete" /></td>'."\n";
618: $num ++;
619: }
620: my $showname = $rows{$url}{path}.$name;
621: if ($legacy{$url}) {
622: $showname = $name.' '.&mt('not in portfolio');
623: }
624: $result .=
625: '<td><a href="'.$link.'"><img src="'.$icon.
626: '" border="0" />'.$showname.'</a></td>'."\n".
627: '<td align="right" valign="bottom">'.$rows{$url}{size}.'</td>'."\n".
628: '<td align="right" valign="bottom">'.$rows{$url}{lastmodified}.'</td>'."\n".
629: &Apache::loncommon::end_data_table_row();
630: }
631: }
632: }
633: }
634: }
635: if ($header_shown) {
636: $result .= &Apache::loncommon::end_data_table().
637: '<br /><span class="LC_warning">'.
638: &mt('Items checked for deletion will not be included amongst the files evaluated when your submission is graded.').'</span>';
639: }
640: if (@bad_file_list) {
641: my $bad_files = '<span class="LC_filename">'.
642: join('</span>, <span class="LC_filename">',@bad_file_list).
643: '</span>';
644: $result.='<p class="LC_error">'.
645: &mt("These file(s) don't exist: [_1]",$bad_files).
646: '</p>';
647: }
648: return $result;
649: }
650:
651: sub current_file_info {
652: my ($url,$uploadedfile,$name,$path) = @_;
653: my ($status,$error,%info);
654: my @stat = &Apache::lonnet::stat_file($url);
655: if ((@stat) && ($stat[0] ne 'no_such_dir')) {
656: my ($lastmod,$size);
657: if ($stat[9] =~ /^\d+$/) {
658: $lastmod = &Apache::lonlocal::locallocaltime($stat[9]);
659: }
660: $size = $stat[7]/(1024*1024);
661: $size = sprintf("%.3f",$size);
662: %info = (
663: link => $uploadedfile,
664: name => $name,
665: path => $path,
666: size => $size,
667: lastmodified => $lastmod,
668: );
669: $status = 'ok';
670: } else {
671: &Apache::lonnet::logthis("bad file is $url");
672: my $icon=&Apache::loncommon::icon($url);
673: $error = '<a href="'.$url.'"><img src="'.$icon.
674: '" border="0" />'.$uploadedfile.'</a>';
675: }
676: return ($status,\%info,$error);
677: }
678:
679: sub valid_award {
680: my ($award) =@_;
681: foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
682: 'NO_RESPONSE',
683: 'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
684: 'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
685: 'UNIT_FAIL', 'NO_UNIT',
686: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
687: 'BAD_FORMULA', 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT',
688: 'MISORDERED_RANK', 'INVALID_FILETYPE',
689: 'EXCESS_FILESIZE', 'FILENAME_INUSE',
690: 'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT',
691: 'ANONYMOUS', 'ANONYMOUS_CREDIT',
692: 'ASSIGNED_SCORE', 'APPROX_ANS',
693: 'EXACT_ANS','COMMA_FAIL') {
694: if ($award eq $possibleaward) { return 1; }
695: }
696: return 0;
697: }
698:
699: {
700: my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
701: 'TOO_LONG',
702: 'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
703: 'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
704: 'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA', 'INTERNAL_ERROR',
705: 'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
706: 'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE',
707: 'DRAFT', 'SUBMITTED',
708: 'SUBMITTED_CREDIT', 'ANONYMOUS', 'ANONYMOUS_CREDIT',
709: 'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
710: my $i=0;
711: my %fwd_awards = map { ($_,$i++) } @awards;
712: my $max=scalar(@awards);
713: @awards=reverse(@awards);
714: $i=0;
715: my %rev_awards = map { ($_,$i++) } @awards;
716:
717: sub awarddetail_to_awarded {
718: my ($awarddetail) = @_;
719: if ($awarddetail eq 'EXACT_ANS'
720: || $awarddetail eq 'APPROX_ANS') {
721: return 1;
722: }
723: return 0;
724: }
725:
726: sub hide_award {
727: my ($award) = @_;
728: if (&Apache::lonhomework::show_no_problem_status()) {
729: return 1;
730: }
731: if ($award =~
732: /^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|SUBMITTED_CREDIT|ANONYMOUS|ANONYMOUS_CREDIT|ASSIGNED_SCORE|INCORRECT)/) {
733: return 1;
734: }
735: return 0;
736: }
737:
738: sub finalizeawards {
739: my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
740: my $result;
741: if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
742: if ($result eq '' ) {
743: my $blankcount;
744: foreach my $award (@$awardref) {
745: if ($award eq '') {
746: $result='MISSING_ANSWER';
747: $blankcount++;
748: }
749: }
750: if ($blankcount == ($#$awardref + 1)) {
751: return ('NO_RESPONSE');
752: }
753: }
754:
755: if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; }
756:
757: if (!$final_scantron && defined($result)) { return ($result); }
758:
759: # if in scantron mode, if the award for any response is
760: # assigned score, then the part gets an assigned score
761: if ($final_scantron
762: && grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
763: return ('ASSIGNED_SCORE');
764: }
765:
766: # if in scantron mode, if the award for any response is
767: # correct and there are non-correct responses,
768: # then the part gets an assigned score
769: if ($final_scantron
770: && (grep { $_ eq 'EXACT_ANS' ||
771: $_ eq 'APPROX_ANS' } (@$awardref))
772: && (grep { $_ ne 'EXACT_ANS' &&
773: $_ ne 'APPROX_ANS' } (@$awardref))) {
774: return ('ASSIGNED_SCORE');
775: }
776: # these awards are ordered from most important error through best correct
777: my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
778:
779: my $best = $max;
780: my $j=0;
781: my $which;
782: foreach my $award (@$awardref) {
783: if ($awards->{$award} < $best) {
784: $best = $awards->{$award};
785: $which = $j;
786: }
787: $j++;
788: }
789:
790: if (defined($which)) {
791: if (ref($nameref)) {
792: return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
793: } else {
794: return ($$awardref[$which],$$msgref[$which]);
795: }
796: }
797: return ('ERROR',undef);
798: }
799: }
800:
801: sub decideoutput {
802: my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
803:
804: my $message='';
805: my $button=0;
806: my $previousmsg;
807: my $css_class='orange';
808: my $added_computer_text=0;
809: my %possible_class =
810: ( 'correct' => 'LC_answer_correct',
811: 'charged_try' => 'LC_answer_charged_try',
812: 'not_charged_try' => 'LC_answer_not_charged_try',
813: 'no_grade' => 'LC_answer_no_grade',
814: 'no_message' => 'LC_no_message',
815: );
816:
817: my $part = $Apache::inputtags::part;
818: my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
819: my $handgrade = ('yes' eq lc($tohandgrade));
820:
821: my $computer = ($handgrade)? ''
822: : " ".&mt("Computer's answer now shown above.");
823: &Apache::lonxml::debug("handgrade has :$handgrade:");
824:
825: if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
826:
827: if ($solved =~ /^correct/) {
828: $css_class=$possible_class{'correct'};
829: $message=&mt('You are correct.');
830: if ($awarded < 1 && $awarded > 0) {
831: $message=&mt('You are partially correct.');
832: $css_class=$possible_class{'not_charged_try'};
833: } elsif ($awarded < 1) {
834: $message=&mt('Incorrect.');
835: $css_class=$possible_class{'charged_try'};
836: }
837: if ($env{'request.filename'} =~
838: m|/res/lib/templates/examupload.problem$|) {
839: $message = &mt("A score has been assigned.");
840: $added_computer_text=1;
841: } else {
842: if ($target eq 'tex') {
843: $message = '\textbf{'.$message.'}';
844: } else {
845: $message = "<b>".$message."</b>";
846: $message.= $computer;
847: }
848: $added_computer_text=1;
849: if ($awarded > 0) {
850: my ($symb) = &Apache::lonnet::whichuser();
851: if (($symb ne '')
852: &&
853: ($env{'course.'.$env{'request.course.id'}.
854: '.disable_receipt_display'} ne 'yes') &&
855: ($Apache::lonhomework::type ne 'practice')) {
856: $message.=(($target eq 'web')?'<br />':' ').
857: &mt('Your receipt no. is [_1]',
858: (&Apache::lonnet::receipt($Apache::inputtags::part).
859: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
860: }
861: }
862: }
863: $button=0;
864: $previousmsg='';
865: } elsif ($solved =~ /^excused/) {
866: if ($target eq 'tex') {
867: $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
868: } else {
869: $message = "<b>".&mt('You are excused from the problem.')."</b>";
870: }
871: $css_class=$possible_class{'charged_try'};
872: $button=0;
873: $previousmsg='';
874: } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
875: if ($solved =~ /^incorrect/ || $solved eq '') {
876: $message = &mt("Incorrect").".";
877: $css_class=$possible_class{'charged_try'};
878: $button=1;
879: } else {
880: if ($target eq 'tex') {
881: $message = '\textbf{'.&mt('You are correct.').'}';
882: } else {
883: $message = "<b>".&mt('You are correct.')."</b>";
884: $message.= $computer;
885: }
886: $added_computer_text=1;
887: if ($awarded > 0
888: && $env{'course.'.
889: $env{'request.course.id'}.
890: '.disable_receipt_display'} ne 'yes') {
891: $message.=(($target eq 'web')?'<br />':' ').
892: &mt('Your receipt is [_1]',
893: (&Apache::lonnet::receipt($Apache::inputtags::part).
894: (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
895: }
896: $css_class=$possible_class{'correct'};
897: $button=0;
898: $previousmsg='';
899: }
900: } elsif ($award eq 'NO_RESPONSE') {
901: $message = '';
902: $css_class=$possible_class{'no_feedback'};
903: $button=1;
904: } elsif ($award eq 'EXTRA_ANSWER') {
905: $message = &mt('Some extra items were submitted.');
906: $css_class=$possible_class{'not_charged_try'};
907: $button = 1;
908: } elsif ($award eq 'MISSING_ANSWER') {
909: $message = &mt('Some items were not submitted.');
910: if ($target ne 'tex') {
911: $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
912: }
913: $css_class=$possible_class{'not_charged_try'};
914: $button = 1;
915: } elsif ($award eq 'ERROR') {
916: $message = &mt('An error occurred while grading your answer.');
917: $css_class=$possible_class{'not_charged_try'};
918: $button = 1;
919: } elsif ($award eq 'TOO_LONG') {
920: $message = &mt("The submitted answer was too long.");
921: $css_class=$possible_class{'not_charged_try'};
922: $button=1;
923: } elsif ($award eq 'WANTED_NUMERIC') {
924: $message = &mt("This question expects a numeric answer.");
925: $css_class=$possible_class{'not_charged_try'};
926: $button=1;
927: } elsif ($award eq 'MISORDERED_RANK') {
928: $message = &mt('You have provided an invalid ranking.');
929: if ($target ne 'tex') {
930: $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
931: }
932: $css_class=$possible_class{'not_charged_try'};
933: $button=1;
934: } elsif ($award eq 'EXCESS_FILESIZE') {
935: $message = &mt('Submission won\'t be graded. The combined size of submitted files exceeded the amount allowed.');
936: $css_class=$possible_class{'not_charged_try'};
937: $button=1;
938: } elsif ($award eq 'FILENAME_INUSE') {
939: $message = &mt('You have already uploaded a file with that filename.');
940: if ($target eq 'tex') {
941: $message.= "\\\\\n";
942: } else {
943: $message .= '<br />';
944: }
945: $message .= &mt('Please use a different file name.');
946: $css_class=$possible_class{'not_charged_try'};
947: $button=1;
948: } elsif ($award eq 'INVALID_FILETYPE') {
949: $message = &mt("Submission won't be graded. The type of file submitted is not allowed.");
950: $css_class=$possible_class{'not_charged_try'};
951: $button=1;
952: } elsif ($award eq 'SIG_FAIL') {
953: my ($used,$min,$max)=split(':',$awardmsg);
954: my $word = ($used < $min) ? 'more' : 'fewer';
955: $message = &mt("Submission not graded. Use $word digits.",$used);
956: $css_class=$possible_class{'not_charged_try'};
957: $button=1;
958: } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
959: $message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
960: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
961: $css_class=$possible_class{'not_charged_try'};
962: $button=1;
963: } elsif ($award eq 'UNIT_INVALID_STUDENT') {
964: $message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
965: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
966: $css_class=$possible_class{'not_charged_try'};
967: $button=1;
968: } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
969: $message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
970: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');}
971: $css_class=$possible_class{'not_charged_try'};
972: $button=1;
973: } elsif ($award eq 'UNIT_NOTNEEDED') {
974: $message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
975: $css_class=$possible_class{'not_charged_try'};
976: $button=1;
977: } elsif ($award eq 'NO_UNIT') {
978: $message = &mt("Units required").'.';
979: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
980: $css_class=$possible_class{'not_charged_try'};
981: $button=1;
982: } elsif ($award eq 'COMMA_FAIL') {
983: $message = &mt("Proper comma separation is required").'.';
984: $css_class=$possible_class{'not_charged_try'};
985: $button=1;
986: } elsif ($award eq 'BAD_FORMULA') {
987: $message = &mt("Unable to understand formula").'.';
988: if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
989: $css_class=$possible_class{'not_charged_try'};
990: $button=1;
991: } elsif ($award eq 'INTERNAL_ERROR') {
992: $message = &mt("An internal error occurred while processing your answer. Please try again later.");
993: $css_class=$possible_class{'not_charged_try'};
994: $button=1;
995: } elsif ($award eq 'INCORRECT') {
996: $message = &mt("Incorrect").'.';
997: $css_class=$possible_class{'charged_try'};
998: $button=1;
999: } elsif ($award eq 'SUBMITTED') {
1000: $message = &mt("Your submission has been recorded.");
1001: $css_class=$possible_class{'no_grade'};
1002: $button=1;
1003: } elsif ($award eq 'SUBMITTED_CREDIT') {
1004: $message = &mt("Your submission has been recorded, and credit awarded.");
1005: $css_class=$possible_class{'correct'};
1006: $button=1;
1007: } elsif ($award eq 'ANONYMOUS') {
1008: $message = &mt("Your anonymous submission has been recorded.");
1009: $css_class=$possible_class{'no_grade'};
1010: $button=1;
1011: } elsif ($award eq 'ANONYMOUS_CREDIT') {
1012: $message = &mt("Your anonymous submission has been recorded, and credit awarded.");
1013: $css_class=$possible_class{'correct'};
1014: } elsif ($award eq 'DRAFT') {
1015: $message = &mt("Copy saved but not submitted.");
1016: $css_class=$possible_class{'not_charged_try'};
1017: $button=1;
1018: } elsif ($award eq 'ASSIGNED_SCORE') {
1019: $message = &mt("A score has been assigned.");
1020: $css_class=$possible_class{'correct'};
1021: $button=0;
1022: } elsif ($award eq '') {
1023: if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
1024: $message = &mt("Nothing submitted.");
1025: $css_class=$possible_class{'charged_try'};
1026: } else {
1027: $css_class=$possible_class{'not_charged_try'};
1028: }
1029: $button=1;
1030: } else {
1031: $message = &mt("Unknown message").": $award";
1032: $button=1;
1033: }
1034: my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1035: foreach my $resid(@Apache::inputtags::response){
1036: if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1037: if ($target eq 'tex') {
1038: $message.= "\\\\\n";
1039: } else {
1040: $message.='<br />';
1041: }
1042: my @files = split(/\s*,\s*/,
1043: $Apache::lonhomework::history{"resource.$part.$resid.handback"});
1044: my $file_msg;
1045: foreach my $file (@files) {
1046: if ($target eq 'tex') {
1047: $file_msg.= "\\\\\n".$file;
1048: } else {
1049: $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
1050: }
1051: }
1052: $message .= &mt('Returned file(s): [_1]',$file_msg);
1053: if ($target eq 'tex') {
1054: $message.= "\\\\\n";
1055: } else {
1056: $message.='<br />';
1057: }
1058: }
1059: }
1060:
1061: if (&Apache::lonhomework::hide_problem_status()
1062: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
1063: && &hide_award($award)) {
1064: $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
1065: $css_class=$possible_class{'no_grade'};
1066: $button=1;
1067: }
1068: if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' &&
1069: !$added_computer_text && $target ne 'tex') {
1070: $message.= $computer;
1071: $added_computer_text=1;
1072: }
1073: if ($Apache::lonhomework::type eq 'practice') {
1074: if ($target eq 'web') {
1075: $message .= '<br />';
1076: } else {
1077: $message .= ' ';
1078: }
1079: $message.=&mt('Submissions to practice problems are not permanently recorded.');
1080: }
1081:
1082: return ($button,$css_class,$message,$previousmsg);
1083: }
1084:
1085: sub markup_unit {
1086: my ($unit,$target)=@_;
1087: if ($target eq 'tex') {
1088: return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}';
1089: } else {
1090: return "<tt>".$unit."</tt>";
1091: }
1092: }
1093:
1094: sub removealldata {
1095: my ($id)=@_;
1096: foreach my $key (keys(%Apache::lonhomework::results)) {
1097: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
1098: &Apache::lonxml::debug("Removing $key");
1099: delete($Apache::lonhomework::results{$key});
1100: }
1101: }
1102: }
1103:
1104: sub hidealldata {
1105: my ($id)=@_;
1106: foreach my $key (keys(%Apache::lonhomework::results)) {
1107: if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
1108: &Apache::lonxml::debug("Hidding $key");
1109: my $newkey=$key;
1110: $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
1111: $Apache::lonhomework::results{$newkey}=
1112: $Apache::lonhomework::results{$key};
1113: delete($Apache::lonhomework::results{$key});
1114: }
1115: }
1116: }
1117:
1118: sub setgradedata {
1119: my ($award,$msg,$id,$previously_used) = @_;
1120: if ($Apache::lonhomework::scantronmode &&
1121: &Apache::lonnet::validCODE($env{'form.CODE'})) {
1122: $Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1123: } elsif ($Apache::lonhomework::scantronmode &&
1124: $env{'form.CODE'} eq '' &&
1125: $Apache::lonhomework::history{"resource.CODE"} ne '') {
1126: $Apache::lonhomework::results{"resource.CODE"}='';
1127: }
1128:
1129: if (!$Apache::lonhomework::scantronmode &&
1130: $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1131: $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
1132: $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1133: return '';
1134: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1135: /^correct/
1136: || $Apache::lonhomework::scantronmode
1137: || &Apache::lonhomework::hide_problem_status() ) {
1138: # the student doesn't already have it correct,
1139: # or we are in a mode (scantron orno problem status) where a correct
1140: # can become incorrect
1141: # handle assignment of tries and solved status
1142: my $solvemsg;
1143: if ($Apache::lonhomework::scantronmode) {
1144: $solvemsg='correct_by_scantron';
1145: } else {
1146: $solvemsg='correct_by_student';
1147: }
1148: if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
1149: $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
1150: }
1151: if ( $award eq 'ASSIGNED_SCORE') {
1152: $Apache::lonhomework::results{"resource.$id.tries"} =
1153: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1154: $Apache::lonhomework::results{"resource.$id.solved"} =
1155: $solvemsg;
1156: my $numawards=scalar(@Apache::inputtags::response);
1157: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1158: foreach my $res (@Apache::inputtags::response) {
1159: if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
1160: $Apache::lonhomework::results{"resource.$id.awarded"}+=
1161: $Apache::lonhomework::results{"resource.$id.$res.awarded"};
1162: } else {
1163: $Apache::lonhomework::results{"resource.$id.awarded"}+=
1164: &awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
1165: }
1166: }
1167: if ($numawards > 0) {
1168: $Apache::lonhomework::results{"resource.$id.awarded"}/=
1169: $numawards;
1170: }
1171: } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
1172: $Apache::lonhomework::results{"resource.$id.tries"} =
1173: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1174: $Apache::lonhomework::results{"resource.$id.solved"} =
1175: $solvemsg;
1176: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1177: } elsif ( $award eq 'SUBMITTED_CREDIT' ) {
1178: $Apache::lonhomework::results{"resource.$id.tries"} =
1179: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1180: $Apache::lonhomework::results{"resource.$id.solved"} =
1181: 'credit_attempted';
1182: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1183: } elsif ( $award eq 'ANONYMOUS_CREDIT' ) {
1184: $Apache::lonhomework::results{"resource.$id.tries"} =
1185: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1186: $Apache::lonhomework::results{"resource.$id.solved"} =
1187: 'credit_attempted';
1188: $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1189: } elsif ( $award eq 'INCORRECT' ) {
1190: $Apache::lonhomework::results{"resource.$id.tries"} =
1191: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1192: if (&Apache::lonhomework::hide_problem_status()
1193: || $Apache::lonhomework::scantronmode) {
1194: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1195: }
1196: $Apache::lonhomework::results{"resource.$id.solved"} =
1197: 'incorrect_attempted';
1198: } elsif ( $award eq 'SUBMITTED' ) {
1199: $Apache::lonhomework::results{"resource.$id.tries"} =
1200: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1201: $Apache::lonhomework::results{"resource.$id.solved"} =
1202: 'ungraded_attempted';
1203: } elsif ( $award eq 'ANONYMOUS' ) {
1204: $Apache::lonhomework::results{"resource.$id.tries"} =
1205: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1206: $Apache::lonhomework::results{"resource.$id.solved"} =
1207: 'ungraded_attempted';
1208: } elsif ( $award eq 'DRAFT' ) {
1209: $Apache::lonhomework::results{"resource.$id.solved"} = '';
1210: } elsif ( $award eq 'NO_RESPONSE' ) {
1211: #no real response so delete any data that got stored
1212: &removealldata($id);
1213: return '';
1214: } else {
1215: $Apache::lonhomework::results{"resource.$id.solved"} =
1216: 'incorrect_attempted';
1217: if (&Apache::lonhomework::show_no_problem_status()
1218: || $Apache::lonhomework::scantronmode) {
1219: $Apache::lonhomework::results{"resource.$id.tries"} =
1220: $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1221: $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1222: }
1223:
1224: if (&Apache::lonhomework::show_some_problem_status()) {
1225: # clear out the awarded if they had gotten it wrong/right
1226: # and are now in an error mode
1227: $Apache::lonhomework::results{"resource.$id.awarded"} = '';
1228: }
1229: }
1230: if (defined($msg)) {
1231: $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
1232: }
1233: # did either of the overall awards chage? If so ignore the
1234: # previous check
1235: if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
1236: $Apache::lonhomework::history{"resource.$id.awarded"}) &&
1237: ($Apache::lonhomework::results{"resource.$id.solved"} eq
1238: $Apache::lonhomework::history{"resource.$id.solved"})) {
1239: # check if this was a previous submission if it was delete the
1240: # unneeded data and update the previously_used attribute
1241: if ( $previously_used eq 'PREVIOUSLY_USED') {
1242: if (&Apache::lonhomework::show_problem_status()) {
1243: delete($Apache::lonhomework::results{"resource.$id.tries"});
1244: $Apache::lonhomework::results{"resource.$id.previous"} = '1';
1245: }
1246: } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1247: #delete all data as they student didn't do anything, but save
1248: #the list of collaborators.
1249: &removealldata($id);
1250: #and since they didn't do anything we were never here
1251: return '';
1252: } else {
1253: $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1254: }
1255: }
1256: } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
1257: /^correct/ ) {
1258: #delete all data as they student already has it correct
1259: &removealldata($id);
1260: #and since they didn't do anything we were never here
1261: return '';
1262: }
1263: $Apache::lonhomework::results{"resource.$id.award"} = $award;
1264: if ($award eq 'SUBMITTED') {
1265: &Apache::response::add_to_gradingqueue();
1266: }
1267: if (($Apache::lonhomework::type eq 'anonsurvey') ||
1268: ($Apache::lonhomework::type eq 'anonsurveycred') ||
1269: ($Apache::lonhomework::type eq 'randomizetry')) {
1270: $Apache::lonhomework::results{"resource.$id.type"} = $Apache::lonhomework::type;
1271: }
1272: if ($Apache::lonhomework::type eq 'randomizetry') {
1273: $Apache::lonhomework::results{"resource.$id.rndseed"} = $env{'form.'.$id.'.rndseed'};
1274: }
1275: }
1276:
1277: sub find_which_previous {
1278: my ($version) = @_;
1279: my $part = $Apache::inputtags::part;
1280: my (@previous_version);
1281: foreach my $resp (@Apache::inputtags::response) {
1282: my $key = "$version:resource.$part.$resp.submission";
1283: my $submission = $Apache::lonhomework::history{$key};
1284: my %previous = &Apache::response::check_for_previous($submission,
1285: $part,$resp,
1286: $version);
1287: push(@previous_version,$previous{'version'});
1288: }
1289: return &previous_match(\@previous_version,
1290: scalar(@Apache::inputtags::response));
1291: }
1292:
1293: sub previous_match {
1294: my ($previous_array,$count) = @_;
1295: my $match = 0;
1296: my @matches;
1297: foreach my $versionar (@$previous_array) {
1298: foreach my $version (@$versionar) {
1299: $matches[$version]++;
1300: }
1301: }
1302: my $which=0;
1303: foreach my $elem (@matches) {
1304: if ($elem eq $count) {
1305: $match=1;
1306: last;
1307: }
1308: $which++;
1309: }
1310: return ($match,$which);
1311: }
1312:
1313: sub grade {
1314: my ($target) = @_;
1315: my $id = $Apache::inputtags::part;
1316: my $response='';
1317: if ( defined $env{'form.submitted'}) {
1318: my (@awards,@msgs);
1319: foreach $response (@Apache::inputtags::response) {
1320: &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
1321: my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1322: &Apache::lonxml::debug("keeping $value from $response for $id");
1323: push (@awards,$value);
1324: $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
1325: &Apache::lonxml::debug("got message $value from $response for $id");
1326: push (@msgs,$value);
1327: }
1328: my ($finalaward,$msg) =
1329: &finalizeawards(\@awards,\@msgs,undef,undef,
1330: $Apache::lonhomework::scantronmode);
1331: my $previously_used;
1332: if ( $#Apache::inputtags::previous eq $#awards ) {
1333: my ($match) =
1334: &previous_match(\@Apache::inputtags::previous_version,
1335: scalar(@Apache::inputtags::response));
1336:
1337: if ($match) {
1338: $previously_used = 'PREVIOUSLY_LAST';
1339: foreach my $value (@Apache::inputtags::previous) {
1340: if ($value eq 'PREVIOUSLY_USED' ) {
1341: $previously_used = $value;
1342: last;
1343: }
1344: }
1345: }
1346: }
1347: &Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
1348: &setgradedata($finalaward,$msg,$id,$previously_used);
1349: }
1350: return '';
1351: }
1352:
1353: sub get_grade_messages {
1354: my ($id,$prefix,$target,$status) = @_;
1355:
1356: my ($message,$latemessage,$trystr,$previousmsg);
1357: my $showbutton = 1;
1358:
1359: my $award = $Apache::lonhomework::history{"$prefix.award"};
1360: my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
1361: my $solved = $Apache::lonhomework::history{"$prefix.solved"};
1362: my $previous = $Apache::lonhomework::history{"$prefix.previous"};
1363: my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
1364: &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1365: if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1366: &Apache::lonxml::debug('Getting message');
1367: ($showbutton,my $css_class,$message,$previousmsg) =
1368: &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1369: $target);
1370: if ($target eq 'tex') {
1371: $message='\vskip 2 mm '.$message.' ';
1372: } else {
1373: $message="<td class=\"$css_class\">$message</td>";
1374: if ($previousmsg) {
1375: $previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1376: }
1377: }
1378: }
1379: my $tries = $Apache::lonhomework::history{"$prefix.tries"};
1380: my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1381: &Apache::lonxml::debug("got maxtries of :$maxtries:");
1382: #if tries are set to negative turn off the Tries/Button and messages
1383: if (defined($maxtries) && $maxtries < 0) { return ''; }
1384: if ( $tries eq '' ) { $tries = '0'; }
1385: if ( $maxtries eq '' ) { $maxtries = '2'; }
1386: if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; }
1387: my $tries_text= &get_tries_text();;
1388: if ($showbutton) {
1389: if ($target eq 'tex') {
1390: if ($env{'request.state'} ne "construct"
1391: && $Apache::lonhomework::type ne 'exam'
1392: && $env{'form.suppress_tries'} ne 'yes') {
1393: $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.
1394: $tries.'/'.$maxtries.'} \vskip 2 mm ';
1395: } else {
1396: $trystr = '\vskip 0 mm ';
1397: }
1398: } else {
1399: $trystr = '<td><span class="LC_nobreak">'.&mt($tries_text)." $tries";
1400: if ($Apache::lonhomework::parsing_a_task) {
1401: } elsif($env{'request.state'} ne 'construct') {
1402: $trystr.="/$maxtries";
1403: } else {
1404: if (defined($Apache::inputtags::params{'maxtries'})) {
1405: $trystr.="/".$Apache::inputtags::params{'maxtries'};
1406: }
1407: }
1408: $trystr.="</span></td>";
1409: }
1410: }
1411:
1412: if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
1413: #last submissions was after due date
1414: $latemessage=&mt(' The last submission was after the Due Date ');;
1415: if ($target eq 'web') {
1416: $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1417: }
1418: }
1419: return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
1420: }
1421:
1422: sub gradestatus {
1423: my ($id,$target,$no_previous) = @_;
1424: my $showbutton = 1;
1425: my $message = '';
1426: my $latemessage = '';
1427: my $trystr='';
1428: my $button='';
1429: my $previousmsg='';
1430:
1431: my $status = $Apache::inputtags::status['-1'];
1432: &Apache::lonxml::debug("gradestatus has :$status:");
1433: if ( $status ne 'CLOSED'
1434: && $status ne 'UNAVAILABLE'
1435: && $status ne 'INVALID_ACCESS'
1436: && $status ne 'NEEDS_CHECKIN'
1437: && $status ne 'NOT_IN_A_SLOT') {
1438:
1439: ($previousmsg,$latemessage,$message,$trystr) =
1440: &get_grade_messages($id,"resource.$id",$target,$status,
1441: $showbutton);
1442: if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1443: $showbutton = 0;
1444: }
1445: if ( $status eq 'SHOW_ANSWER') {
1446: undef($previousmsg);
1447: }
1448: if ( $showbutton ) {
1449: if ($target ne 'tex') {
1450: $button =
1451: '<input onmouseup="javascript:setSubmittedPart(\''.$id.'\');this.form.action+=\'#'.&escape($id).'\';"
1452: type="submit" name="submit_'.$id.'"
1453: value="'.&mt('Submit Answer').'" />';
1454: }
1455: }
1456:
1457: }
1458: my $output= $previousmsg.$latemessage.$message.$trystr;
1459: if ($output =~ /^\s*$/) {
1460: return $button;
1461: } else {
1462: if ($target eq 'tex') {
1463: return $button.' \vskip 0 mm '.$output.' ';
1464: } else {
1465: $output =
1466: '<table><tr><td>'.$button.'</td>'.$output;
1467: if (!$no_previous) {
1468: $output.='<td>'.&previous_tries($id,$target).'</td>';
1469: }
1470: $output.= '</tr></table>';
1471: return $output;
1472: }
1473: }
1474: }
1475:
1476: sub previous_tries {
1477: my ($id,$target) = @_;
1478: my $output;
1479: my $status = $Apache::inputtags::status['-1'];
1480:
1481: my $count;
1482: my %count_lookup;
1483: my $lastrndseed;
1484:
1485: foreach my $i (1..$Apache::lonhomework::history{'version'}) {
1486: my $prefix = $i.":resource.$id";
1487: my $is_anon;
1488: if (defined($env{'form.grade_symb'})) {
1489: if (($Apache::lonhomework::history{"$prefix.type"} eq 'anonsurvey') ||
1490: ($Apache::lonhomework::history{"$prefix.type"} eq 'anonsurveycred')) {
1491: $is_anon = 1;
1492: }
1493: }
1494: next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1495: $count++;
1496: $count_lookup{$i} = $count;
1497: my $curr_rndseed = $Apache::lonhomework::history{"$prefix.rndseed"};
1498:
1499: my ($previousmsg,$latemessage,$message,$trystr);
1500:
1501: ($previousmsg,$latemessage,$message,$trystr) =
1502: &get_grade_messages($id,"$prefix",$target,$status);
1503:
1504: if ($previousmsg ne '') {
1505: my ($match,$which) = &find_which_previous($i);
1506: $message=$previousmsg;
1507: my $previous = $count_lookup{$which};
1508: $message =~ s{(</td>)}{ as submission \# $previous $1};
1509: } elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1510: if (!(&Apache::lonhomework::hide_problem_status()
1511: && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
1512: && $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
1513: ) {
1514:
1515: my $txt_correct = &mt('Correct');
1516: $message =~ s{(<td.*?>)(.*?)(</td>)}
1517: {$1 <strong>$txt_correct</strong>. $3}s;
1518: }
1519: my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1520: if ($curr_rndseed || $lastrndseed) {
1521: if ($curr_rndseed ne $lastrndseed) {
1522: $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>';
1523: }
1524: }
1525: $message =~ s{(</td>)}{ $trystr $1};
1526: }
1527: my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
1528: $message =~ s{(<td.*?>)}{<td>};
1529:
1530:
1531: $output.='<tr class="'.$class.'">';
1532: $output.='<td align="center">'.$count.'</td>';
1533: $output.=$message;
1534:
1535: foreach my $resid (@Apache::inputtags::response) {
1536: my $prefix = $prefix.".$resid";
1537: if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
1538: my $submission =
1539: $Apache::inputtags::submission_display{"$prefix.submission"};
1540: if (!defined($submission)) {
1541: $submission =
1542: $Apache::lonhomework::history{"$prefix.submission"};
1543: }
1544: if ($is_anon) {
1545: $output.='<td>'.&mt('(only shown to submitter)').'</td>';
1546: } else {
1547: $output.='<td>'.$submission.'</td>';
1548: }
1549: } else {
1550: $output.='<td></td>';
1551: }
1552: }
1553: $output.=&Apache::loncommon::end_data_table_row()."\n";
1554: $lastrndseed = $curr_rndseed;
1555: }
1556: return if ($output eq '');
1557: my $headers =
1558: '<tr>'.'<th>'.&mt('Submission #').'</th><th>'.&mt('Try').
1559: '</th><th colspan="'.scalar(@Apache::inputtags::response).'">'.
1560: &mt('Submitted Answer').'</th>';
1561: $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1562: #return $output;
1563: $output = &Apache::loncommon::js_ready($output);
1564: $output.='<br /><form action=""><center><input type="button" name="close" value="'.&mt('Close Window').'" onClick="window.close()" /></center></form>';
1565:
1566: my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1567: my $tries_text = &get_tries_text('link');
1568: my $start_page =
1569: &Apache::loncommon::start_page($tries_text, undef,
1570: {'only_body' => 1,
1571: 'bgcolor' => '#FFFFFF',
1572: 'js_ready' => 1,
1573: 'inherit_jsmath' => 1, });
1574: my $end_page =
1575: &Apache::loncommon::end_page({'js_ready' => 1,});
1576: my $prefix = $env{'form.request.prefix'};
1577: $prefix =~ tr{.}{_};
1578: my $function_name = "LONCAPA_previous_tries_".$prefix.
1579: $Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1580: my $result ="<script type=\"text/javascript\">
1581: // <![CDATA[
1582: function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page $output $end_page');newWindow.document.close();newWindow.focus()}
1583: // ]]>
1584: </script><a href=\"javascript:$function_name();void(0);\">".&mt($tries_text)."</a><br />";
1585: #use Data::Dumper;
1586: #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display));
1587: return $result;
1588: }
1589:
1590: sub get_tries_text {
1591: my ($context) = @_;
1592: my $tries_text;
1593: if ($context eq 'link') {
1594: $tries_text = 'Previous Tries';
1595: } else {
1596: $tries_text = 'Tries';
1597: }
1598: if ( $Apache::lonhomework::type eq 'survey' ||
1599: $Apache::lonhomework::type eq 'surveycred' ||
1600: $Apache::lonhomework::type eq 'anonsurvey' ||
1601: $Apache::lonhomework::type eq 'anonsurveycred' ||
1602: $Apache::lonhomework::parsing_a_task) {
1603: if ($context eq 'link') {
1604: $tries_text = 'Previous Submissions';
1605: } else {
1606: $tries_text = 'Submissions';
1607: }
1608: }
1609: return $tries_text;
1610: }
1611:
1612: 1;
1613: __END__
1614:
1615: =pod
1616:
1617: =back
1618:
1619: =cut
1620:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>