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