Annotation of loncom/homework/externalresponse.pm, revision 1.21
1.1 albertel 1: # The LearningOnline Network with CAPA
2: # external style responses
3: #
1.21 ! kruse 4: # $Id: externalresponse.pm,v 1.20 2013/08/22 07:39:15 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.6 albertel 176: my $part=$Apache::inputtags::part;
177: my $id = $Apache::inputtags::response['-1'];
178: my $increment = 1;
1.5 albertel 179: if ($target eq 'grade') {
1.20 kruse 180: my $collaborators = $env{'form.HWCOL'.$part.'_'.$id};
181: my $previous_list= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.collaborators"},'<>&"');
182: if ($collaborators ne $previous_list) {
1.21 ! kruse 183: # &Apache::lonnet::logthis("New collaborators [$collaborators] [$previous_list]");
1.20 kruse 184: $Apache::lonhomework::results{"resource.$part.$id.collaborators"}=$collaborators;
185: }
1.6 albertel 186: if ( &Apache::response::submitted('scantron') ) {
187: my $increment=&Apache::response::scored_response($part,$id);
188: } elsif ( &Apache::response::submitted() ) {
1.20 kruse 189: my $response = $env{'form.HWVAL_'.$id};
190: my $jspart=$part;
191: $jspart=~s/\./_/g;
192: my $filename = $env{'form.HWFILE'.$jspart.'_'.$id.'.filename'} ||
193: $env{'form.HWFILETOOBIG'.$part.'_'.$id};
194: my $portfiles = $env{'form.HWPORT'.$jspart.'_'.$id};
195: my @deletions = &Apache::loncommon::get_env_multiple('form.HWFILE'.$jspart.'_'.$id.'_delete');
196: my ($is_submit,$was_draft);
197: if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
198: $is_submit = 1;
199: }
200: if ($Apache::lonhomework::history{"resource.$part.award"} eq 'DRAFT') {
201: $was_draft = 1;
202: }
203: if (($response =~ /[^\s]/) || ($filename =~ /[^\s]/) || ($portfiles =~ /[^\s]/) ||
204: (@deletions > 0) || ($was_draft && $is_submit)) {
205: my $award='DRAFT';
206: if ($env{'form.HWDRAFT'.$part.'_'.$id} eq 'yes') {
207: if ($Apache::lonhomework::type eq 'anonsurvey') {
208: $award='ANONYMOUS';
209: } elsif ($Apache::lonhomework::type eq 'anonsurveycred') {
210: $award='ANONYMOUS_CREDIT';
211: } elsif ($Apache::lonhomework::type eq 'surveycred') {
212: $award='SUBMITTED_CREDIT';
213: } else {
214: $award='SUBMITTED';
215: }
216: }
217: my $uploadedflag=0;
218: my $totalsize=0;
1.21 ! kruse 219: &Apache::essayresponse::file_submission($part,$id,\$award,\$uploadedflag,\$totalsize,\@deletions);
1.20 kruse 220: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
221: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$award;
222: my %previous=&Apache::response::check_for_previous($response,$part,$id);
223: if ($uploadedflag) {
224: if ($award eq 'FILENAME_INUSE') {
225: delete($Apache::lonhomework::results{"resource.$id.tries"});
226: }
227: } else {
228: &Apache::response::handle_previous(\%previous,$award);
229: }
230: if ($award eq 'SUBMITTED') {
231: my ($symb,$crsid,$domain,$name)=
232: &Apache::lonnet::whichuser();
233: if ($crsid) {
234: my $akey=join('.',&escape($name),&escape($domain),
235: &escape($crsid));
236: my $essayurl=
237: &Apache::lonnet::declutter($ENV{'REQUEST_URI'});
238: my ($adom,$aname,$apath)=
239: ($essayurl=~/^($LONCAPA::domain_re)\/($LONCAPA::username_re)\/(.*)$/);
240: $apath=&escape($apath);
241: $apath=~s/\W/\_/gs;
242: &Apache::lonnet::put('nohist_essay_'.$apath,
243: { $akey => $response },$adom,$aname);
244: }
245: }
246: }
1.21 ! kruse 247: my $response = &Apache::response::getresponse();
! 248: my $filestest;
! 249: my $fileshistory = $Apache::lonhomework::history{"resource.$part.$id.portfiles"};
! 250: $fileshistory .= $Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
! 251: my $filesresults = $Apache::lonhomework::results{"resource.$part.$id.portfiles"};
! 252: $filesresults .= $Apache::lonhomework::results{"resource.$part.$id.uploadedurl"};
! 253: if (( $response =~ /[^\s]/) || $filesresults || $fileshistory) {
! 254: my $url = &Apache::lonxml::get_param('url',$parstack,$safeeval);
! 255: my $answer = &Apache::lonxml::get_param('answer',$parstack,$safeeval);
! 256: my %form = &Apache::lonxml::get_param_var('form',$parstack,$safeeval);
! 257: $form{'LONCAPA_student_response'}=$response;
! 258: $form{'LONCAPA_correct_answer'}=$answer;
! 259: $form{'LONCAPA_language'}=
! 260: &Apache::lonnet::metadata($ENV{'REQUEST_URI'},'language');
! 261: $form{'LONCAPA_student_submitted_files_results'} = $filesresults;
! 262: $form{'LONCAPA_student_submitted_files_history'} = $fileshistory;
! 263: &Apache::lonxml::debug("Asking $url, with:");
! 264: &Apache::lonhomework::showhash(%form);
! 265: my $udom = &Apache::lonnet::EXT('user.domain');
! 266: my $uname = &Apache::lonnet::EXT('user.name');
! 267: my $symb = $env{'resource.symb'};
! 268: my %record = &Apache::lonnet::restore($symb,$env{'request.course.id'},$udom,$uname);
! 269: my $ua = LWP::UserAgent->new;
! 270: my $res = $ua->request(POST $url, \%form);
! 271: my %previous = &Apache::response::check_for_previous($response,
! 272: $part,$id);
! 273: %Apache::loncapagrade::results=();
! 274: $Apache::lonhomework::results{"resource.$part.$id.submission"}=$response;
! 275: if ($res->is_error()) {
! 276: $Apache::loncapagrade::results{'awarddetail'}='ERROR';
! 277: } else {
! 278: &Apache::lonxml::register('Apache::loncapagrade',
! 279: ('loncapagrade'));
! 280: @Apache::scripttag::parser_env = @_;
! 281: my $result=&Apache::scripttag::xmlparse($res->{_content});
! 282: &Apache::lonxml::debug("Got a result of :$result:");
! 283: }
! 284: foreach my $key (keys(%Apache::loncapagrade::results)) {
! 285: $Apache::lonhomework::results{"resource.$part.$id.$key"}=
! 286: $Apache::loncapagrade::results{$key};
! 287: }
! 288: $Apache::externalresponse::message=$Apache::loncapagrade::results{'message'};
! 289: &Apache::response::handle_previous(\%previous,
! 290: $Apache::loncapagrade::results{'awarddetail'});
! 291: if ($Apache::loncapagrade::results{'awarddetail'} eq 'ASSIGNED_SCORE') {
! 292: $Apache::lonhomework::results{"resource.$part.$id.awarded"}=
! 293: 1.*$Apache::loncapagrade::results{"awarded"};
! 294: }
! 295: &Apache::lonxml::debug("response of");
! 296: &Apache::lonhomework::showhash(%$res);
! 297: &Apache::lonxml::debug("capagrade of");
! 298: &Apache::lonhomework::showhash(%Apache::loncapagrade::results);
! 299: &Apache::lonxml::debug("results of");
! 300: &Apache::lonhomework::showhash(%Apache::lonhomework::results);
! 301: }
1.20 kruse 302: }
1.1 albertel 303: }
1.20 kruse 304: if ($target eq 'web') {
1.21 ! kruse 305: &Apache::response::setup_prior_tries_hash(\&Apache::essayresponse::format_prior_response,
! 306: ['portfiles',
! 307: 'uploadedurl']);
1.17 www 308: if (&Apache::response::show_answer()) {
1.18 www 309: $result.='<table border="1"><tr><th>'.&mt('Your answer:').'</th></tr><tr><td><pre>'.
1.17 www 310: $Apache::lonhomework::history{"resource.$part.$id.submission"}.
1.18 www 311: '</pre></td></table><br />'.&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval).
312: '<br />';
1.17 www 313: }
1.16 www 314: if ($Apache::externalresponse::message) {
1.17 www 315: $result.='<br /><table><tr><td bgcolor="#FFFFAA">'.$Apache::externalresponse::message.'</td></tr></table><br />';
1.16 www 316: }
317: $Apache::externalresponse::message='';
1.21 ! kruse 318:
1.7 albertel 319: }
320:
1.6 albertel 321: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
322: $target eq 'tex' || $target eq 'analyze') {
1.12 foxr 323: &Apache::lonxml::increment_counter($increment, "$part.$id");
1.11 foxr 324:
325: if ($target eq 'analyze') {
1.13 raeburn 326: $Apache::lonhomework::analyze{"$part.$id.type"} = 'externalresponse';
1.11 foxr 327: &Apache::lonhomework::set_bubble_lines();
328: }
1.6 albertel 329: }
1.20 kruse 330: elsif ($target eq 'edit') {
331: $result.=&Apache::edit::end_table();
332:
333: } elsif ($target eq 'tex'
334: && $Apache::lonhomework::type eq 'exam') {
335: $result .= &Apache::inputtags::exam_score_line($target);
336:
1.21 ! kruse 337: }
1.6 albertel 338: &Apache::response::end_response();
1.5 albertel 339: return $result;
1.1 albertel 340: }
341:
342: 1;
343: __END__
344:
345:
1.15 jms 346: =pod
347:
348: =head1 NAME
349:
350: Apache::externalresponse.pm
351:
352: =head1 SYNOPSIS
353:
354: Handler to evaluate externally graded responses.
355:
356: This is part of the LearningOnline Network with CAPA project
357: described at http://www.lon-capa.org.
358:
359: =head1 SUBROUTINES
360:
361: =over
362:
363: =item start_externalresponse()
364:
365: =item end_externalresponse()
366:
367: =back
368:
1.16 www 369: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>