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