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