Annotation of loncom/homework/essayresponse.pm, revision 1.126

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # essay (ungraded) style responses
1.5       albertel    3: #
1.126   ! raeburn     4: # $Id: essayresponse.pm,v 1.125 2018/11/20 19:14:14 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.108     raeburn    32: use Apache::lonhtmlcommon;
                     33: use Apache::loncommon;
1.62      albertel   34: use Apache::lonnet;
1.108     raeburn    35: use Apache::lonnavmaps;
1.33      albertel   36: use Apache::lonlocal;
1.88      raeburn    37: use LONCAPA qw(:DEFAULT :match);
1.72      www        38:  
1.1       albertel   39: 
1.6       harris41   40: BEGIN {
1.10      ng         41:     &Apache::lonxml::register('Apache::essayresponse',('essayresponse'));
1.1       albertel   42: }
                     43: 
                     44: sub start_essayresponse {
1.10      ng         45:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                     46:     my $result;
1.14      albertel   47:     my $id = &Apache::response::start_response($parstack,$safeeval);
                     48:     if ($target eq 'meta') {
                     49: 	$result=&Apache::response::meta_package_write('essayresponse');
1.121     raeburn    50:     } elsif ($target eq 'web') {
                     51:         my $part= $Apache::inputtags::part; 
                     52:         my $coll;
                     53:         if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
                     54:             $coll = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
                     55:         }
                     56: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                     57: 	    my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
                     58: 	    my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
                     59:             $uploadedfiletypes=~s/[^\w\,]//g;
                     60:             my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
                     61:             if (!defined($maxfilesize)) {
                     62:                 $maxfilesize = 10.0; #FIXME This should become a domain configuration 
                     63:             }
                     64:             my $hiddendraft;
                     65: 	    if (($Apache::lonhomework::type eq 'survey') ||
                     66:                 ($Apache::lonhomework::type eq 'surveycred') ||
                     67:                 ($Apache::lonhomework::type eq 'anonsurvey') ||
                     68:                 ($Apache::lonhomework::type eq 'anonsurveycred')) {
                     69:                 $hiddendraft = '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" />';
                     70:             } else {
1.124     raeburn    71:                 $result = &draft_row($part,$id,$ncol,$uploadedfiletypes,'essayresponse');
1.121     raeburn    72: 	    }
                     73: 	    if ($ncol > 0) {
                     74:                 $result .= &get_collab_row($part,$id,$coll,$ncol);
                     75: 	    }
                     76:             my $filesfrom = 'both';
                     77:             my $stuname = &Apache::lonnet::EXT('user.name');
                     78:             my $studom = &Apache::lonnet::EXT('user.domain');
                     79:             if (!&Apache::lonnet::usertools_access($stuname,$studom,'portfolio')) {
                     80:                 $filesfrom = 'uploadonly';
                     81:             }
                     82: 	    $result.=&Apache::inputtags::file_selector($part,$id,$uploadedfiletypes,
                     83: 						       $filesfrom,undef,$maxfilesize);
                     84:             if ($result) {
                     85:                 $result =
                     86:                       '<div>'.$hiddendraft.
                     87:                       &Apache::lonhtmlcommon::start_pick_box().
                     88:                       $result.
                     89:                       &Apache::lonhtmlcommon::end_pick_box().'</div>';
                     90:             } else {
                     91:                 $result = $hiddendraft;
1.114     raeburn    92:             }
                     93:         } else {
1.121     raeburn    94:             $result = &show_status_table($part,$id,$coll);
1.114     raeburn    95:         }
1.121     raeburn    96:     }
                     97:     return $result;
                     98: }
                     99: 
                    100: sub draft_row {
1.123     raeburn   101:     my ($part,$id,$ncol,$uploadedfiletypes,$resptype) = @_;
1.121     raeburn   102:     my $status_text = &mt('Submission type');
                    103:     if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
                    104:         $status_text .= '<br />'.&mt('(Currently -- draft)');
                    105:     }
                    106:     my $closure;
                    107:     unless ($ncol || $uploadedfiletypes) {
                    108:         $closure = 1;
                    109:     }
1.123     raeburn   110:     my %label = (
                    111:         draft => {
                    112:                    essayresponse => &mt('Save entries below (not submitted for credit yet)'),
                    113:                    externalresponse => &mt('Save entries below (not submitted for grading yet)'),
                    114:                  },
                    115:         submit => {
                    116:                     essayresponse => &mt('Submit entries below as answer to receive credit'),
                    117:                     externalresponse => &mt('Submit entries below for grading'),
                    118:                   }
                    119:     );
