--- loncom/interface/statistics/lonstathelpers.pm 2004/11/03 16:13:08 1.29 +++ loncom/interface/statistics/lonstathelpers.pm 2004/11/08 15:14:52 1.30 @@ -1,6 +1,6 @@ # The LearningOnline Network with CAPA # -# $Id: lonstathelpers.pm,v 1.29 2004/11/03 16:13:08 matthew Exp $ +# $Id: lonstathelpers.pm,v 1.30 2004/11/08 15:14:52 matthew Exp $ # # Copyright Michigan State University Board of Trustees # @@ -578,7 +578,7 @@ sub GetStudentAnswers { last if ($c->aborted()); my $sname = $student->{'username'}; my $sdom = $student->{'domain'}; - my $answer = &Apache::lonstathelpers::analyze_problem_as_student + my $answer = &Apache::lonstathelpers::get_student_answer ($resource,$sname,$sdom,$partid,$respid); &Apache::lonhtmlcommon::Increment_PrgWin($r,\%prog_state, &mt('last student')); @@ -599,27 +599,19 @@ sub GetStudentAnswers { =item analyze_problem_as_student -Analyzes a homework problem for a student and returns the correct answer -for the student. Attempts to put together an answer for problem types -that do not natively support it. +Analyzes a homework problem for a student Inputs: $resource: a resource object $sname, $sdom, $partid, $respid -Returns: $answer - -If $partid and $respid are specified, $answer is simply a scalar containing -the correct answer for the response. - -If $partid or $respid are undefined, $answer will be a hash reference with -keys $partid.'.'.$respid.'.answer'. +Returns: the problem analysis hash =cut ##################################################### ##################################################### sub analyze_problem_as_student { - my ($resource,$sname,$sdom,$partid,$respid) = @_; + my ($resource,$sname,$sdom) = @_; if (ref($resource) ne 'HASH') { my $res = $resource; $resource = { 'src' => $res->src, @@ -630,12 +622,10 @@ sub analyze_problem_as_student { [$res->responseIds($part)]; } } - my $returnvalue; my $url = $resource->{'src'}; my $symb = $resource->{'symb'}; my $analysis = &get_from_analysis_cache($sname,$sdom,$symb); if (! defined($analysis)) { - &Apache::lonnet::logthis('uncached analysis'); my $courseid = $ENV{'request.course.id'}; my $Answ=&Apache::lonnet::ssi($url,('grade_target' => 'analyze', 'grade_domain' => $sdom, @@ -647,15 +637,62 @@ sub analyze_problem_as_student { } my %Answer=&Apache::lonnet::str2hash($analysis); # + return \%Answer; +} + +##################################################### +##################################################### + +=pod + +=item get_student_answer + +Analyzes a homework problem for a particular student and returns the correct +answer. Attempts to put together an answer for problem types +that do not natively support it. + +Inputs: $resource: a resource object (from navmaps or hash from loncoursedata) + $sname, $sdom, $partid, $respid + +Returns: $answer + +If $partid and $respid are specified, $answer is simply a scalar containing +the correct answer for the response. + +If $partid or $respid are undefined, $answer will be a hash reference with +keys $partid.'.'.$respid.'.answer'. + +=cut + +##################################################### +##################################################### +sub get_student_answer { + my ($resource,$sname,$sdom,$partid,$respid) = @_; + # + if (ref($resource) ne 'HASH') { + my $res = $resource; + $resource = { 'src' => $res->src, + 'symb' => $res->symb, + 'parts' => $res->parts }; + foreach my $part (@{$resource->{'parts'}}) { + $resource->{'partdata'}->{$part}->{'ResponseIds'}= + [$res->responseIds($part)]; + } + } + # + my $analysis = + &analyze_problem_as_student($resource,$sname,$sdom); my $answer; foreach my $partid (@{$resource->{'parts'}}) { my $partdata = $resource->{'partdata'}->{$partid}; foreach my $respid (@{$partdata->{'ResponseIds'}}) { my $prefix = $partid.'.'.$respid; my $key = $prefix.'.answer'; - $answer->{$partid}->{$respid} = &get_answer($prefix,$key,%Answer); + $answer->{$partid}->{$respid} = + &get_answer($prefix,$key,%$analysis); } } + my $returnvalue; if (! defined($partid)) { $returnvalue = $answer; } elsif (! defined($respid)) {