WeBWorK Main Forum

Problem with $refreshCachedImages

Problem with $refreshCachedImages

by Joel Trussell -
Number of replies: 23

I'm not discovering my error, so I'm asking for help. The problem generates a sinusoid with random frequency and asks students to identify the graph of the function. It works the first time, but fails to work when the student or prof uses get a new version of the problem. The frequency is changed in the question formulation, but the graphs are not changed. I've checked the macro files, set $refreshCachedImages  = 1 in various places.

Here's the code, Thanks

DOCUMENT();

loadMacros("PG.pl",
  "PGstandard.pl",
  "PGchoicemacros.pl",
  "PGgraphmacros.pl",
  "extraAnswerEvaluators.pl",
  "MathObjects.pl",
  "PGcourse.pl"
);

TEXT(beginproblem());

Context("Numeric");

Context("Numeric")->functions->add(
  step => {
    class => 'Parser::Legacy::Numeric',
    perl => 'Parser::Legacy::Numeric::do_step'
  },
);



$showPartialCorrectAnswers = 1;

#$a = 2;
#$b = 3;                 
Context()->variables->add(t=>'Real');
Context()->variables->add(F=>'Real');
$func = Formula("sin(2 pi F t)");

@eq = ("\( y=-2f(-t) \)",
       "\( y=2f(-t) \)",
       "\( y=-2f(t) \)",
       "\( y=-f \left( - \frac{1}{2} t \right) \)",
       "\( y=-\frac{1}{2} f(-t) \)",
       "\( y=f \left( \frac{1}{2} t \right) \)"     );

@descript = ("is a reflection about both the \(t\)-axis and \(y\)-axis as well as a vertical stretch by a factor of 2.",
"is a horizontal reflection about the \(y\)-axis as well as a vertical stretch by a factor of 2.",
"is a vertical reflection about the \(t\)-axis as well as a vertical stretch by a factor of 2.",
"is a reflection about both the \(t\)-axis and \(y\)-axis as well as a horizontal stretch by a factor of 2.");

$p1[0] = FEQ("step(t) - step(t-1) for t in <-1,10> using color:blue and weight:2");
$p2[0] = FEQ("step(t) - step(t-0.5) for t in <-1,10> using color:blue and weight:2");
$p3[0] = FEQ("step(t) - step(t-2) for t in <-1,10> using color:blue and weight:2");
$p4[0] = FEQ("step(t+1) - step(t) for t in <-1,10> using color:blue and weight:2");
$p1[1] = FEQ(qq! step(t) - step(t-0.5) + step(t) - step(t-2) for t in <-1,10> using color:blue and weight:2!);


##$graphf = init_graph(@opts);
##    (plot_functions($graphf,"$f for x in <-$dom,$dom> using color:blue"))[0]->steps(250);
## $labelf = new Label(@gr_lab, 'y = f(x)', 'blue' , 'center', 'center');
##    $graphf->lb($labelf);

$gr = init_graph(-2,-2,10,4,'axes'=>[0,0],'ticks'=>[6,6] );
$gr->lb('reset');
for ($i = -2; $i <= 4; $i++) { if ($i != 0) {
  $gr->lb(new Label(-.1,$i,$i,'black','right','middle')) }};
for ($i = -1; $i <= 5; $i++) {
  $gr->lb(new Label(2*$i,-.2,2*$i,'black','center','top')) };
$gr->lb(new Label(-.2,4.5,"y",'black','right','top'));
$gr->lb(new Label(9.5,-.2,"t",'black','right','top'));
$gr->lb(new Label(3.5,4,"s(t)",'black','left','bottom'));
## parentheses in ($fn1) are necessary
($f1n) = plot_functions( $gr, $p1[1] );
$f1n->steps(200);

$orig = image(insertGraph($gr),width => 400,height => 300,tex_size => 600);

$F = random(1,5,1);

$g[0] = "sin(2*pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[1] = "sin(2*pi*(-$F*t)) for t in <-1,1> using color:blue and weight:2";
$g[2] = "sin(pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[4] = "cos(2*pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[3] = "-cos(2*pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[5] = "sin(0.5*pi*$F*t) for t in <-1,1> using color:blue and weight:2";

$graph[0] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[1] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[2] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[3] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[4] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[5] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );

