Annotation of loncom/homework/externalresponse.pm, revision 1.23

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>