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