1.121     raeburn   120:     return &Apache::lonhtmlcommon::row_title($status_text).
                    121:            '<label>'.
                    122:            '<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="yes" checked="checked" />'.
1.123     raeburn   123:            '&nbsp;'.$label{'submit'}{$resptype}.'</label> <br />'.
1.121     raeburn   124:            '<label>'.
                    125:            '<input type="radio" name="HWDRAFT'.$part.'_'.$id.'" value="no" />&nbsp;'.
1.123     raeburn   126:            $label{'draft'}{$resptype}.'</label>'.
1.121     raeburn   127:            &Apache::lonhtmlcommon::row_closure($closure);
                    128: }
1.74      albertel  129: 
1.121     raeburn   130: sub get_collab_row {
                    131:     my ($part,$id,$coll,$ncol,$closure) = @_;
                    132:     my $output = &Apache::lonhtmlcommon::row_title(&mt('Collaborators')).
                    133:                  '<label>'.
                    134:                  &mt('Collaborators:').' <input type="text" size="70" max="80" name="HWCOL'.
                    135:                  $part.'_'.$id.'" value="'.$coll.'" /><br />'.
                    136:                  &mt('Enter a maximum of [quant,_1,collaborator] using username or username:domain, e.g. smithje or smithje:[_2].',
                    137:                      $ncol,$env{'user.domain'});
                    138:     if ($ncol > 1) {
                    139:         $output .= '<br />'.&mt('If entering more than one, use spaces to separate the collaborators.');
                    140:     }
                    141:     $output .= '</label><br />';
                    142:     $output .= &check_collaborators($ncol,$coll) if ($coll =~ /\w+/);
                    143:     $output .= &Apache::lonhtmlcommon::row_closure($closure);
                    144:     return $output;
                    145: }
1.74      albertel  146: 
1.121     raeburn   147: sub show_status_table {
                    148:     my ($part,$id,$coll) = @_;
                    149:     my $output;
                    150:     if ($coll) {
                    151:         $output = '<td><i>'.&mt('Collaborated with [_1]',$coll).'</i></td>';
                    152:     }
                    153:     my $current_files_display = &Apache::inputtags::current_file_submissions($part,$id);
                    154:     if ($current_files_display) {
                    155:         $output .= '<td><b>'.&mt('Submitted files:').'</b><br />'.
                    156:                    $current_files_display.'</td>';
                    157:     }
                    158:     if ($output ne '') {
                    159:         return '<table class="LC_pastsubmission"><tr>'.$output.
                    160:                '</tr></table>';
1.10      ng        161:     }
1.121     raeburn   162:     return;
1.1       albertel  163: }
                    164: 
                    165: sub end_essayresponse {
1.10      ng        166:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.13      ng        167:     my $part          = $Apache::inputtags::part;
1.14      albertel  168:     my $id            = $Apache::inputtags::response[-1];
1.70      albertel  169:     my $increment     = &Apache::response::repetition();
1.15      albertel  170:     my $result;
1.10      ng        171:     if ( $target eq 'grade' ) {
1.121     raeburn   172:         &get_collaborators($part,$id);
1.106     www       173: # Scantron
1.58      albertel  174: 	if (  &Apache::response::submitted('scantron') ) {
1.31      albertel  175: 	    $increment=&Apache::response::scored_response($part,$id);
1.58      albertel  176: 	} elsif ( &Apache::response::submitted() ) {
1.62      albertel  177: 	    my $response      = $env{'form.HWVAL_'.$id};
1.110     raeburn   178:             my $jspart=$part;
                    179:             $jspart=~s/\./_/g;
                    180:             my $filename = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} || 
1.91      raeburn   181:                            $env{'form.HWFILETOOBIG'.$part.'_'.$id};
1.110     raeburn   182:             my $portfiles = $env{'form.HWPORT'.$jspart.'_'.$id};
                    183:             my @deletions = &Apache::loncommon::get_env_multiple('form.HWFILE'.$jspart.'_'.$id.'_delete');
1.108     raeburn   184:             my ($is_submit,$was_draft);
                    185:             if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
                    186:                  $is_submit = 1;
                    187:             }
                    188:             if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
                    189:                 $was_draft = 1;
                    190:             }
                    191: 	    if (($response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) || 
                    192:                 (@deletions > 0) || ($was_draft && $is_submit)) {
1.61      albertel  193:  		my $award='DRAFT';
1.62      albertel  194:         	if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
1.102     raeburn   195:                     if ($Apache::lonhomework::type eq 'anonsurvey') {
                    196:                         $award='ANONYMOUS';
                    197:                     } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
                    198:                         $award='ANONYMOUS_CREDIT';
                    199:                     } elsif ($Apache::lonhomework::type eq 'surveycred') {
                    200:                         $award='SUBMITTED_CREDIT';
                    201:                     } else {
                    202: 		        $award='SUBMITTED';
                    203:                     }