for ($j = 0; $j <=5; $j++) {
$graph[$j]->lb('reset');
$graph[$j]->lb(new Label(-.07,-1,-1,'black','right','middle'));
$graph[$j]->lb(new Label(-.07,1,1,'black','right','middle'));
for ($i = -3; $i <= 3; $i++) { if ($i != 0) {
    $graph[$j]->lb(new Label(0.25*$i,-.2,0.25*$i,'black','center','top')) }};
$graph[$j]->lb(new Label(-.05,1.9,"y",'black','right','top'));
$graph[$j]->lb(new Label(0.95,0.1,"t",'black','right','bottom'));
plot_functions( $graph[$j], $g[$j]);
$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200); };


# $pick = random(0,3,1);

# if ( $pick != 0 ) { $temp_eq = $eqn[0];
#                    $temp_gr = $fig[0];
#                    $eq[0] = $eq[$pick];
#                    $fig[0] = $fig[$pick];
#                    $eq[$pick] = $temp_eq;
#                    $fig[$pick] = $temp_gr};

$mc = new_multiple_choice();
$mc->qa('On a separate piece of paper, sketch an accurate graph of this function for \( F = $F \) and \( t \in [-1, 1] \).  Which (if any) of the graphs below matches the graph you drew?','$fig[0]');
$mc->extra('$fig[1] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[2] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[3] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[4] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[5] $BR $BITALIC(click on image to enlarge)$EITALIC');

$mc->makeLast('None of the above');

## force a refresh of the image after changes
$refreshCachedImages = 1;

Context()->texStrings;
BEGIN_TEXT
This problem reflects Problem 1.21a in the text
$PAR
Consider the function
$BR
$BR
\( y = $func \).
$BR
$BR
\{ $mc->print_q() \} $BR
\{ $mc->print_a() \}

END_TEXT
Context()->normalStrings;

ANS(radio_cmp($mc->correct_ans));
## force a refresh of the image after changes
$refreshCachedImages = 1;

Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR
$BBOLD  SOLUTION $EBOLD
$PAR
Setting \( F = $F \) gives the function \( y = \sin(2 \pi $F t) \). This has $F cycles in one unit of time. It starts at zero at t = 0, since it is a sine and is positive for the first values greater than zero. Therefore the correct graph is
$PAR
$BCENTER
$fig[0]
$ECENTER
$BR
which is answer \{ $mc->correct_ans \}.

END_SOLUTION
Context()->normalStrings;


ENDDOCUMENT();


In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Robert Mařík -

I tried the problem with different seed (used SRAND(406); etc directly in the PG file) and the images behave as expected - they change when the seed changes. So you should probably look for the error outside the pg file.

In reply to Robert Mařík

Re: Problem with $refreshCachedImages

by Joel Trussell -

I don't understand. If you change the seed in the problem using SRAND then it generates a problem correctly for the seed in SRAND, but all versions of the problem obtained using the "get a new version" have exactly the same frequency. That doesn't give the student a new problem. Maybe I'm misinterpreting your method.

I've done the same thing by changing the seed in the webwork editor and using the "view" tab. That changes the images to agree with the frequency. But the student can't change the seed. The "get a new version" button in the displayed problem shows a new frequency but the same images as the original problem and original seed.

I tried clearing the cache in the browser using ctrl-shift-delete prior to hitting the "get a new version" button (without using SRAND) that still uses the same images even though the frequency F changes.

I've done all of these experiments on Firefox, Chrome and Edge.

Thanks for help.

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Robert Mařík -

There are many ways how to change the seed. I work in Emacs editor on local file and refresh the web browser automatically when the file is saved. So for me the easiest way is to use SRAND in the pg file. If you edit the file in the web browser, you use probably another method to change the version of the problem. And as you wrote, this has to be removed in the final version.

Concerning Firefox, clicking refresh with shift button should reload the webpage and all its content. Without using the cache.

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -
The images are being generated when the seed changes for me as well.

Note that there is no need to set $refreshCachedImages = 1. Webwork will always generate a new image for different problem seeds (as well as different users, different problem set version numbers, different course IDs, different set numbers, different problem numbers, and different problem unique ids). It also takes into account the number of images in a problem and makes sure those are all generated. The only time that you need to set $refreshCachedImages = 1 is if you are testing things and want to force the image to be regenerated. Even then you usually don't need it because webwork also regenerates images if the source file has changed. In fact for a problem that is used in production, you should not set $refreshCachedImages = 1.  Webwork will take care of ensuring that the images are generated differently when they should be as per above.  Setting $refreshCachedImages = 1 will force images to always be regenerated, even when they should not be, and this results in extra load on the server.

