Annotation of loncom/homework/externalresponse.pm, revision 1.28
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # external style responses
3: #
1.28 ! raeburn 4: # $Id: externalresponse.pm,v 1.27 2014/12/04 15:41:30 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.24 kruse 153: my ($is_submit,$was_draft,$externalgrade);
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.27 raeburn 176: my %info;
177: my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
178: if ($url =~ m{^https?://([^/]+)/}) {
179: use Socket;
180: my $ip = gethostbyname($1);
181: if (length($ip) eq 4) {
182: $info{'ip'} = inet_ntoa($ip);
183: }
184: } elsif ($url =~ m{^/}) {
185: my $ip = &Apache::lonnet::get_host_ip($Apache::lonnet::perlvar{'lonHostID'});
186: if ($ip =~ /^[\d\.]+$/) {
187: $info{'ip'} = $ip;
188: }
189: }
190: &Apache::essayresponse::file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,
191: \@deletions,'externalresponse',\%info);
1.20 kruse 192: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
193: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
194: my %previous=&Apache::response::check_for_previous($response,$part,$id);
195: if ($uploadedflag) {
196: if ($award eq 'FILENAME_INUSE') {
197: delete($Apache::lonhomework::results{"resource.$id.tries"});
198: }
199: } else {
200: &Apache::response::handle_previous(\%previous,$award);
201: }
202: if ($award eq 'SUBMITTED') {
203: my ($symb,$crsid,$domain,$name)=
204: &Apache::lonnet::whichuser();
205: if ($crsid) {
206: my $akey=join('.',&escape($name),&escape($domain),
207: &escape($crsid));
208: my $essayurl=
209: &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
210: my ($adom,$aname,$apath)=
211: ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
212: $apath=&escape($apath);
213: $apath=~s/\W/\_/gs;
214: &Apache::lonnet::put('nohist_essay_'.$apath,
215: { $akey => $response },$adom,$aname);
216: }
217: }
1.24 kruse 218: ($award eq 'DRAFT') ? ($externalgrade = 0) : ($externalgrade =1);
1.20 kruse 219: }
1.25 kruse 220: if (&Apache::response::getresponse() =~ /[^\s]/) {
221: $response = &Apache::response::getresponse();
222: $externalgrade = 1;
223: }
1.22 www 224:
1.25 kruse 225: if ($externalgrade) {
1.22 www 226: my $filesresults=0;
227: # Previously turned in files
228: my %previously_turned_in=();
229: foreach my $file (split(/\,/,$Apache::lonhomework::history{"resource.$part.$id.portfiles"}.','.
230: $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"})) {
231: if ($file=~/\S/) {
232: $previously_turned_in{'/uploaded/'.$env{'user.domain'}.'/'.$env{'user.name'}.'/portfolio'.$file}=1;
233: }
234: }
235: # Currently turned in files
236: my %currently_turned_in=();
1.23 kruse 237: foreach my $file (split(/\,/,$Apache::lonhomework::results{"resource.$part.$id.portfiles"}.','.
1.22 www 238: $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"})) {
1.23 kruse 239: if ($file=~/\S/) {
1.22 www 240: $filesresults=1;
241: $currently_turned_in{'/uploaded/'.$env{'user.domain'}.'/'.$env{'user.name'}.'/portfolio'.$file}=1;
242: }
243: }
1.23 kruse 244: my $filelinks=join(',',map{$_}(keys(%currently_turned_in)));
1.22 www 245: &Apache::lonnet::logthis("Previously: ".join("\n",sort(keys(%previously_turned_in))));
246: &Apache::lonnet::logthis("Currently: ".join("\n",sort(keys(%currently_turned_in))));
247: &Apache::lonnet::logthis("File flag: ".$filesresults);
248: &Apache::lonnet::logthis("Links String: ".$filelinks);
1.24 kruse 249: if ($filesresults || ( $response =~ /[^\s]/) ) {
1.21 kruse 250: my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
251: my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
252: my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
253: $form{'LONCAPA_student_response'}=$response;
254: $form{'LONCAPA_correct_answer'}=$answer;
255: $form{'LONCAPA_language'}=
256: &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
1.22 www 257: $form{'LONCAPA_student_submitted_files'} = $filelinks;
1.21 kruse 258: &Apache::lonxml::debug("Asking $url, with:");
259: &Apache::lonhomework::showhash(%form);
260: my $udom = &Apache::lonnet::EXT('user.domain');
261: my $uname = &Apache::lonnet::EXT('user.name');
262: my $symb = $env{'resource.symb'};
263: my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
264: my $ua = LWP::UserAgent->new;
265: my $res = $ua->request(POST $url, \%form);
266: %Apache::loncapagrade::results=();
267: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
268: if ($res->is_error()) {
269: $Apache::loncapagrade::results{'awarddetail'}='ERROR';
270: } else {
271: &Apache::lonxml::register('Apache::loncapagrade',
272: ('loncapagrade'));
273: @Apache::scripttag::parser_env = @_;
274: my $result=&Apache::scripttag::xmlparse($res->{_content});
275: &Apache::lonxml::debug("Got a result of :$result:");
276: }
277: foreach my $key (keys(%Apache::loncapagrade::results)) {
278: $Apache::lonhomework::results{"resource.$part.$id.$key"}=
279: $Apache::loncapagrade::results{$key};
280: }
281: $Apache::externalresponse::message=$Apache::loncapagrade::results{'message'};
282: if ($Apache::loncapagrade::results{'awarddetail'} eq 'ASSIGNED_SCORE') {
283: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
284: 1.*$Apache::loncapagrade::results{"awarded"};
285: }
1.24 kruse 286: my $handbackurl = $Apache::loncapagrade::results{'handbackurl'}
287: &Apache::lonnet::logthis("handbackurl: ". $Apache::loncapagrade::results{'handbackurl'});
1.21 kruse 288: &Apache::lonxml::debug("response of");
289: &Apache::lonhomework::showhash(%$res);
290: &Apache::lonxml::debug("capagrade of");
291: &Apache::lonhomework::showhash(%Apache::loncapagrade::results);
292: &Apache::lonxml::debug("results of");
293: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
294: }
1.20 kruse 295: }
1.24 kruse 296: }
1.1 albertel 297: }
1.20 kruse 298: if ($target eq 'web') {
1.21 kruse 299: &Apache::response::setup_prior_tries_hash(\&Apache::essayresponse::format_prior_response,
300: ['portfiles',
301: 'uploadedurl']);
1.17 www 302: if (&Apache::response::show_answer()) {
1.18 www 303: $result.='<table border="1"><tr><th>'.&mt('Your answer:').'</th></tr><tr><td><pre>'.
1.26 bisitz 304: &HTML::Entities::encode(
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.21 kruse 313:
1.7 albertel 314: }
315:
1.6 albertel 316: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
317: $target eq 'tex' || $target eq 'analyze') {
1.12 foxr 318: &Apache::lonxml::increment_counter($increment, "$part.$id");
1.11 foxr 319:
320: if ($target eq 'analyze') {
1.13 raeburn 321: $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
1.11 foxr 322: &Apache::lonhomework::set_bubble_lines();
323: }
1.6 albertel 324: }
1.20 kruse 325: elsif ($target eq 'edit') {
326: $result.=&Apache::edit::end_table();
327:
328: } elsif ($target eq 'tex'
329: && $Apache::lonhomework::type eq 'exam') {
330: $result .= &Apache::inputtags::exam_score_line($target);
331:
1.21 kruse 332: }
1.6 albertel 333: &Apache::response::end_response();
1.5 albertel 334: return $result;
1.1 albertel 335: }
336:
337: 1;
338: __END__
339:
340:
1.15 jms 341: =pod
342:
343: =head1 NAME
344:
345: Apache::externalresponse.pm
346:
347: =head1 SYNOPSIS
348:
349: Handler to evaluate externally graded responses.
350:
351: This is part of the LearningOnline Network with CAPA project
352: described at http://www.lon-capa.org.
353:
354: =head1 SUBROUTINES
355:
356: =over
357:
358: =item start_externalresponse()
359:
360: =item end_externalresponse()
361:
362: =back
363:
1.16 www 364: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>