1.14      albertel  204: 		}
1.22      www       205:                 my $uploadedflag=0;
1.91      raeburn   206:                 my $totalsize=0;
1.108     raeburn   207:                 &file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,\@deletions);
1.10      ng        208: 		$Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
1.14      albertel  209: 		$Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
1.10      ng        210: 		my %previous=&Apache::response::check_for_previous($response,$part,$id);
1.107     raeburn   211: 		if ($uploadedflag) {
                    212:                     if ($award eq 'FILENAME_INUSE') {
                    213:                         delete($Apache::lonhomework::results{"resource.$id.tries"});
                    214:                     }
                    215:                 } else {
                    216:                     &Apache::response::handle_previous(\%previous,$award);
                    217:                 }
1.32      www       218: #
                    219: # Store with resource author for similarity testing
                    220: #
                    221:                 if ($award eq 'SUBMITTED') {
                    222: 		    my ($symb,$crsid,$domain,$name)=
1.77      albertel  223: 			&Apache::lonnet::whichuser();
1.32      www       224: 		    if ($crsid) {
1.83      albertel  225: 			my $akey=join('.',&escape($name),&escape($domain),
                    226: 				      &escape($crsid));
1.32      www       227: 			my $essayurl=
                    228: 			    &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
1.125     raeburn   229:                         if ($essayurl eq 'lib/templates/simpleproblem.problem') {
                    230:                             my %crsinfo = &Apache::lonnet::coursedescription($crsid);
                    231:                             my $cdom = $crsinfo{'domain'};
                    232:                             my $cnum = $crsinfo{'num'};
                    233:                             my ($map,$id,$res) = &Apache::lonnet::decode_symb($symb);
                    234:                             if ($map =~ m{^\Quploaded/$cdom/$cnum/\E(default(?:|_\d+)\.(?:sequence|page))$}) {
                    235:                                 my $apath = $1.'_'.$id;
                    236:                                 $apath=~s/\W/\_/gs;
                    237:                                 my $akey = join('.',&escape($name),&escape($domain));
                    238:                                 &Apache::lonnet::put('nohist_essay_'.$apath,
                    239:                                                      { $akey => $response },$cdom,$cnum);
                    240:                             }
                    241:                         } else {
                    242: 			    my ($adom,$aname,$apath)=
                    243: 			        ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
                    244:                             $apath=&escape($apath);
                    245: 			    $apath=~s/\W/\_/gs;
                    246: 			    &Apache::lonnet::put('nohist_essay_'.$apath,
                    247:                                                  { $akey => $response },$adom,$aname);
                    248:                         }
1.126   ! raeburn   249:                     }
1.42      banghart  250:                 }
1.108     raeburn   251:             }
1.126   ! raeburn   252:         }
1.15      albertel  253:     } elsif ($target eq 'edit') {
                    254: 	$result.=&Apache::edit::end_table();
1.71      albertel  255: 
                    256:     } elsif ($target eq 'tex'
                    257: 	     && $Apache::lonhomework::type eq 'exam') {
                    258: 	$result .= &Apache::inputtags::exam_score_line($target);
                    259: 
1.70      albertel  260:     } elsif ($target eq 'answer') {
1.78      albertel  261: 	$result.=&Apache::response::answer_header($$tagstack[-1]);
1.81      foxr      262: 	my $answer = &mt('Essay will be hand graded.');
1.78      albertel  263: 	$result.=&Apache::response::answer_part($$tagstack[-1],$answer,
                    264: 						{'no_verbatim' => 1});
                    265: 	$result.=&Apache::response::answer_footer($$tagstack[-1]);
1.10      ng        266:     }
1.82      albertel  267:     if ($target eq 'web') {
                    268: 	&Apache::response::setup_prior_tries_hash(\&format_prior_response,
                    269: 						  ['portfiles',
                    270: 						   'uploadedurl']);
                    271:     }