Now, with all of that said, I have noticed when testing things, for example after changing the source code, that even though webwork does regenerate the image, what you see in the browser is the same.  The reason for this is something that webwork has no control over.  That is your browser's network caching.  When you change the source file resulting in webwork regenerating the image, the url with the filename is still the same (because the discussed things above are still the same in this case).  So your browser believes this is the same resource, and loads from cache.  Of course that results in you seeing the unchanged former image.  So do a hard refresh on your browser (ctrl-f5) and you will get the changed image.

One last thing.  $refreshCachedImages does not work the way you seem to think according to your comments.  Setting the value to 1 does not force a refresh of the image after you make changes to it.  $refreshCachedImages is used in the insertGraph method.  If it is 1 at the time that method is called, then the image will be regenerated.  Now in your problem you set $refreshCachedImages after you call insertImage.  The effect is that it is not used at all.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -
Thanks - moving the $refreshCachedImages to before the generation of graphs "helps" - but only if I manually clear the cache (ctrl-shift-delete) between using "get a new version" If I simply hit "get a new version" without the manual clear cache, I see the same images as the original seed. How about trying the sequence generate original problem (I use update tab, view tab will use old images) with seed 323 you should get F=4 and consistent graphs. Now repeatedly hit "get a new version" and check the graphs. I get the same graphs but in different order. The right answer is always the graph with the correct answer for F=4. Now try, with the graphs for F=4, clear cache(ctrl-shift-delete) and get new version, check the graphs and repeat.

here is my current code
DOCUMENT();

loadMacros("PG.pl",
"PGstandard.pl",
"PGchoicemacros.pl",
"PGgraphmacros.pl",
"extraAnswerEvaluators.pl",
"MathObjects.pl",
"PGcourse.pl"
);

TEXT(beginproblem());

Context("Numeric");

Context("Numeric")->functions->add(
step => {
class => 'Parser::Legacy::Numeric',
perl => 'Parser::Legacy::Numeric::do_step'
},
);


$showPartialCorrectAnswers = 1;
$refreshCachedImages = 1;

#$a = 2;
#$b = 3;
Context()->variables->add(t=>'Real');
Context()->variables->add(F=>'Real');
$func = Formula("sin(2 pi F t)");

@eq = ("\( y=-2f(-t) \)",
"\( y=2f(-t) \)",
"\( y=-2f(t) \)",
"\( y=-f \left( - \frac{1}{2} t \right) \)",
"\( y=-\frac{1}{2} f(-t) \)",
"\( y=f \left( \frac{1}{2} t \right) \)" );

@descript = ("is a reflection about both the \(t\)-axis and \(y\)-axis as well as a vertical stretch by a factor of 2.",
"is a horizontal reflection about the \(y\)-axis as well as a vertical stretch by a factor of 2.",
"is a vertical reflection about the \(t\)-axis as well as a vertical stretch by a factor of 2.",
"is a reflection about both the \(t\)-axis and \(y\)-axis as well as a horizontal stretch by a factor of 2.");

$p1[0] = FEQ("step(t) - step(t-1) for t in <-1,10> using color:blue and weight:2");
$p2[0] = FEQ("step(t) - step(t-0.5) for t in <-1,10> using color:blue and weight:2");
$p3[0] = FEQ("step(t) - step(t-2) for t in <-1,10> using color:blue and weight:2");
$p4[0] = FEQ("step(t+1) - step(t) for t in <-1,10> using color:blue and weight:2");
$p1[1] = FEQ(qq! step(t) - step(t-0.5) + step(t) - step(t-2) for t in <-1,10> using color:blue and weight:2!);


##$graphf = init_graph(@opts);
## (plot_functions($graphf,"$f for x in <-$dom,$dom> using color:blue"))[0]->steps(250);
## $labelf = new Label(@gr_lab, 'y = f(x)', 'blue' , 'center', 'center');
## $graphf->lb($labelf);

