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