1.71      albertel  272: 
1.27      albertel  273:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                    274: 	$target eq 'tex' || $target eq 'analyze') {
1.86      foxr      275: 	&Apache::lonxml::increment_counter($increment, "$part.$id");
1.85      foxr      276: 
                    277: 	if ($target eq 'analyze') {
1.87      raeburn   278:             $Apache::lonhomework::analyze{"$part.$id.type"} = 'essayresponse';
1.96      raeburn   279:             push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.85      foxr      280: 	    &Apache::lonhomework::set_bubble_lines();
                    281: 	}
1.27      albertel  282:     }
1.10      ng        283:     &Apache::response::end_response;
1.42      banghart  284: 
1.15      albertel  285:     return $result;
1.10      ng        286: }
                    287: 
1.121     raeburn   288: sub get_collaborators {
                    289:     my ($part,$id) = @_;
                    290:     my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};
                    291:     my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"'
                    292: );
                    293:     if ($collaborators ne $previous_list) {
                    294:         $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators;
                    295:     }
                    296: }
                    297: 
1.82      albertel  298: sub format_prior_response {
                    299:     my ($mode,$answer,$other_data) = @_;
                    300:     my $output;
                    301: 
                    302:     my (undef,undef,$udom,$uname) = &Apache::lonnet::whichuser();
                    303:     my $port_url = '/uploaded/'.$udom.'/'.$uname.'/portfolio/';
                    304: 
                    305:     my $file_list;
                    306: 
                    307:     foreach my $file (split(/\s*,\s*/,
                    308: 			    $other_data->[0].','.$other_data->[1])) {
                    309: 	next if ($file!~/\S/);
                    310: 	if ($file !~ m{^/uploaded/}) { $file=$port_url.$file; }
                    311: 	$file=~s|/+|/|g;
                    312: 	&Apache::lonnet::allowuploaded('/adm/essayresponse',$file);
                    313: 	$file_list.='<li><span class="LC_nobreak"><a href="'.$file.'?rawmode=1" target="lonGRDs"><img src="'.
                    314: 	    &Apache::loncommon::icon($file).'" alt="file icon" border="0" /> '.$file.
                    315: 	    '</a></span></li>'."\n";
                    316:     }
                    317:     if ($file_list) {
                    318: 	$output.= &mt('Submitted Files').'<ul>'.$file_list.'</ul>';
                    319:     }
                    320:     if ($answer =~ /\S/) {
                    321: 	$output.='<p>'.&mt('Submitted text').
1.118     kruse     322: 	    '<blockquote>'.&HTML::Entities::encode($answer, '"<>&').'</blockquote></p>';
1.82      albertel  323:     }
                    324: 
                    325:     return '<div class="LC_prior_essay">'.$output.'</div>';
                    326: }
                    327: 
