WeBWorK Problems

HTML INPUT(?) for dynamic problem adjustments

HTML INPUT(?) for dynamic problem adjustments

by D. Brian Walton -
Number of replies: 1
I came across the following problem file:
Library/270/setDerivatives1/c1s6p1.pg

It has the curious feature that there is an answer box that is used the next time the page is loaded to adjust the table. I've looked at the code and don't really understand how it works.

There is a section of the code that obviously loads the information out of somewhere:
if (defined $inputs{'dh'} ) {
$dh = $inputs{'dh'};
}
elsif (defined $in{'dh'} ) {
$dh = $in{'dh'};
} elsif (defined $inputs_ref->{'dh'}) {
$dh = $inputs_ref->{'dh'};
}
else {
$dh = .0001
}

and there is also clearly a section that is designed to save the information away somewhere:
$INPUTSPACE = M3( qq! \\rule{${len}in}{.01in}!,
qq!\begin{rawhtml}<INPUT TYPE = "TEXT" NAME="dh"
VALUE="$dh">\end{rawhtml}!,
qq!<INPUT TYPE = "TEXT" NAME="dh" VALUE="$dh">!
);

So it appears that this box is actually not part of the actual PG code, but is instead an HTML form. Is this interpretation correct? If so, exactly what is happening in the reading part up above? Why are there three different places being checked --- $inputs, $in, and $inputs_ref ? Is this something to do with different browsers?

I'd love to do something similar. Are there limitations or further instructions on this technique? Can the answer box used for the HTML INPUT also be used as an ans_rule, or if they are intrinsically separated?

Are there some other example problems to these that anyone can point me toward?

Thanks,
- Brian
In reply to D. Brian Walton

Re: HTML INPUT(?) for dynamic problem adjustments

by Michael Gage -
Hi Brian,

I think that only the $inputs_ref variable is being used in this problem. The others are "no longer operative" :-)

To see which variables are available when authoring a webwork question you can use this snippet:


DOCUMENT();
loadMacros("PG.pl",
"PGstandard.pl",
"PGinfo.pl",
);

listVariables();

ENDDOCUMENT();


You can also load PGinfo.pl to any other question while you are authoring or debugging. That file defines the macro listVariables(); as well as listFormVariables(); etc. which are described at http://webwork.maa.org/pod/pg_TRUNK/macros/PGinfo.pl.html

It doesn't show the %in or %inputs hashes but it does show
$inputs_ref -- so I think the latter has replaced the former -- this problem was written in a transition stage where it needed to check in several places for the information it needed.

With that out of the way: answer blanks (and hidden answer blanks) are just HTML input types ( type = "text" or type="hidden") and the author can access the information entered into those blanks using their name as the key for $inputs_ref.

You can reference an answer blank if you give it a name or label

use labeled_ans_rule http://webwork.maa.org/wiki/Labeled_ans_rule

and then

LABELED_ANS();

to evaluate the student response.


(synonyms are NAMED_ANS_RULE and NAMED_ANS() )

(You could also try to guess the label that is automatically produced for ordinary ans_rule() but I discourage this because the method for generating these labels may change from time to time.)

Very interesting problems can be written using these techniques. Since they use some of the deeper internal mechanisms of the PG language these problems might require updating from time to time as we perfect the PG language but I expect to have higher level tools for accessing this information in some later release that will stabilize the situation. Meantime I encourage you to experiment and let us know what works and what doesn't and what additional tools you would like to have since that will guide the development of these higher level tools.

Hope this helped. If not ask again -- it's a nice project.

Take care,

Mike