$gr = init_graph(-2,-2,10,4,'axes'=>[0,0],'ticks'=>[6,6] );
$gr->lb('reset');
for ($i = -2; $i <= 4; $i++) { if ($i != 0) {
$gr->lb(new Label(-.1,$i,$i,'black','right','middle')) }};
for ($i = -1; $i <= 5; $i++) {
$gr->lb(new Label(2*$i,-.2,2*$i,'black','center','top')) };
$gr->lb(new Label(-.2,4.5,"y",'black','right','top'));
$gr->lb(new Label(9.5,-.2,"t",'black','right','top'));
$gr->lb(new Label(3.5,4,"s(t)",'black','left','bottom'));
## parentheses in ($fn1) are necessary
($f1n) = plot_functions( $gr, $p1[1] );
$f1n->steps(200);

$orig = image(insertGraph($gr),width => 400,height => 300,tex_size => 600);

$F = random(1,5,1);

$g[0] = "sin(2*pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[1] = "sin(2*pi*(-$F*t)) for t in <-1,1> using color:blue and weight:2";
$g[2] = "sin(pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[4] = "cos(2*pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[3] = "-cos(2*pi*$F*t) for t in <-1,1> using color:blue and weight:2";
$g[5] = "sin(0.5*pi*$F*t) for t in <-1,1> using color:blue and weight:2";

$graph[0] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[1] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[2] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[3] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[4] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );
$graph[5] = init_graph(-1,-2,1,2,'axes'=>[0,0],'ticks'=>[8,8] );

for ($j = 0; $j <=5; $j++) {
$graph[$j]->lb('reset');
$graph[$j]->lb(new Label(-.07,-1,-1,'black','right','middle'));
$graph[$j]->lb(new Label(-.07,1,1,'black','right','middle'));
for ($i = -3; $i <= 3; $i++) { if ($i != 0) {
$graph[$j]->lb(new Label(0.25*$i,-.2,0.25*$i,'black','center','top')) }};
$graph[$j]->lb(new Label(-.05,1.9,"y",'black','right','top'));
$graph[$j]->lb(new Label(0.95,0.1,"t",'black','right','bottom'));
plot_functions( $graph[$j], $g[$j]);
$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200); };


# $pick = random(0,3,1);

# if ( $pick != 0 ) { $temp_eq = $eqn[0];
# $temp_gr = $fig[0];
# $eq[0] = $eq[$pick];
# $fig[0] = $fig[$pick];
# $eq[$pick] = $temp_eq;
# $fig[$pick] = $temp_gr};

$mc = new_multiple_choice();
$mc->qa('On a separate piece of paper, sketch an accurate graph of this function for \( F = $F \) and \( t \in [-1, 1] \). Which (if any) of the graphs below matches the graph you drew?','$fig[0]');
$mc->extra('$fig[1] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[2] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[3] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[4] $BR $BITALIC(click on image to enlarge)$EITALIC',
'$fig[5] $BR $BITALIC(click on image to enlarge)$EITALIC');

$mc->makeLast('None of the above');

Context()->texStrings;
BEGIN_TEXT
This problem reflects Problem 1.21a in the text
$PAR
Consider the function
$BR
$BR
\( y = $func \).
$BR
$BR
\{ $mc->print_q() \} $BR
\{ $mc->print_a() \}

END_TEXT
Context()->normalStrings;

ANS(radio_cmp($mc->correct_ans));
## force a refresh of the image after changes


Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR
$BBOLD SOLUTION $EBOLD
$PAR
Setting \( F = $F \) gives the function \( y = \sin(2 \pi $F t) \). This has $F cycles in one unit of time. It starts at zero at t = 0, since it is a sine and is positive for the first values greater than zero. Therefore the correct graph is
$PAR
$BCENTER
$fig[0]
$ECENTER
$BR
which is answer \{ $mc->correct_ans \}.

END_SOLUTION
Context()->normalStrings;


ENDDOCUMENT();
In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -

I am not sure what you are talking about by clicking "get a new version"?  Are you talking about about the problemRandomize.pl macro?  It doesn't look like you are using that macro.  Or do you load it via a custom PGcourse.pl macro?

You should consider switching to using webworks built in problem randomization instead if you are somehow using the problemRandomize.pl macro.  That macro is deprecated, and will probably be eventually removed.

