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

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

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