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