1.61      albertel  328: sub file_submission {
1.119     raeburn   329:     my ($part,$id,$award,$uploadedflag,$totalsize,$deletions,$context,$info)=@_;
1.61      albertel  330:     my $files;
1.67      albertel  331:     my $jspart=$part;
                    332:     $jspart=~s/\./_/g;
1.108     raeburn   333:     my ($symb,$crsid,$udom,$uname) = &Apache::lonnet::whichuser();
                    334:     my %crsinfo = &Apache::lonnet::coursedescription($crsid);
                    335:     my $cdom = $crsinfo{'domain'};
                    336:     my $cnum = $crsinfo{'num'};
                    337:     my (@portfiles,$uploadedurl,@submitted_portfiles,$submitted_upload,
                    338:         @acceptable_portfiles,$acceptable_upload,@accepted_portfiles,
                    339:         $accepted_upload,@savedportfiles,$stored_upload,@tolock,
                    340:         %port_delete,$uploaded_delete);
                    341:     if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"} ||
                    342:         $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"}) {
                    343:         if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}) {
1.123     raeburn   344:             my @possfiles = split(/,/,$Apache::lonhomework::history{"resource.$part.$id.portfiles"});
                    345:             foreach my $file (@possfiles) {
                    346:                 my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
                    347:                 my ($origname,$version,$ext) = &Apache::lonnet::file_name_version_ext($name);
                    348:                 unless ($version) {
                    349:                     push(@portfiles,$file);
                    350:                 }
                    351:             }
1.108     raeburn   352:         }
                    353:         $uploadedurl = $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
                    354:         if (ref($deletions) eq 'ARRAY') {
                    355:             if (@{$deletions} > 0) {
                    356:                 foreach my $file (@{$deletions}) {
                    357:                     $file = &HTML::Entities::decode($file);
                    358:                     if (grep(/^\Q$file\E$/,@portfiles)) {
                    359:                         $port_delete{$file} = 1;
                    360:                     } elsif ($file =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/essayresponse/\Q$cdom\E/\Q$cnum\E/}) {
                    361:                         $uploaded_delete = $file; 
1.112     raeburn   362:                     } elsif ($file =~ m{^/uploaded/\Q$udom\E/\Q$uname\E/essayresponse/[^/]+$}) {
                    363:                         $uploaded_delete = $file;
1.108     raeburn   364:                     }
                    365:                 }
                    366:             }
                    367:         }
                    368:         foreach my $current (@portfiles) {
                    369:             unless ($port_delete{$current}) {
                    370:                 push(@savedportfiles,$current);
                    371:             }
                    372:         }
                    373:         if ($uploaded_delete) {
1.112     raeburn   374:             if ($uploaded_delete eq $uploadedurl) {
                    375:                 $Apache::lonhomework::results{"resource.$part.$id.uploadedfile"} = "";
                    376:                 $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"} = "";
                    377:             } else {
                    378:                 undef($uploaded_delete);
                    379:             }
1.108     raeburn   380:         }
                    381:     }
                    382:     if ($env{'form.HWPORT'.$jspart.'_'.$id} ne '') {
                    383:         my $newfiles= $env{'form.HWPORT'.$jspart.'_'.$id};
                    384:         $newfiles =~s/,$//;
                    385:         if ($newfiles =~ /[^\s]/) {
                    386:             foreach my $file (split(/\s*,\s*/,$newfiles)) {
                    387:                 if ($file =~ /[^\s]/) {
                    388:                     push(@submitted_portfiles,$file);
                    389:                 }
                    390:             }
                    391:         }
                    392:     }
