File:
[LON-CAPA] /
loncom /
homework /
externalresponse.pm
Revision
1.31:
download - view:
text,
annotated -
select for diffs
Sun Dec 7 19:45:08 2014 UTC (9 years, 8 months ago) by
raeburn
Branches:
MAIN
CVS tags:
HEAD
- Support for submission of files to externalresponse.
- New routine: &portfiles_versioning() in lonnet.pm called after grading
externalresponse to handle versioning of submitted files in portfolio.
- &get_next_version(), &version_selected_portfile(), &file_name_version_ext()
moved from grades.pm to lonnet.pm to facilitate re-use.
1: # The LearningOnline Network with CAPA
2: # external style responses
3: #
4: # $Id: externalresponse.pm,v 1.31 2014/12/07 19:45:08 raeburn Exp $
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: #
28:
29:
30:
31: package Apache::externalresponse;
32: use strict;
33: use HTTP::Request::Common;
34: use Apache::loncapagrade;
35: use Apache::lonnet;
36: use Apache::lonlocal;
37: use Apache::lonxml();
38: use Apache::lonhtmlcommon;
39: use Apache::loncommon;
40: use Apache::lonnavmaps;
41: use Apache::essayresponse;
42: use LONCAPA qw(:DEFAULT :match);
43:
44: BEGIN {
45: &Apache::lonxml::register('Apache::externalresponse',('externalresponse'));
46: }
47:
48: =pod
49:
50: =head1 FIXME (start_externalresponse)
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:
58: sub start_externalresponse {
59: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
60: my $result;
61: my $id = &Apache::response::start_response($parstack,$safeeval);
62: if ($target eq 'edit') {
63: $result .=&Apache::edit::tag_start($target,$token);
64: $result .=&Apache::edit::text_arg('URL:','url',$token,60).'<br />';
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);
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,
72: 'answerdisplay','answer','form','url');
73: if ($constructtag) {
74: $result = &Apache::edit::rebuild_tag($token);
75: }
76: } elsif ($target eq 'meta') {
77: $result=&Apache::response::meta_package_write('externalresponse');
78: } elsif ($target eq 'web') {
79: my $part= $Apache::inputtags::part;
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"},'<>&"');
83: }
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: }
92: my $hiddendraft;
93: if ($uploadedfiletypes) {
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 {
100: $result = &Apache::essayresponse::draft_row($part,$id,$ncol,$uploadedfiletypes);
101: }
102: if ($ncol > 0) {
103: $result .= &Apache::essayresponse::get_collab_row($part,$id,$coll,$ncol);
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')) {
109: $filesfrom = 'uploadonly';
110: }
111: $result.=&Apache::inputtags::file_selector($part,$id,$uploadedfiletypes,
112: $filesfrom,undef,$maxfilesize);
113: } else {
114: if ($ncol > 0) {
115: $result = &Apache::essayresponse::get_collab_row($part,$id,$coll,$ncol,1);
116: }
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);
129: }
130: }
131: return $result;
132: }
133:
134: sub end_externalresponse {
135: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
136: my $result;
137: my $response;
138: my $part=$Apache::inputtags::part;
139: my $id = $Apache::inputtags::response['-1'];
140: my $increment = 1;
141: if ($target eq 'grade') {
142: &Apache::essayresponse::get_collaborators($part,$id);
143: if ( &Apache::response::submitted('scantron') ) {
144: my $increment=&Apache::response::scored_response($part,$id);
145: } elsif ( &Apache::response::submitted() ) {
146: $response = $env{'form.HWVAL_'.$id};
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');
153: my ($is_submit,$was_draft,$externalgrade,%info);
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: }
160: if (($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) ||
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;
176: %info = (
177: 'is_submit' => $is_submit,
178: );
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);
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: }
220: ($award eq 'DRAFT') ? ($externalgrade = 0) : ($externalgrade =1);
221: }
222: if (&Apache::response::getresponse() =~ /[^\s]/) {
223: $response = &Apache::response::getresponse();
224: $externalgrade = 1;
225: }
226:
227: if ($externalgrade) {
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=();
239: foreach my $file (split(/\,/,$Apache::lonhomework::results{"resource.$part.$id.portfiles"}.','.
240: $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"})) {
241: if ($file=~/\S/) {
242: $filesresults=1;
243: $currently_turned_in{'/uploaded/'.$env{'user.domain'}.'/'.$env{'user.name'}.'/portfolio'.$file}=1;
244: }
245: }
246: my $filelinks=join(',',map{$_}(keys(%currently_turned_in)));
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);
251: if ($filesresults || ( $response =~ /[^\s]/) ) {
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');
259: $form{'LONCAPA_student_submitted_files'} = $filelinks;
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');
264: my $symb = &Apache::lonnet::symbread();
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);
268: my @delaccess = keys(%currently_turned_in);
269: &Apache::lonnet::automated_portfile_access('ip',undef,\@delaccess,\%info);
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);
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: }
296: my $handbackurl = $Apache::loncapagrade::results{'handbackurl'}
297: &Apache::lonnet::logthis("handbackurl: ". $Apache::loncapagrade::results{'handbackurl'});
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: }
305: }
306: }
307: }
308: if ($target eq 'web') {
309: &Apache::response::setup_prior_tries_hash(\&Apache::essayresponse::format_prior_response,
310: ['portfiles',
311: 'uploadedurl']);
312: if (&Apache::response::show_answer()) {
313: $result.='<table border="1"><tr><th>'.&mt('Your answer:').'</th></tr><tr><td><pre>'.
314: &HTML::Entities::encode(
315: $Apache::lonhomework::history{"resource.$part.$id.submission"},'"<>&').
316: '</pre></td></table><br />'.&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval).
317: '<br />';
318: }
319: if ($Apache::externalresponse::message) {
320: $result.='<br /><table><tr><td bgcolor="#FFFFAA">'.$Apache::externalresponse::message.'</td></tr></table><br />';
321: }
322: $Apache::externalresponse::message='';
323:
324: }
325:
326: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
327: $target eq 'tex' || $target eq 'analyze') {
328: &Apache::lonxml::increment_counter($increment, "$part.$id");
329:
330: if ($target eq 'analyze') {
331: $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
332: &Apache::lonhomework::set_bubble_lines();
333: }
334: }
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:
342: }
343: &Apache::response::end_response();
344: return $result;
345: }
346:
347: 1;
348: __END__
349:
350:
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:
374: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>