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