1.110     raeburn   393:     if ($env{'form.HWFILETOOBIG'.$part.'_'.$id} ne '') {
1.108     raeburn   394:         $$award = 'EXCESS_FILESIZE';
                    395:     } elsif ($env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} ne '') {
                    396:         my $newfile = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'};
                    397:         if ($newfile =~ /[^\s]/) {
                    398:             $submitted_upload = $newfile;
1.91      raeburn   399:         }
1.61      albertel  400:     }
1.108     raeburn   401:     if (@savedportfiles) {
                    402:         foreach my $file (reverse(@savedportfiles)) {
                    403:             unless(grep(/^\Q$file\E$/,@submitted_portfiles)) {
                    404:                 unshift(@submitted_portfiles,$file);
                    405:             }
1.91      raeburn   406:         }
1.108     raeburn   407:     }
                    408:     if (@submitted_portfiles || $submitted_upload) {
                    409:         my $uploadedfiletypes=
1.91      raeburn   410:             &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
1.99      raeburn   411:         if ($uploadedfiletypes ne '') {
1.91      raeburn   412:             $uploadedfiletypes=~s/[^\w\,]//g;
                    413:             $uploadedfiletypes=','.$uploadedfiletypes.',';
1.108     raeburn   414:             if (@submitted_portfiles) {
                    415:                 foreach my $file (@submitted_portfiles) {
                    416:                     my ($extension)=($file=~/\.(\w+)$/);
                    417:                     if ($uploadedfiletypes=~/\,\s*\Q$extension\E\s*\,/i) {
                    418:                         push(@acceptable_portfiles,$file);
                    419:                     }
                    420:                 }
                    421:             }
                    422:             if ($submitted_upload) {
                    423:                 my ($upload_ext)=($submitted_upload=~/\.(\w+)$/);
                    424:                 if ($uploadedfiletypes=~/\,\s*\Q$upload_ext\E\s*\,/i) {
                    425:                     $acceptable_upload = $submitted_upload;
1.91      raeburn   426:                 } else {
                    427:                     $$award='INVALID_FILETYPE';
1.108     raeburn   428:                     &delete_form_items($jspart,$id);
1.91      raeburn   429:                 }
                    430:             }
1.99      raeburn   431:         } else {
1.108     raeburn   432:             @acceptable_portfiles = @submitted_portfiles;
                    433:             $acceptable_upload = $submitted_upload;
1.91      raeburn   434:         }
1.108     raeburn   435:     }
                    436:     if ((@acceptable_portfiles) || ($acceptable_upload ne '')) {
1.91      raeburn   437:         my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
                    438:         if (!$maxfilesize) {
1.93      raeburn   439:             $maxfilesize = 10.0; #FIXME This should become a domain configuration
1.91      raeburn   440:         }
                    441:         my %dirlist;
1.108     raeburn   442:         if (@acceptable_portfiles) {
                    443:             foreach my $file (@acceptable_portfiles) {
1.97      raeburn   444:                 my ($path,$filename) = ($file =~ m{^(.*/)([^/]+)$});
1.91      raeburn   445:                 my $fullpath = '/userfiles/portfolio'.$path;
                    446:                 if (!exists($dirlist{$fullpath})) {
1.116     raeburn   447:                     my ($listref,$listerror) =
                    448:                         &Apache::lonnet::dirlist($fullpath,$udom,$uname,1);
                    449:                     if (ref($listref) eq 'ARRAY') {
                    450:                         $dirlist{$fullpath} = $listref;
                    451:                     }
1.97      raeburn   452:                 }
                    453:                 if (ref($dirlist{$fullpath}) eq 'ARRAY') {
                    454:                     foreach my $dir_line (@{$dirlist{$fullpath}}) {
1.91      raeburn   455:                         my ($fname,$dom,undef,$testdir,undef,undef,undef,undef,
1.108     raeburn   456:                             $size,undef,$mtime,undef,undef,undef,$obs,undef) =
1.91      raeburn   457:                         split(/\&/,$dir_line,16);
                    458:                         if ($filename eq $fname) {
                    459:                             my $mbsize = $size/(1024.0*1024.0);
                    460:                             if (ref($totalsize)) {
                    461:                                 $$totalsize += $mbsize;
                    462:                             }
                    463:                             last;
                    464:                         }
                    465:                     }
                    466:                 }
1.108     raeburn   467:                 if (ref($totalsize)) {
                    468:                     if ($$totalsize > $maxfilesize) {
                    469:                         $$award='EXCESS_FILESIZE';
                    470:                         &delete_form_items($jspart,$id);
                    471:                     } else {
                    472:                         push(@accepted_portfiles,$file);
                    473:                     }
                    474:                 } else {
                    475:                     push(@accepted_portfiles,$file);
                    476:                 }
1.91      raeburn   477:             }
1.108     raeburn   478:         }
                    479:         if ($acceptable_upload ne '') {
1.91      raeburn   480:             if (ref($totalsize)) {
1.108     raeburn   481:                 $$totalsize += $env{'form.HWFILESIZE'.$jspart.'_'.$id};
1.91      raeburn   482:                 if ($$totalsize > $maxfilesize) {
                    483:                     $$award='EXCESS_FILESIZE';
1.108     raeburn   484:                     delete($env{'form.HWFILE'.$jspart.'_'.$id});
1.94      raeburn   485:                 } else {
1.108     raeburn   486:                     $accepted_upload = $acceptable_upload;
1.91      raeburn   487:                 }
                    488:             } else {
1.108     raeburn   489:                 $accepted_upload = $acceptable_upload;
1.91      raeburn   490:             }
                    491:         }
1.108     raeburn   492:     }
                    493:     if ($accepted_upload ne '') {
1.115     raeburn   494:         my ($path,$multiresp) = 
                    495:             &Apache::loncommon::get_turnedin_filepath($symb,$uname,$udom,
                    496:                                                       'submission');
                    497:         if ($path eq '') {
                    498:             $$award = 'INTERNAL_ERROR';
                    499:         } else {
                    500:             if ($multiresp) {
                    501:                 $path .= '/'.$jspart.'_'.$id;
                    502:             }
                    503:             my $prefix = 'portfolio';
                    504:             my $formelement = 'HWFILE'.$jspart.'_'.$id;
                    505:             my $fname = &Apache::lonnet::clean_filename($env{'form.'.$formelement.'.filename'});
                    506:             my $url = '/uploaded/'.$udom.'/'.$uname.'/'.$prefix.$path.'/'.$fname;
                    507:             my @stat = &Apache::lonnet::stat_file($url);
                    508:             my $conflicts = 0;
                    509:             if (@stat && $stat[0] ne 'no_such_dir') {
                    510:                 my $current_permissions = 
                    511:                     &Apache::lonnet::get_portfile_permissions($udom,$uname);
                    512:                 if (ref($current_permissions) eq 'HASH') {
                    513:                     if (ref($current_permissions->{$path.'/'.$fname}) eq 'ARRAY') {
                    514:                         foreach my $record (@{$current_permissions->{$path.'/'.$fname}}) {
                    515:                             if (ref($record) eq 'ARRAY') {
                    516:                                 next if (($record->[0] eq $symb) && 
                    517:                                          ($record->[1] eq $crsid));
                    518:                                 $conflicts ++;
                    519:                             } 
1.107     raeburn   520:                         }
                    521:                     }
1.108     raeburn   522:                 }
1.115     raeburn   523:                 if ($conflicts) {
                    524:                     $$award = 'FILENAME_INUSE';
1.107     raeburn   525:                 }
1.95      raeburn   526:             }
1.115     raeburn   527:             unless ($conflicts) { 
                    528:                 my ($mode,%allfiles,%codebase);
                    529:                 my $result = &Apache::lonnet::userfileupload($formelement,'',
1.108     raeburn   530:                              $prefix.$path,$mode,\%allfiles,\%codebase);
1.115     raeburn   531:                 if ($result =~ m{^/uploaded/}) {
                    532:                     $stored_upload = $path.'/'.$fname;
                    533:                     unless (grep(/^\Q$stored_upload\E$/,@accepted_portfiles)) {
                    534:                         $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = $stored_upload;
                    535:                         push(@tolock,$stored_upload);
                    536:                     }
                    537:                 } else {
                    538:                     $$award = 'INTERNAL_ERROR';
                    539:                 }
1.95      raeburn   540:             }
1.108     raeburn   541:         }
                    542:         delete($env{'form.HWFILE'.$jspart.'_'.$id});
                    543:     }
                    544:     if (@accepted_portfiles) {
1.115     raeburn   545:         if ($Apache::lonhomework::results{"resource.$part.$id.portfiles"}) {
1.108     raeburn   546:             $Apache::lonhomework::results{"resource.$part.$id.portfiles"} .= ',';
                    547:         }
                    548:         $Apache::lonhomework::results{"resource.$part.$id.portfiles"}.=join(',',@accepted_portfiles);
                    549:         push(@tolock,@accepted_portfiles);
                    550:     }
                    551:     if (!defined($Apache::lonhomework::results{"resource.$part.$id.portfiles"})) {
                    552:         if (keys(%port_delete) > 0) {
                    553:             $Apache::lonhomework::results{"resource.$part.$id.portfiles"} = "";
                    554:         }
                    555:     }