If you are using the built in problem randomization approach, that changes the actual seed of the problem and so when you get a new problem version you will see the new images.  However, the problemRandomization.pl macro does not change the problem seed.  It sets a new seed inside the problem by calling srand.  This does not change the problem parameters that are used to generated the uuid for the image, and so you will not see images regenerated.  However, setting $refreshCachedImages = 1 (do this before you even start defining the graphs in the problem) will regenerate the images.  Now, this will not fix the browser caching issue.  Although the images are regenerated, your browser sees the same resource name, and so it just loads from cache.

The problem here is that you are using deprecated code and features.  Consider updating your approaches.  Yes, I realize documentation on this is scarce at this point.  Hopefully we get around to updating these things soon.

In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Glenn Rice -
There is a way you might be able to get this to work yet. I haven't tested this, but you could try setting $envir{problemSeed} = $main::problemSeed near the beginning of the problem. This might work to cause the resource id of the images to be changed.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

Wow! We're on different wavelengths. In order to allow the students to get a new version of the problem for practice after the problem set has closed, I use the PGcourse.pl, which I believed was standard. That produces a button on the problem page that says "Get a new version of this problem" I'm not testing my own code. I want to give the student a chance to work a different version of the same problem. In this case, the student should identify a simply sine wave from a collection of graphs.

I checked the PGcourse,pl macro that I have been using for a long time, but never looked at because it did what I wanted. I'm clearly out of my league on coding this myself, but it looks like it is supposed to do what I wanted. There are two calls to problemRandomize. The last appears to be what I've been getting and was happy with, except for the graphics. Any help is appreciated. Here is my PGcourse.pl

##  PGcourse.pl Dick Lane, mildly revised with Davide Cervone's help 2011-06-23

##  Put into templates/macros folder for a course (upload using File Manager).
##  Use by having last item of loadMacros be 'PGcourse.pl' (after a comma).

##  Note: usual path for loading macro files includes an identically-named file
##  which does nothing; problem files which explicitly load PGcourse.pl will be
##  usable by any course, independent of the course having its own version.

sub _PGcourse_init {};		####	Don't reload this file.

$guest   = ($effectivePermissionLevel == -5) ;	####	practice user

####	Presume that faculty observers are given TA status (permission 5).
$observe = ($effectivePermissionLevel >=  5) ;	## TA or Professor (perm = 10)

loadMacros( "problemRandomize.pl" ) ;

####	Allow a Guest or Observer to always get a new version of problem.
ProblemRandomize( when => "Always", onlyAfterDue => 0, style => "Button" )
	if	($guest or $observe) ;

##  Allow everybody else (e.g., a student) to get a new version after correct
##  answer(s) to first (scored) version OR after the assignment's due date
$when = (time >= $main::dueDate ? "Always" : "Correct");
ProblemRandomize( when => $when, onlyAfterDue => 0, style => "Button" )
	if	not ($guest or $observe) ;

####	Note: those two uses of ProblemRandomize reflect choices
####	which can be made independently, e.g., this file can be changed so that
####	a course invokes one while the other is commented-out (or zapped).

####	Always allow observer or teacher to View Source Code for problem.
####			loadMacros( "source.pl" )	if	$observe ;
####	commented-out because extra setup needed by an admin with shell access

####	Note: PREP-2011 participants have professor status for each PREP course
####	and can always read a problem's code by invoking Edit Source.

1;

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -
I am not sure what you mean by being on different wavelengths. You are using many things here that are outdated, and I am just advising that you update some things.

I know what you are using problemRandomize.pl for.  The point is that most of what is offered by that macro is done better by the built in problem randomization.  This is available from the course configuration options.  Under "Optional Modules", set "Enable periodic re-randomization of problems" to be True.

In any case, the answer to my question as to whether or not you are using problemRandomization.pl is macro via PGcourse.pl is yes.

Did you try adding

$envir{problemSeed} = $main::problemSeed

to the beginning of your problem? Add that right after the loadMacros(...) call.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

I added  $envir{problemSeed} = $main::problemSeed  after the load macros - no change

I thought "Enable periodic re-randomization of problems"  rerandomized after a selected number of attempts determined by the next setting:

The default number of attempts between re-randomization of the problems ( 0 => never)

This was to prevent students from guessing and eliminating possible answers to initial parameters of the problem.

I don't understand what you mean with a "built in" randomization. I want a new random set of parameters only after the student has been graded on the problem, and every time s/he hits the get a new version.  Can you tell me  what code to insert? I've been coding Webwork problems for 7 years now, for 10 different ECE courses. And I'm really not understanding the solution that you propose.

