WeBWorK Main Forum

using student answers to generate output

Re: using student answers to generate output

by Michael Gage -
Number of replies: 0
Hi Joel,

The image has a caching problem.  First the server is caching the image
so that the image is created only once.  This is usually what you want.
It can be turned off by: 
$refreshCachedImages =1;

But that is not enough because the browser is also caching the image. And I'm not sure how to turn that off easily.  You can see the true image using shift-reload which makes the browser request the image again instead of using the cache -- but that is annoying and hard to explain to students.

The best solution seems to be to add a time stamp to the image name -- so that every image is different.  I checked StackOverflow and this is at least one answer.  The downside is that stale images will pile up on your server so you might want to delete them from the tmp directory on a regular basis. 

Others may have cleverer ways to fool the browser into refreshing the image.

Anyway here is some code which works for me:

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"scaffold.pl",
"PGcourse.pl",
"PGgraphmacros.pl",
"PGinfo.pl",
);
#$refreshCachedImages=1;
# the above line is not needed if we are adding random numbers to the image name

TEXT(beginproblem());

Context("Numeric");
Context()->variables->are(t=>'Real');
$answer1 = Compute("sin(t)");

Scaffold::Begin(
  can_open => "when_previous_correct",
  is_open  => "first_incorrect"
);

Section::Begin("Part 1: The first part");
BEGIN_PGML
Enter [` \sin(t) `] [________]{$answer1}
END_PGML
Section::End();

$answer1_student = $inputs_ref->{ANS_NUM_TO_NAME(1)}; # alternate, perhaps more direct method

$gr = init_graph(-4,-1,4,1,
axes=>[0,0],
grid=>[8,2],
size=>[400,400]
);
$gr->imageName($gr->imageName."-".time);
#TEXT( "graph name ", $gr->imageName);
#$V_out = Formula("cos(t )");
#$G0 = FEQ(qq! $V_out for t in <-4,4> using color:blue and weight:2!);
#($f1n) = plot_functions( $gr, $G0 );
add_functions($gr, "$answer1_student for t in <-4,4> using color:blue and weight:2");

Section::Begin("Part 2: The second part");
BEGIN_PGML
The student's first answer was [$answer1_student].  
Here's a graph of the student's answer:

[@ image( insertGraph($gr), width=>200,height=>200,tex_size=>800 ) @]*
END_PGML

Section::End();

Scaffold::End();

ENDDOCUMENT();