1.112     raeburn   556:     if (($Apache::lonhomework::history{"resource.$part.$id.portfiles"} ne  
                    557:          $Apache::lonhomework::results{"resource.$part.$id.portfiles"}) ||
                    558:         ($uploaded_delete)) {
1.108     raeburn   559:         if (ref($uploadedflag)) {
                    560:             $$uploadedflag=1;
                    561:         }
1.61      albertel  562:     }
1.119     raeburn   563:     if ($context eq 'externalresponse') {
1.120     raeburn   564:         my @todelete = map { "/uploaded/$udom/$uname/portfolio".$_;  } keys(%port_delete);
1.119     raeburn   565:         if (@tolock || @todelete) {
                    566:             if (ref($info) eq 'HASH') {
1.122     raeburn   567:                 if (($info->{'ip'}) && ($info->{'is_submit'})) {
1.120     raeburn   568:                     my @adds;
                    569:                     if (@tolock) {
                    570:                         @adds = map { "/uploaded/$udom/$uname/portfolio".$_;  } @tolock;
                    571:                     }
                    572:                     &Apache::lonnet::automated_portfile_access('ip',\@adds,\@todelete,$info);
1.119     raeburn   573:                 }
                    574:             }
                    575:         }
                    576:     }
1.108     raeburn   577:     &Apache::lonnet::unmark_as_readonly($udom,$uname,[$symb,$crsid]);
                    578:     &Apache::lonnet::mark_as_readonly($udom,$uname,[@tolock],[$symb,$crsid]);
                    579:     &Apache::lonnet::clear_selected_files($uname);
                    580:     return;