If a phone call or a zoom session would be better, I'm fine with that.

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -
The "built in" randomization that I meant is the "Enable periodic re-randomization of problems" option. With this the students are only allowed a limited number of attempts (the default is 5, but you can change that either course wide or per problem), and then the students are given a new version of the problem with a new seed. This eliminates guessing, as the student is given a new version of the problem with different random parameters after the limited number of attempts are used up.

The same as with the problemRandomization.pl macro. Your PGcourse.pl allows you to do this only after the set due date because it only activates the macro at that time. This is currently not an option with the built in randomization.

After doing some testing I see that the PGalias is initialized before the problem code is run. So my suggestion of adding $envir{problemSeed} = $main::problemSeed won't work. Unfortunately, that means there is no work around to the limitations of the problemRandomization.pl macro in this situation.

You could generate static images of all of the possible graphs (there are only 30 of them), and then just load those. That would work since each file would have its own file name.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

Thanks - we're getting closer to speaking the same language. Your patience is appreciated.

I used the suggestion you made when coding several other problems in the same problem set, which was illustrating scaling and shifting of functions. Generate a collection of graphs and randomly choose a subset of them.

I really don't want recode this problem with that form with 30+ graphs. Neither do I want to get into the gory details of the code for ProblemRandomize. I have the description, but I don't know what actually happens when a student hits the "get a new version" button. I tried to find the details and found this link

https://webwork.math.ucdavis.edu/wiki/doku.php?id=how_to_allow_students_to_generate_new_versions_of_problems

which refers to another link

http://webwork.maa.org/pod/pg_TRUNK/macros/problemRandomize.pl.html contains extensive documentation on these options and how to change them.

but when I clicked on this link I got a 404 error. I found one more

http://webwork.maa.org/doc/cvs/pg_CURRENT/macros/problemRandomize.pl.html

but got the same result.

I'd like to have an idea of what happens. Does the problem restart execution with a new seed, or start from the middle, or restart with some modifications?

The problem I've coded works like I want EXCEPT for showing the new graphics. It generates the graphics but doesn't show them. From what you say, they don't get the right name.  Is there anything I can read on this that I might understand?

I can get the graphics to show by forcing the clearing of cache manually with ctrl/shift/delete. Is there anyway to execute this command from the *.pl file?

Thanks

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -

The updated link to the problemRandomize.pl.html POD file is https://demo.webwork.rochester.edu/wwdocs/pg/macros/problemRandomize.pl.html.

The way that macro works is that is does not change the actual problem seed that is given to PG.  What it does is stores a new seed in a hidden input of the problem main form.  It retrieves the seed from there when problems are submitted.  Since the PGalias object is initialized (and determines unique file names for generated images using the PG problem seed and other factors) before the problemRandomize.pl macro is even loaded, the seed that the problemRandomize.pl macro is not used.

It is not that the images do not get the right name, it is that the name is not changed when problemRandomize.pl reseeds.

There certainly is not a way to force the browser to clear its cache from the page.  That would be a major security vulnerability, and browsers will not allow that.  There are ways to force the browser to not use its cache for the loading of a particular resources.  I am not certain how to make that work right off hand, but you could look into it.  This is usually done by adding an html url timestamp parameter.

In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

That is very helpful. I'd still like the system to reset the seed for the problem graphs but I found a work around - I can change the text in the "get a new version" button to include instructions to tell the student to clear cache using ctrl/shift/delete

ProblemRandomize( when => $when, onlyAfterDue => 0, style => "Button",label => "Clear Cache typing ctrl/shift/delete before clicking this button to Get a new version of the problem" )
    if    not ($guest or $observe) ;

Is there a way to insert a line break to make the text multiple lines and thus a bigger button? I'd like to tell the student to clear only the cache and not the history, etc.

Is it possible for me to insert instructions in text around the button?

Thanks

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -
You can add "\n" in the string where you want the line break.
For example, "Clear Cache typing ctrl/shift/delete before clicking\nthis button to Get a new version of the problem".

