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

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

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