1: # The LearningOnline Network with CAPA
2: # essay (ungraded) style responses
3: #
4: # $Id: essayresponse.pm,v 1.92 2008/11/20 14:37:37 jms 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:
29: package Apache::essayresponse;
30: use strict;
31: use Apache::lonxml();
32: use Apache::lonnet;
33: use Apache::lonlocal;
34: use LONCAPA qw(:DEFAULT :match);
35:
36:
37: BEGIN {
38: &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
39: }
40:
41: sub start_essayresponse {
42: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
43: my $result;
44: my $id = &Apache::response::start_response($parstack,$safeeval);
45: if ($target eq 'meta') {
46: $result=&Apache::response::meta_package_write('essayresponse');
47: } elsif ($target eq 'web' &&
48: $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
49: my $part= $Apache::inputtags::part;
50: my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
51: my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
52: my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
53: $uploadedfiletypes=~s/[^\w\,]//g;
54: if ( $Apache::lonhomework::type eq 'survey' ) {
55: $result.= '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" /> ';
56: }
57: $result.='<br /><table border="1">';
58: if ( $Apache::lonhomework::type ne 'survey' ) {
59: $result.= '<tr><td>'.
60: '<label>'.
61: '<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" /> '.
62: &mt('Submit entries below as answer to receive credit').
63: '</label> <br />'.
64: '<label>'.
65: '<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" /> '.
66: &mt('Save entries below as a draft answer (not submitting them for credit yet)').
67: '</label>'.
68: '</td></tr>';
69: }
70:
71: if ($ncol > 0) {
72: $result .='<tr><td>'.'<label>'.
73: &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
74: $part.'_'.$id.'" value="'.$coll.'" /><br />'.
75: &mt('(Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].)',$ncol,$env{'user.domain'});
76: if ($ncol > 1) {
77: $result .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
78: }
79: $result .= '</label><br />';
80: $result .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
81: $result .='</td></tr>';
82: }
83: $result.=&Apache::inputtags::file_selector($part,$id,
84: $uploadedfiletypes,'both');
85: $result.='</table>';
86: } elsif ($target eq 'web' &&
87: $Apache::inputtags::status[-1] ne 'CAN_ANSWER') {
88: my $part= $Apache::inputtags::part;
89: my @msgs;
90: if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
91: my $coll= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
92: $result .= '<td>'.&mt('Collaborated with [_1]',$coll).'</td>';
93: }
94:
95: my $file_submission =
96: &Apache::inputtags::show_past_file_submission($part,$id);
97: if ($file_submission) {
98: $result .= '<td>'.$file_submission.'</td>';
99: }
100:
101: my $port_submission =
102: &Apache::inputtags::show_past_portfile_submission($part,$id);
103: if ($port_submission) {
104: $result .= '<td>'.$port_submission.'</td>';
105: }
106:
107: if ($result ne '') {
108: $result =
109: '<table class="LC_pastsubmission"><tr>'.$result.
110: '</tr></table>';
111: }
112: }
113: return $result;
114: }
115:
116: sub end_essayresponse {
117: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
118: my $part = $Apache::inputtags::part;
119: my $id = $Apache::inputtags::response[-1];
120: my $increment = &Apache::response::repetition();
121: my $result;
122: if ( $target eq 'grade' ) {
123: my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};
124: if ($collaborators =~ /[^\s]/) {
125: my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
126: $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators
127: if ($collaborators ne $previous_list);
128: }
129: if ( &Apache::response::submitted('scantron') ) {
130: $increment=&Apache::response::scored_response($part,$id);
131: } elsif ( &Apache::response::submitted() ) {
132: my $response = $env{'form.HWVAL_'.$id};
133: my $filename = $env{'form.HWFILE'.$part.'_'.$id.'.filename'} ||
134: $env{'form.HWFILETOOBIG'.$part.'_'.$id};
135: my $portfiles = $env{'form.HWPORT'.$part.'_'.$id};
136: if (( $response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/)) {
137: my $award='DRAFT';
138: if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
139: $award='SUBMITTED';
140: }
141: my $uploadedflag=0;
142: my $totalsize=0;
143: &file_submission($part,$id,'filename',\$award,\$uploadedflag,\$totalsize);
144: &file_submission($part,$id,'portfiles',\$award,\$uploadedflag,\$totalsize);
145: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
146: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
147: my %previous=&Apache::response::check_for_previous($response,$part,$id);
148: unless ($uploadedflag) { &Apache::response::handle_previous(\%previous,$award); }
149: #
150: # Store with resource author for similarity testing
151: #
152: if ($award eq 'SUBMITTED') {
153: my ($symb,$crsid,$domain,$name)=
154: &Apache::lonnet::whichuser();
155: if ($crsid) {
156: my $akey=join('.',&escape($name),&escape($domain),
157: &escape($crsid));
158: my $essayurl=
159: &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
160: my ($adom,$aname,$apath)=
161: ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
162: $apath=&escape($apath);
163: $apath=~s/\W/\_/gs;
164: &Apache::lonnet::put('nohist_essay_'.$apath,
165: { $akey => $response },$adom,$aname);
166: }
167: }
168: }
169: }
170: } elsif ($target eq 'edit') {
171: $result.=&Apache::edit::end_table();
172:
173: } elsif ($target eq 'tex'
174: && $Apache::lonhomework::type eq 'exam') {
175: $result .= &Apache::inputtags::exam_score_line($target);
176:
177: } elsif ($target eq 'answer') {
178: $result.=&Apache::response::answer_header($$tagstack[-1]);
179: my $answer = &mt('Essay will be hand graded.');
180: $result.=&Apache::response::answer_part($$tagstack[-1],$answer,
181: {'no_verbatim' => 1});
182: $result.=&Apache::response::answer_footer($$tagstack[-1]);
183: }
184: if ($target eq 'web') {
185: &Apache::response::setup_prior_tries_hash(\&format_prior_response,
186: ['portfiles',
187: 'uploadedurl']);
188: }
189:
190: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
191: $target eq 'tex' || $target eq 'analyze') {
192: &Apache::lonxml::increment_counter($increment, "$part.$id");
193:
194: if ($target eq 'analyze') {
195: $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
196: &Apache::lonhomework::set_bubble_lines();
197: }
198: }
199: &Apache::response::end_response;
200:
201: return $result;
202: }
203:
204: sub format_prior_response {
205: my ($mode,$answer,$other_data) = @_;
206: my $output;
207:
208: my (undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
209: my $port_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
210:
211: my $file_list;
212:
213: foreach my $file (split(/\s*,\s*/,
214: $other_data->[0].','.$other_data->[1])) {
215: next if ($file!~/\S/);
216: if ($file !~ m{^/uploaded/}) { $file=$port_url.$file; }
217: $file=~s|/+|/|g;
218: &Apache::lonnet::allowuploaded('/adm/essayresponse',$file);
219: $file_list.='<li><span class="LC_nobreak"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
220: &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
221: '</a></span></li>'."\n";
222: }
223: if ($file_list) {
224: $output.= &mt('Submitted Files').'<ul>'.$file_list.'</ul>';
225: }
226: if ($answer =~ /\S/) {
227: $output.='<p>'.&mt('Submitted text').
228: '<blockquote>'.$answer.'</blockquote></p>';
229: }
230:
231: return '<div class="LC_prior_essay">'.$output.'</div>';
232: }
233:
234: sub file_submission {
235: my ($part,$id,$which,$award,$uploadedflag,$totalsize)=@_;
236: my $files;
237: my $jspart=$part;
238: $jspart=~s/\./_/g;
239: if ($which eq 'portfiles') {
240: $files= $env{'form.HWPORT'.$jspart.'_'.$id};
241: } elsif ($which eq 'filename') {
242: if ($env{'form.HWFILETOOBIG'.$jspart.'_'.$id} ne '') {
243: $$award = 'EXCESS_FILESIZE';
244: return;
245: } else {
246: $files = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
247: }
248: }
249: if ($files =~ /[^\s]/) {
250: $files =~s/,$//;
251: my (@submitted_files,@acceptable_files,@accepted_files);
252: if ($which eq 'portfiles') {
253: @submitted_files = split(/\s*,\s*/,$files);
254: } else {
255: @submitted_files = ($files);
256: }
257: my $uploadedfiletypes=
258: &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
259: if ($uploadedfiletypes) {
260: $uploadedfiletypes=~s/[^\w\,]//g;
261: $uploadedfiletypes=','.$uploadedfiletypes.',';
262: foreach my $file (@submitted_files) {
263: my ($extension)=($file=~/\.(\w+)$/);
264: if ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
265: push(@acceptable_files,$file);
266: } else {
267: $$award='INVALID_FILETYPE';
268: if ($which eq 'filename') {
269: &delete_form_items($jspart,$id);
270: }
271: }
272: }
273: }
274: my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
275: if (!$maxfilesize) {
276: $maxfilesize = 100.0; #FIXME This should become a domain configuration
277: }
278: my %dirlist;
279: foreach my $file (@acceptable_files) {
280: if ($which eq 'filename') {
281: if (ref($totalsize)) {
282: $$totalsize += $env{'form.HWFILESIZE'.$jspart.'_'.$id};
283: }
284: } else {
285: my ($symb,$crsid,$udom,$uname) = &Apache::lonnet::whichuser();
286: my ($path,$filename) = ($file =~ m{^(.+)/([^/]+)$});
287: my $fullpath = '/userfiles/portfolio'.$path;
288: if (!exists($dirlist{$fullpath})) {
289: my @list = &Apache::lonnet::dirlist($fullpath,$udom,$uname,1);
290: foreach my $dir_line (@list) {
291: my ($fname,$dom,undef,$testdir,undef,undef,undef,undef,
292: $size,undef,$mtime,undef,undef,undef,$obs,undef) =
293: split(/\&/,$dir_line,16);
294: if ($filename eq $fname) {
295: my $mbsize = $size/(1024.0*1024.0);
296: if (ref($totalsize)) {
297: $$totalsize += $mbsize;
298: }
299: last;
300: }
301: }
302: $dirlist{$fullpath} = \@list;
303: }
304: }
305: if (ref($totalsize)) {
306: if ($$totalsize > $maxfilesize) {
307: $$award='EXCESS_FILESIZE';
308: if ($which eq 'filename') {
309: &delete_form_items($jspart,$id);
310: }
311: }
312: } else {
313: push(@accepted_files,$file);
314: }
315: }
316: my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
317: $Apache::lonhomework::results{"resource.$part.$id.$which"}=join(',',@accepted_files);
318: if (($$award eq 'INVALID_FILETYPE') || ($award eq 'EXCESS_FILESIZE')) {
319: return;
320: }
321: if (ref($uploadedflag)) {
322: $$uploadedflag=1;
323: }
324: if ($which eq 'portfiles') {
325: my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
326: &Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
327: &Apache::lonnet::mark_as_readonly($domain,$name,\@submitted_files,[$symb,$crsid]);
328: &Apache::lonnet::clear_selected_files($name);
329: }
330: if ($which eq 'filename') {
331: $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"}=
332: $files;
333: $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"}=
334: &Apache::lonnet::userfileupload('HWFILE'.$jspart.'_'.$id,undef,
335: 'essayresponse');
336: &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id});
337: }
338: } elsif ($which eq 'portfiles' &&
339: $Apache::lonhomework::history{"resource.$part.$id.$which"}) {
340: my ($symb,$crsid,$domain,$name)=&Apache::lonnet::whichuser();
341: &Apache::lonnet::unmark_as_readonly($domain,$name,[$symb,$crsid]);
342: $Apache::lonhomework::results{"resource.$part.$id.$which"}="";
343: }
344: }
345:
346: sub delete_form_items {
347: my ($jspart,$id) = @_;
348: &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id.'.filename'});
349: &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id.'.mimetype'});
350: &Apache::lonnet::delenv($env{'form.HWFILE'.$jspart.'_'.$id});
351: }
352:
353:
354: sub check_collaborators {
355: my ($ncol,$coll) = @_;
356: my %classlist=&Apache::lonnet::dump('classlist',
357: $env{'course.'.$env{'request.course.id'}.'.domain'},
358: $env{'course.'.$env{'request.course.id'}.'.num'});
359: my (@badcollaborators,$result);
360:
361: my (@collaborators) = split(/,?\s+/,$coll);
362: foreach my $entry (@collaborators) {
363: my $collaborator;
364: if ($entry =~ /:/) {
365: $collaborator = $entry;
366: } else {
367: $collaborator = $entry.':'.$env{'user.domain'};
368: }
369: if ($collaborator !~ /^$match_username:$match_domain$/) {
370: if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
371: push(@badcollaborators,$entry);
372: }
373: } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
374: if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
375: push(@badcollaborators,$entry);
376: }
377: }
378: }
379:
380: my $numbad = scalar(@badcollaborators);
381: if ($numbad) {
382: $result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
383: if ($numbad == 1) {
384: $result .= &mt('The following user is invalid:');
385: } else {
386: $result .= &mt('The following [_1] users are invalid:',$numbad);
387: }
388: $result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
389: '</td></tr></table>';
390: }
391: my $toomany = scalar(@collaborators) - $ncol;
392: if ($toomany > 0) {
393: $result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
394: &mt('You have too many collaborators.').' '.
395: &mt('Please remove [quant,_1,collaborator].',$toomany).
396: '</td></tr></table>';
397: }
398: return $result;
399: }
400:
401: 1;
402: __END__
403:
404:
405: =pod
406:
407: =head1 NAME
408:
409: Apache::easyresponse
410:
411: =head1 SYNOPSIS
412:
413: Handler to evaluate essay (ungraded) style responses.
414:
415: This is part of the LearningOnline Network with CAPA project
416: described at http://www.lon-capa.org.
417:
418: =head1 SUBROUTINES
419:
420: =over
421:
422: =item start_essayresponse()
423:
424: =item end_essayresponse()
425:
426: =item format_prior_response()
427:
428: =item file_submission()
429:
430: =item delete_form_items()
431:
432: =item check_collaborators()
433:
434: =back
435:
436: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>