WeBWorK Problems

Retrieve Javascript data for answer to problem

Retrieve Javascript data for answer to problem

by Glenn Rice -
Number of replies: 8
I am trying to write a problem that uses jsxgraph to have a student graph something. However, I am having trouble finding a way to check if the graph is correct. What I need to do is obtain information from the javascript to use in checking the answer. How can I do this?
In reply to Glenn Rice

Re: Retrieve Javascript data for answer to problem

by Glenn Rice -
I figured out how to do this using the NAMED_HIDDEN_ANS_RULE macro, and setting the value of the created form field inside the javascript. Now the problem is then when the answer is checked the things that are done in the javascript are reset, and so the graph that the student created disappears. So I need a way to save the state of things in the script and retrieve that to reload it after the answer is checked. In this case the question asks the student to graph a line by plotting two points. So I need to save the coordinates of the points that are plotted.

Also, is there a way to prevent WeBWork from displaying the "Entered", "Answer Preview" columns when the answer is checked?
In reply to Glenn Rice

Re: Retrieve Javascript data for answer to problem

by Michael Gage -
For your last question: You can remove the "entered" display using the course configuration page: Display the evaluated student answer.   I don't think there is a way to get rid of Answer preview without small modifications to the source code in Problem.pm

For your first question.  You've found one way of returning information.  If you don't want the information in the list of answers, which requires a corresponding answer evaluator, you can just use a hidden input field.

There is also  RECORD_EXTRA_ANSWERS() -- but that gets tricky. It stores the data between invocations without specifically needing an answer evaluator. 


To find out what information is being returned to you include

"PGinfo.pl"  into the loadMacros() subroutine  and include

listVariables();
in the .pg file (No need for TEXT() here, the subroutine prints itself) -- this will give you a table of all of the variables available to you in a pg file. That should help you quite a bit with your experiments.


For long term storage between invocations of the problem you want to use    store_persistent_data, retrieve_persistent_data  defined in PG.pl (which links to the real subroutine in PGcore.pl ).  However that subroutine hasn't been properly implemented yet.  I got called away to something else in the middle of that project. :-(   It was meant to be used for storing persistent data for applets.  It's not clear yet whether it is best to use localStorage (now available in html5) or to provide storage on the server. Probably each would be useful in different situations. 

Glad you are experimenting with this -- there are all kinds of extensions to webwork which become possible using javaScript.

-- Mike
In reply to Michael Gage

Re: Retrieve Javascript data for answer to problem

by Michael Gage -
One addition.  To see the current, slightly hacked, way that we store persistent data by sneaking it into the stored answers, take a look at the subroutine insertAll() in AppletObjects.pl.  Around lines 155 through 192 we are storing the state of the applet so that it is maintained from one invocation to the next. (It uses RECOED_FORM_LABEL() to do this.). We also store the state in the HTML form itself so that the state is preserved during the time that the users session is active, even if their answers are not being recorded. 


In reply to Michael Gage

Re: Retrieve Javascript data for answer to problem

by Glenn Rice -
I figured out how to get the problem to work the way I wanted by using the graphed points for the answers instead of the slope and y intercept as I initially attempted. So I just used the NAMED_HIDDEN_ANS_RULE macro for this and a custom answer checker that checks to see if the plotted points are (close to being) on the line.

Thanks for your response in any case. You have provided useful information that I may need on other problems of this type.

Although I have a question about your following comment:
"If you don't want the information in the list of answers, which requires a corresponding answer evaluator, you can just use a hidden input field."
I tried doing this by including something like
<input type=hidden ...>
in the html code, but the information that I stored in the field was not persistent when the answer was checked. How should this have been done?
In reply to Glenn Rice

Re: Retrieve Javascript data for answer to problem

by Glenn Rice -
Since I have the problem working now I thought I would post it here for suggestions, and to share what I have.
In reply to Glenn Rice

Re: Retrieve Javascript data for answer to problem

by Michael Gage -
For now use RECORD_FORM_LABEL() as defined in PG.pl and used in problemRandomize.pl, parserMultiAnswers.pl.  Eventually you should be able to use store_persistent_data() but the implementation code in PGcore.pm hasn't been written yet.
In reply to Michael Gage

Re: Retrieve Javascript data for answer to problem

by Glenn Rice -
Okay. That is what I thought. So you create a hidden form field, as I mentioned before, and then call RECORD_FORM_LABEL (or RECORD_EXTRA_ANSWERS) on the id or name of that, correct?