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