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