1.61      albertel  581: }
                    582: 
1.91      raeburn   583: sub delete_form_items {
                    584:     my ($jspart,$id) = @_;
1.98      raeburn   585:     delete($env{'form.HWFILE'.$jspart.'_'.$id.'.filename'});
                    586:     delete($env{'form.HWFILE'.$jspart.'_'.$id.'.mimetype'});
                    587:     delete($env{'form.HWFILE'.$jspart.'_'.$id});
1.91      raeburn   588: }
                    589: 
1.10      ng        590: sub check_collaborators {
1.11      ng        591:     my ($ncol,$coll) = @_;
1.10      ng        592:     my %classlist=&Apache::lonnet::dump('classlist',
1.62      albertel  593: 					$env{'course.'.$env{'request.course.id'}.'.domain'},
                    594: 					$env{'course.'.$env{'request.course.id'}.'.num'});
1.10      ng        595:     my (@badcollaborators,$result);
1.88      raeburn   596:   
1.106     www       597:     my (@collaborators) = split(/[,;\s]+/,$coll);
1.88      raeburn   598:     foreach my $entry (@collaborators) {
                    599:         my $collaborator;
                    600: 	if ($entry =~ /:/) {
                    601: 	    $collaborator = $entry;
1.10      ng        602: 	} else {
1.88      raeburn   603: 	    $collaborator = $entry.':'.$env{'user.domain'};
1.10      ng        604: 	}
1.88      raeburn   605:         if ($collaborator !~ /^$match_username:$match_domain$/) {
                    606:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    607: 	        push(@badcollaborators,$entry);
                    608:             }
                    609:         } elsif (!grep(/^\Q$collaborator\E$/i,keys(%classlist))) {
                    610:             if (!grep(/^\Q$entry\E$/,@badcollaborators)) {
                    611:                 push(@badcollaborators,$entry);
                    612:             }
                    613:         }
1.10      ng        614:     }
                    615:     
1.88      raeburn   616:     my $numbad = scalar(@badcollaborators);
                    617:     if ($numbad) {
                    618: 	$result = '<table border="0"><tr bgcolor="#ffbbbb"><td>';
                    619:         if ($numbad == 1) {
                    620:             $result .= &mt('The following user is invalid:');
                    621:         } else {
                    622:             $result .= &mt('The following [_1] users are invalid:',$numbad);
                    623:         }
                    624: 	$result .= ' '.join(', ',@badcollaborators).'. '.&mt('Please correct.').
                    625:                    '</td></tr></table>';
1.10      ng        626:     }
                    627:     my $toomany = scalar(@collaborators) - $ncol;
                    628:     if ($toomany > 0) {
                    629: 	$result .= '<table border="0"><tr bgcolor="#ffbbbb"><td>'.
1.88      raeburn   630: 	           &mt('You have too many collaborators.').' '.
                    631:                    &mt('Please remove [quant,_1,collaborator].',$toomany).
                    632: 	           '</td></tr></table>';
1.10      ng        633:     }
                    634:     return $result;
1.1       albertel  635: }
1.2       albertel  636: 
                    637: 1;
                    638: __END__
1.92      jms       639: 
                    640: 
                    641: =pod
                    642: 
                    643: =head1 NAME
                    644: 
1.106     www       645: Apache::essayresponse
1.92      jms       646: 
                    647: =head1 SYNOPSIS
                    648: 
                    649: Handler to evaluate essay (ungraded) style responses.
                    650: 
                    651: This is part of the LearningOnline Network with CAPA project
                    652: described at http://www.lon-capa.org.
                    653: 
                    654: =head1 SUBROUTINES
                    655: 
                    656: =over 
                    657: 
                    658: =item start_essayresponse()
                    659: 
1.121     raeburn   660: =item draft_row()
                    661: 
                    662: =item get_collab_row()
                    663:  
                    664: =item show_status_table()
                    665: 
1.92      jms       666: =item end_essayresponse()
                    667: 
1.121     raeburn   668: =item get_collaborators()
                    669: 
1.92      jms       670: =item format_prior_response()
                    671: 
                    672: =item file_submission()
                    673: 
                    674: =item delete_form_items()
                    675: 
                    676: =item check_collaborators()
                    677: 
                    678: =back
                    679: 
1.93      raeburn   680: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>