However, I should tell you that what you are doing is really bad practice.  Asking a user to clear the browser cache should only be done when there is something corrupted in the cache, or a one time occurrence to force a stale resource to be reloaded.  It shouldn't be used as a normal course of action.  If you are going to insist on this approach, then you should at least make sure to clarify to your students to only clear the browser cache and not all of the other things that can be cleared via ctrl-shift-delete like cookies, browsing history, passwords, etc.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

I mentioned that was the reason for wanting multiple lines - tell them to clear only cache and not history, etc.

I don't see another solution that can be implemented in a relatively short time. I'm coding these problems for other instructors and have limited time.

ProblemRandomize( when => $when, onlyAfterDue => 0, style => "Button",label => "Clear Cache by typing ctrl/shift/delete \n before clicking this button to \n Get a new version of the problem" )  didn't work.

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -
The "\n" works for me. Did you put that into both of the ProblemRandomize instances? The first one is the one that will appear for you as an instructor, and the second will appear for your students.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

interesting - I did not use the PGcourse. I commented it out and used #"PGcourse.pl",
  "problemRandomize.pl"  in the load macros for the problem.

I inserted the

ProblemRandomize( when => $when, onlyAfterDue => 0, style => "Button",label => "Clear Cache by typing ctrl/shift/delete \n before clicking this button to \n Get a new version of the problem" )

directly in the pg code

so there is only one occurrence.

It shows up for me and I"m the instructor/prof.

In reply to Joel Trussell

Re: Problem with $refreshCachedImages

by Glenn Rice -
That is because in the PGcourse.pl file that you are/were using, the the two ocurences are in conditionals. The first is "if ($guest or $observe)" and the second is "if not ($guest or $observe)". $guest and $observe are set from the user permissions earlier in the file. As instructor $observe will be true for you. Since you added the line to the problem without those conditionals, it will apply to all users.

Also, since you put that line directly into the problem the "\n" will not work there.  It does in a macro file.  You will need to change that to "~~n".
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Glenn Rice -
Here is a bit of a hack that should work to do what you want without asking your students to clear the cache.

On the line before you start the for loop to generate the images add

$timestamp = time;

Then change the last line in the for loop from

$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200);

to

$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200) =~ s/~~.png"/.png?timestamp=$timestamp"/gr;

so you end up with:

$timestamp = time;

for my $j (0 .. 5) {
$graph[$j]->lb('reset');
$graph[$j]->lb(new Label(-.07,-1,-1,'black','right','middle'));
$graph[$j]->lb(new Label(-.07,1,1,'black','right','middle'));
for ($i = -3; $i <= 3; $i++) { if ($i != 0) {
$graph[$j]->lb(new Label(0.25*$i,-.2,0.25*$i,'black','center','top')) }};
$graph[$j]->lb(new Label(-.05,1.9,"y",'black','right','top'));
$graph[$j]->lb(new Label(0.95,0.1,"t",'black','right','bottom'));
plot_functions( $graph[$j], $g[$j]);
$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200) =~ s/~~.png"/.png?timestamp=$timestamp"/gr;
};

The added url parameter "timestamp" makes the url different each time the problem is loaded, and so the browser will not try to load from cache. The server will ignore that parameter and still serve the same file.  Of course you have $refreshCachedImages = 1, so that file will be different than before if the "Get new version of this problem" button is clicked.
In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Glenn Rice -

There is a minor mistake in that code.  It will work as is, but there really shouldn't be a "g" flag on that regular expression.

So change:

$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200) =~ s/~~.png"/.png?timestamp=$timestamp"/gr;

to

$fig[$j] = image(insertGraph($graph[$j]),width => 240,height => 180,tex_size => 200) =~ s/~~.png"/.png?timestamp=$timestamp"/r;

In reply to Glenn Rice

Re: Problem with $refreshCachedImages

by Joel Trussell -

Fantastic!  This works with the old PGcourse.pl file. It avoids putting a weird call in the code that instructors who don't know Webwork will wonder about. - They can make minor changes, e.g. editing the words in the problem statement, changing the tolerance, etc.

This allows us to use fully dynamic graphs in the problems, so we can use a greater range of parameters and variations, rather than the generate a fixed number of graphs and permute them.

I wondered about a way to change the graphic file id. I'd used the student name as a seed for previous problems using scaffolding. I had no clue how to actually do it and would not have considered doing the study to figure out how it might be done. I doubt I would have discovered the modification of the Fig call.

This does exactly what I want and is easy to implement.  THANKS!