Annotation of loncom/homework/externalresponse.pm, revision 1.33
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # external style responses
3: #
1.33 ! raeburn 4: # $Id: externalresponse.pm,v 1.32 2014/12/22 17:04:03 raeburn 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;
1.28 raeburn 41: use Apache::essayresponse;
1.20 kruse 42: use LONCAPA qw(:DEFAULT :match);
1.33 ! raeburn 43: use LONCAPA::LWPReq;
1.1 albertel 44:
45: BEGIN {
1.5 albertel 46: &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
1.1 albertel 47: }
48:
1.14 jms 49: =pod
50:
1.15 jms 51: =head1 FIXME (start_externalresponse)
1.14 jms 52:
53: send of response params and their current values (form good enough? what parameters to send?)
54: Need to get returned message displayed
55:
56: =cut
57:
58:
1.1 albertel 59: sub start_externalresponse {
1.5 albertel 60: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
61: my $result;
62: my $id = &Apache::response::start_response($parstack,$safeeval);
1.10 albertel 63: if ($target eq 'edit') {
64: $result .=&Apache::edit::tag_start($target,$token);
65: $result .=&Apache::edit::text_arg('URL:','url',$token,60).'<br />';
1.17 www 66: $result .=&Apache::edit::text_arg(&mt('Answer:'),'answer',$token);
67: $result .=&Apache::edit::text_arg(&mt('Form:'),'form',$token).'<br />';
68: $result .=&Apache::edit::text_arg(&mt('Answer display:'),'answerdisplay',$token,80);
1.10 albertel 69: $result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
70: } elsif ($target eq 'modified') {
71: my $constructtag =
72: &Apache::edit::get_new_args($token,$parstack,$safeeval,
1.17 www 73: 'answerdisplay','answer','form','url');
1.10 albertel 74: if ($constructtag) {
75: $result = &Apache::edit::rebuild_tag($token);
76: }
77: } elsif ($target eq 'meta') {
78: $result=&Apache::response::meta_package_write('externalresponse');
1.28 raeburn 79: } elsif ($target eq 'web') {
1.20 kruse 80: my $part= $Apache::inputtags::part;
1.28 raeburn 81: my $coll;
82: if ($Apache::lonhomework::history{"resource.$part.$id.collaborators"} =~ /\S/) {
83: $coll = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
1.20 kruse 84: }
1.28 raeburn 85: if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
86: my $ncol= &Apache::lonnet::EXT("resource.$part".'_'."$id.maxcollaborators");
87: my $uploadedfiletypes= &Apache::lonnet::EXT("resource.$part".'_'."$id.uploadedfiletypes");
88: $uploadedfiletypes=~s/[^\w\,]//g;
89: my $maxfilesize=&Apache::lonnet::EXT("resource.$part".'_'."$id.maxfilesize");
90: if (!defined($maxfilesize)) {
91: $maxfilesize = 10.0; #FIXME This should become a domain configuration
92: }
1.20 kruse 93: my $hiddendraft;
1.28 raeburn 94: if ($uploadedfiletypes) {
1.20 kruse 95: if (($Apache::lonhomework::type eq 'survey') ||
96: ($Apache::lonhomework::type eq 'surveycred') ||
97: ($Apache::lonhomework::type eq 'anonsurvey') ||
98: ($Apache::lonhomework::type eq 'anonsurveycred')) {
99: $hiddendraft = '<input type="hidden" name="HWDRAFT'.$part.'_'.$id.'" value="yes" />';
100: } else {
1.32 raeburn 101: $result = &Apache::essayresponse::draft_row($part,$id,$ncol,$uploadedfiletypes,
102: 'externalresponse');
1.20 kruse 103: }
104: if ($ncol > 0) {
1.28 raeburn 105: $result .= &Apache::essayresponse::get_collab_row($part,$id,$coll,$ncol);
1.20 kruse 106: }
107: my $filesfrom = 'both';
108: my $stuname = &Apache::lonnet::EXT('user.name');
109: my $studom = &Apache::lonnet::EXT('user.domain');
110: if (!&Apache::lonnet::usertools_access($stuname,$studom,'portfolio')) {
1.28 raeburn 111: $filesfrom = 'uploadonly';
1.20 kruse 112: }
113: $result.=&Apache::inputtags::file_selector($part,$id,$uploadedfiletypes,
1.28 raeburn 114: $filesfrom,undef,$maxfilesize);
115: } else {
116: if ($ncol > 0) {
117: $result = &Apache::essayresponse::get_collab_row($part,$id,$coll,$ncol,1);
1.20 kruse 118: }
1.28 raeburn 119: }
120: if ($result) {
121: $result =
122: '<div>'.$hiddendraft.
123: &Apache::lonhtmlcommon::start_pick_box().
124: $result.
125: &Apache::lonhtmlcommon::end_pick_box().'</div>';
126: } else {
127: $result = $hiddendraft;
128: }
129: } else {
130: $result = &Apache::essayresponse::show_status_table($part,$id,$coll);
1.20 kruse 131: }
1.28 raeburn 132: }
1.5 albertel 133: return $result;
1.1 albertel 134: }
135:
136: sub end_externalresponse {
1.5 albertel 137: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
138: my $result;
1.25 kruse 139: my $response;
1.6 albertel 140: my $part=$Apache::inputtags::part;
141: my $id = $Apache::inputtags::response['-1'];
142: my $increment = 1;
1.5 albertel 143: if ($target eq 'grade') {
1.28 raeburn 144: &Apache::essayresponse::get_collaborators($part,$id);
1.6 albertel 145: if ( &Apache::response::submitted('scantron') ) {
146: my $increment=&Apache::response::scored_response($part,$id);
147: } elsif ( &Apache::response::submitted() ) {
1.25 kruse 148: $response = $env{'form.HWVAL_'.$id};
1.20 kruse 149: my $jspart=$part;
150: $jspart=~s/\./_/g;
151: my $filename = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} ||
152: $env{'form.HWFILETOOBIG'.$part.'_'.$id};
153: my $portfiles = $env{'form.HWPORT'.$jspart.'_'.$id};
154: my @deletions = &Apache::loncommon::get_env_multiple('form.HWFILE'.$jspart.'_'.$id.'_delete');
1.30 raeburn 155: my ($is_submit,$was_draft,$externalgrade,%info);
1.20 kruse 156: if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
157: $is_submit = 1;
158: }
159: if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
160: $was_draft = 1;
161: }
1.25 kruse 162: if (($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) ||
1.20 kruse 163: (@deletions > 0) || ($was_draft && $is_submit)) {
164: my $award='DRAFT';
165: if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
166: if ($Apache::lonhomework::type eq 'anonsurvey') {
167: $award='ANONYMOUS';
168: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
169: $award='ANONYMOUS_CREDIT';
170: } elsif ($Apache::lonhomework::type eq 'surveycred') {
171: $award='SUBMITTED_CREDIT';
172: } else {
173: $award='SUBMITTED';
174: }
175: }
176: my $uploadedflag=0;
177: my $totalsize=0;
1.30 raeburn 178: %info = (
179: 'is_submit' => $is_submit,
180: );
1.27 raeburn 181: my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
182: if ($url =~ m{^https?://([^/]+)/}) {
183: use Socket;
184: my $ip = gethostbyname($1);
185: if (length($ip) eq 4) {
186: $info{'ip'} = inet_ntoa($ip);
187: }
188: } elsif ($url =~ m{^/}) {
189: my $ip = &Apache::lonnet::get_host_ip($Apache::lonnet::perlvar{'lonHostID'});
190: if ($ip =~ /^[\d\.]+$/) {
191: $info{'ip'} = $ip;
192: }
193: }
194: &Apache::essayresponse::file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,
195: \@deletions,'externalresponse',\%info);
1.20 kruse 196: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
197: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
198: my %previous=&Apache::response::check_for_previous($response,$part,$id);
199: if ($uploadedflag) {
200: if ($award eq 'FILENAME_INUSE') {
201: delete($Apache::lonhomework::results{"resource.$id.tries"});
202: }
203: } else {
204: &Apache::response::handle_previous(\%previous,$award);
205: }
206: if ($award eq 'SUBMITTED') {
207: my ($symb,$crsid,$domain,$name)=
208: &Apache::lonnet::whichuser();
209: if ($crsid) {
210: my $akey=join('.',&escape($name),&escape($domain),
211: &escape($crsid));
212: my $essayurl=
213: &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
214: my ($adom,$aname,$apath)=
215: ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
216: $apath=&escape($apath);
217: $apath=~s/\W/\_/gs;
218: &Apache::lonnet::put('nohist_essay_'.$apath,
219: { $akey => $response },$adom,$aname);
220: }
221: }
1.24 kruse 222: ($award eq 'DRAFT') ? ($externalgrade = 0) : ($externalgrade =1);
1.20 kruse 223: }
1.25 kruse 224: if (&Apache::response::getresponse() =~ /[^\s]/) {
225: $response = &Apache::response::getresponse();
226: $externalgrade = 1;
227: }
1.22 www 228:
1.25 kruse 229: if ($externalgrade) {
1.22 www 230: my $filesresults=0;
231: # Previously turned in files
232: my %previously_turned_in=();
233: foreach my $file (split(/\,/,$Apache::lonhomework::history{"resource.$part.$id.portfiles"}.','.
234: $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"})) {
235: if ($file=~/\S/) {
236: $previously_turned_in{'/uploaded/'.$env{'user.domain'}.'/'.$env{'user.name'}.'/portfolio'.$file}=1;
237: }
238: }
239: # Currently turned in files
240: my %currently_turned_in=();
1.23 kruse 241: foreach my $file (split(/\,/,$Apache::lonhomework::results{"resource.$part.$id.portfiles"}.','.
1.22 www 242: $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"})) {
1.23 kruse 243: if ($file=~/\S/) {
1.22 www 244: $filesresults=1;
245: $currently_turned_in{'/uploaded/'.$env{'user.domain'}.'/'.$env{'user.name'}.'/portfolio'.$file}=1;
246: }
247: }
1.23 kruse 248: my $filelinks=join(',',map{$_}(keys(%currently_turned_in)));
1.22 www 249: &Apache::lonnet::logthis("Previously: ".join("\n",sort(keys(%previously_turned_in))));
250: &Apache::lonnet::logthis("Currently: ".join("\n",sort(keys(%currently_turned_in))));
251: &Apache::lonnet::logthis("File flag: ".$filesresults);
252: &Apache::lonnet::logthis("Links String: ".$filelinks);
1.24 kruse 253: if ($filesresults || ( $response =~ /[^\s]/) ) {
1.21 kruse 254: my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
255: my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
256: my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
257: $form{'LONCAPA_student_response'}=$response;
258: $form{'LONCAPA_correct_answer'}=$answer;
259: $form{'LONCAPA_language'}=
260: &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
1.22 www 261: $form{'LONCAPA_student_submitted_files'} = $filelinks;
1.21 kruse 262: &Apache::lonxml::debug("Asking $url, with:");
263: &Apache::lonhomework::showhash(%form);
264: my $udom = &Apache::lonnet::EXT('user.domain');
265: my $uname = &Apache::lonnet::EXT('user.name');
1.31 raeburn 266: my $symb = &Apache::lonnet::symbread();
1.21 kruse 267: my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
1.33 ! raeburn 268: my $request=new HTTP::Request('POST',$url);
! 269: $request->content(join('&',map {
! 270: my $name = escape($_);
! 271: "$name=" . ( ref($form{$_}) eq 'ARRAY'
! 272: ? join("&$name=", map {escape($_) } @{$form{$_}})
! 273: : &escape($form{$_}) );
! 274: } keys(%form)));
! 275: my $res = &LONCAPA::LWPReq::makerequest('',$request,'','',10,1);
1.30 raeburn 276: my @delaccess = keys(%currently_turned_in);
277: &Apache::lonnet::automated_portfile_access('ip',undef,\@delaccess,\%info);
1.31 raeburn 278: my @submitted_portfiles = split(/,/,$Apache::lonhomework::results{"resource.$part.$id.portfiles"});
279: my @versioned_portfiles;
280: &Apache::lonnet::portfiles_versioning($symb,$udom,$uname,\@submitted_portfiles,
281: \@versioned_portfiles);
282: $Apache::lonhomework::results{"resource.$part.$id.portfiles"} =
283: join(',',@versioned_portfiles);
1.21 kruse 284: %Apache::loncapagrade::results=();
285: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
286: if ($res->is_error()) {
287: $Apache::loncapagrade::results{'awarddetail'}='ERROR';
288: } else {
289: &Apache::lonxml::register('Apache::loncapagrade',
290: ('loncapagrade'));
291: @Apache::scripttag::parser_env = @_;
292: my $result=&Apache::scripttag::xmlparse($res->{_content});
293: &Apache::lonxml::debug("Got a result of :$result:");
294: }
295: foreach my $key (keys(%Apache::loncapagrade::results)) {
296: $Apache::lonhomework::results{"resource.$part.$id.$key"}=
297: $Apache::loncapagrade::results{$key};
298: }
299: $Apache::externalresponse::message=$Apache::loncapagrade::results{'message'};
300: if ($Apache::loncapagrade::results{'awarddetail'} eq 'ASSIGNED_SCORE') {
301: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
302: 1.*$Apache::loncapagrade::results{"awarded"};
303: }
1.24 kruse 304: my $handbackurl = $Apache::loncapagrade::results{'handbackurl'}
305: &Apache::lonnet::logthis("handbackurl: ". $Apache::loncapagrade::results{'handbackurl'});
1.21 kruse 306: &Apache::lonxml::debug("response of");
307: &Apache::lonhomework::showhash(%$res);
308: &Apache::lonxml::debug("capagrade of");
309: &Apache::lonhomework::showhash(%Apache::loncapagrade::results);
310: &Apache::lonxml::debug("results of");
311: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
312: }
1.20 kruse 313: }
1.24 kruse 314: }
1.1 albertel 315: }
1.20 kruse 316: if ($target eq 'web') {
1.21 kruse 317: &Apache::response::setup_prior_tries_hash(\&Apache::essayresponse::format_prior_response,
318: ['portfiles',
319: 'uploadedurl']);
1.17 www 320: if (&Apache::response::show_answer()) {
1.18 www 321: $result.='<table border="1"><tr><th>'.&mt('Your answer:').'</th></tr><tr><td><pre>'.
1.26 bisitz 322: &HTML::Entities::encode(
323: $Apache::lonhomework::history{"resource.$part.$id.submission"},'"<>&').
1.18 www 324: '</pre></td></table><br />'.&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval).
325: '<br />';
1.17 www 326: }
1.16 www 327: if ($Apache::externalresponse::message) {
1.17 www 328: $result.='<br /><table><tr><td bgcolor="#FFFFAA">'.$Apache::externalresponse::message.'</td></tr></table><br />';
1.16 www 329: }
330: $Apache::externalresponse::message='';
1.21 kruse 331:
1.7 albertel 332: }
333:
1.6 albertel 334: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
335: $target eq 'tex' || $target eq 'analyze') {
1.12 foxr 336: &Apache::lonxml::increment_counter($increment, "$part.$id");
1.11 foxr 337:
338: if ($target eq 'analyze') {
1.13 raeburn 339: $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
1.11 foxr 340: &Apache::lonhomework::set_bubble_lines();
341: }
1.6 albertel 342: }
1.20 kruse 343: elsif ($target eq 'edit') {
344: $result.=&Apache::edit::end_table();
345:
346: } elsif ($target eq 'tex'
347: && $Apache::lonhomework::type eq 'exam') {
348: $result .= &Apache::inputtags::exam_score_line($target);
349:
1.21 kruse 350: }
1.6 albertel 351: &Apache::response::end_response();
1.5 albertel 352: return $result;
1.1 albertel 353: }
354:
355: 1;
356: __END__
357:
358:
1.15 jms 359: =pod
360:
361: =head1 NAME
362:
363: Apache::externalresponse.pm
364:
365: =head1 SYNOPSIS
366:
367: Handler to evaluate externally graded responses.
368:
369: This is part of the LearningOnline Network with CAPA project
370: described at http://www.lon-capa.org.
371:
372: =head1 SUBROUTINES
373:
374: =over
375:
376: =item start_externalresponse()
377:
378: =item end_externalresponse()
379:
380: =back
381:
1.16 www 382: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>