WeBWorK Problems

Using GeoGebra in WeBWorK problems

Using GeoGebra in WeBWorK problems

by Mike May -
Number of replies: 11
I am looking for some help using a GeoGebra applet in webwork.
I can get most of the pieces to work, but I cannot figure out how to make it
so the correct answer is computed by GeoGebra.

To walk through pseudocode:

Section 1 Initialization - I am fine here.
Environment and code from Edfinity to use a GeoGebra applet that has been posted to
GeoGebra.org

Section 2 - Setup - fine here
For this section I am going to say
$a=3;
$b= 4;

Section 3 GeoGebra applet - perhaps help needed here
I make an appropriate reference to the GeoGebra applet that has been preset.
send $a and $b to GeoGebra as variables vara and varb.
The applet adds them to varc which is 7.
I bring varc back as a javascript variable c1.
I would like to turn it into a webwork variable $c.
How do I do this?

Section 4 Main text - fine here
There is nice text showing the values of $a and $b and asking the student to give the sum.
Thus is captured with an answer_rule construction, to give a reference $StudentAnswer

Section 4 Answer Evaluation - Help needed here.
I want to evaluate the answer and compare $StudentAnswer to c1.

Is this doable with a couple of lines of code?
Obviously, I can do the computation in WeBWorK, but I have other GeoGebra
computations where GeoGebra figuring out the correct answer would make life much easier.

Mike May




In reply to Mike May

Re: Using GeoGebra in WeBWorK problems

by Andrew Parker -
Mike, this would be easier if you would attach the pg file for your problem.

Also, is the geogebra applet of your own construction? In other words, you know the geogebra-names of all the objects you wish to set using WeBWorK values?
In reply to Andrew Parker

Re: Using GeoGebra in WeBWorK problems

by Mike May -
Thanks.
It is an applet I created that generates a random A and B
and puts the sum in C.
I started with code from another problem and simplified it as much as I could to focus on the functionality I want.
I can tell that I am sending values to the applet and retrieving them.
The difficult line is
$ansc = Compute("1");
which computes the correct answer for the third input. I would like that to be the javascript variable c.

I will admit that I do not understand what the lines

$lsgraph =
"<div id='ggb' class='ww-ggb'></div>

and

[$lsgraph]* [__]{$ansa}[__]{$ansb}[__]{$ansc}
I also don't know if there is a way to get a value like c, which was computed by GeoGebra into the WeBWorK text.

Thanks again.

Mike



pg file attached below.

#######################
## DESCRIPTION
## Basic Applet manipulation
## ENDDESCRIPTION

## DBsubject('')
## Author('Mike May')

################################################################################
# Initialization
################################################################################

DOCUMENT();

HEADER_TEXT('<script type="text/javascript" src="https://cdn.geogebra.org/apps/deployggb.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ww_ggb_applet/lib/ww_ggb_applet.js"></script>');

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"contextArbitraryString.pl",
"parserPopUp.pl",
"niceTables.pl",
"PGchoicemacros.pl",
);

TEXT(beginproblem());

################################################################################
# Problem Setup
################################################################################

Context("Numeric");

$A=random(1,20,1);
$B=random(1,20,1);

$lsgraph =
"<div id='ggb' class='ww-ggb'></div>

<script>

var onLoad = function(applet) {
applet.evalCommand('A: $A');
applet.evalCommand('B: $B');
applet.setVisible('scattersoln',false);
var a = this.applet.getValue('A');
var b = this.applet.getValue('B');
var c = this.applet.getValue('C');
this.setAnswer('AnSwEr0001', a);
this.setAnswer('AnSwEr0002', b);
this.setAnswer('AnSwEr0003', c);
}

var onUpdate = function(obj) {
var a = this.applet.getValue('A');
var b = this.applet.getValue('B');
var c = this.applet.getValue('C');
this.setAnswer('AnSwEr0001', a);
this.setAnswer('AnSwEr0002', b);
this.setAnswer('AnSwEr0003', c);
}

wwG = new WwGgbApplet('ggb', {height:200, width:150, borderColor:'#FFFFFF', 'material_id':'mnz7sa8t', appletOnLoad: onLoad, appletOnUpdate: onUpdate, hideAnswers: true});

</script>";

$ansa = Compute("$A");
$ansb = Compute("$B");
$ansc = Compute("1");
# [$table]***

################################################################################
# Text
################################################################################

BEGIN_PGML
A company sends out ten different questionnaires to its customers.

The table shows the number sent and replies received for each questionnaire.


[$lsgraph]* [__]{$ansa}[__]{$ansb}[__]{$ansc}

Complete the following parts using the interactive graph above.

A=[$A]

B=[$B]

C=[$C]

END_PGML

################################################################################
# Answers and Solutions
################################################################################

#BEGIN_PGML_SOLUTION

#END_PGML_SOLUTION

ENDDOCUMENT();

In reply to Mike May

Re: Using GeoGebra in WeBWorK problems

by Andrew Parker -
Mike, I'll play around with this a bit and get back to you.

For now,

$lsgraph =
"<div id='ggb' class='ww-ggb'></div>

is only part of the definition for lsgraph - this variable includes the scripts, and concludes only when we reach that second double-quote and semicolon. (</script>";)

the <div> is an HTML object with the identifier 'ggb', which the ww_ggb_applet.js script searches for and uses as a placeholder (into which it inserts the geogebra applet you requested).

[$lsgraph]* inserts the long string containing the <div> and <script>s into the problem HTML.

but, to clarify, what you're trying to do is to retrieve the value of c from the ggb and use it as a ww variable?
In reply to Mike May

Re: Using GeoGebra in WeBWorK problems

by Andrew Parker -
It seems as though your GG applet is what you described: adding A+B to get C, where A and B can be entered by the student.

My interpretation of your problem is that the students are given two values, which they type into the applet. The applet computes the sum and (invisibly) updates WeBWorK with the values to evaluate once the student hits submit. So, if student enters the right values, then the GG applet will give WW the expected "correct" answers: $ansa, $ansb, and $ansc - and the student will receive full credit.

Notes:
* scripts should be named `onLoad` instead of `XonLoad` and `onUpdate` instead of `XonUpdate`
* when the script loads, you're setting the initial values of A and B to already have the values that you want students to enter - is that what you intend?
* `$ansc = Compute("1");` should be `$ansc = Compute("$ansa+$ansb");`

Pulling the value of "c" from the GG applet, to be displayed in the WW page, will not be possible. How is GG supposed to compute the sum before the page is loaded (in order for WW to know what value to display)?

I get the feeling that this is just a "toy" example... perhaps you could elaborate on what your bigger idea is? There might be better alternatives to accomplish what you want.
In reply to Andrew Parker

Re: Using GeoGebra in WeBWorK problems

by Mike May -
You are right that this is a toy example.
It lets me check several pieces of functionality.
1) Set up a GeoGegra applet and put it into a WeBWorK page, and have this work on the server that I will use for class. Check
2) Let WeBWorK do randomization of initial variables and feed that to GeoGebra. This means students get different versions from each other but the same version when they reopen the problem.
3) Get information from GeoGebra back into answer variables for WeBWorK. Check.
4) Get information from GeoGebra back as the right answer for WeBWorK.
You say that can't be done.
5) Figure out how to get GeoGebra to do answer checking, we we send back 1 or zero for correct or incorrect. In other applets, so I think I can make it work.

One of the issues with 5 rather than 4 is that if an instructor is asked about a problem in class, the instructor can't take a fast look at the correct answer. I know a number of times I have looked at the answer to see what the question is asking.
The other problem with 5 is it does not let me feed answers into a solution section.

For what I really want to do:
I have written an open source business calculus book
<https://mathstat.slu.edu/~may/ExcelCalculus/> that tries to be a business calculus book rather than an engineering calculus book watered down and sifted to fit in one semester. I would like to claim true originality, but it boils down to following the recommendations of the MAA and the CRAFTY project, which is now almost 20 years old.

As past of that approach, I have students using Excel.
One of the steady themes is that the functions we care most about are defined by data tables and the generic problem says to start with data, find a best fitting curve(s) of the appropriate type, produce an equation for each curve, and use the equation(s) to predict, or do any of the things you would want to do with equations in a calculus class.

I have seen the code for best fitting lines in other WeBWorK problems. I would like to do best fitting curves where the curve is from any standard family where Excel will do curve fitting.

In GeoGebra, these are all easy steps, and I have verified it gives answers consistent with Excel.
I am also very familiar with GeoGebra, so I can easily build the corresponding applet. (While I am interested in business calculus, the GeoGebra functionality would be great for statistics problems.)

I have heard SAGE mentioned. Then I would have to figure out a stable place for the sage server.

I am open to other approaches.

Thanks for the help.

Mike

In reply to Mike May

Re: Using GeoGebra in WeBWorK problems

by Alex Jordan -
I am working on something relevant. I have refrained from posting because it's not finished, and not likely to be finished for about one more month. (My school is on three ten-week terms, not two semesters, and I'm still teaching until early June.)

I'm working on a GeoGebra MathObject class. As in $geogebra = GeoGebra(...). Details of the ... for later once it is done.
  1. Displaying $geogebra will show the applet. (In print I think I can even get a headless browser to generate a screen shot png.) It could be that's the end of how you'd use it. No connection to answer checking.
  2. You can use it as in an answer blank too, like [__]{$geogebra} in PGML markup.
    Used this way, no answer blank is visible. (one is there, but hidden.) Instead you see the applet, manipulate things, and press Submit. The "real answer" is some more familiar MathObject like Point(1,2), or a new kind of MathObject called a Score (a value returned from GeoGebra if it is doing the assessing). In the answer table, the correct answer will either be reported as this more familiar MathObject, or if it's a Score, then you will be able to define correct_ans_latex_string in the usual ways. Or you can make correct_ans be instructions for doing things to the applet.
I am trying to toss as much direct GeoGebra API javascript as I can under the hood so a typical problem author won't have to wade so deep.

This thread reached the point where I felt I should say something. But again, it's a month away, and I shouldn't make promises. Earlier this year I consulted with Davide about an outline and some technical details, and I'm encouraged by his help.

I'll post to this thread when it is ready for public scrutiny.
In reply to Alex Jordan

Re: Using GeoGebra in WeBWorK problems

by Andrew Parker -
I'd be very interested in this - particularly if this can handle multiple GGB on one page (as you mention below re: problem library).

Is there any reason not to put the GeoGebra Math Apps Bundle onto our own server? I ask because we're hitting 3000+ users now and GGB problems can be incredibly slow to load (taking up to 3-4 minutes or longer).
In reply to Mike May

Re: Using GeoGebra in WeBWorK problems

by Alex Jordan -
> 4) Get information from GeoGebra back as the right answer for WeBWorK.
> You say that can't be done

That's not quite correct and I don't think that is what Andrew is saying. I think Andrew is saying you cannot use GeoGebra to compute c and then display c somewhere in the problem body.

But in a sense, you can use GeoGebra to provide "a/the correct answer". Sort of. GeoGebra can compute c, use onUpdate to put c into the answer blank, and use a custom checker that checks that c meets whatever conditions you want it to meet. Sometimes that is easier to program than using perl/pg to find a/the correct answer. Like if the question were to find an eigenvector for a given matrix. That is easier to check than to algorithmically create.
In reply to Mike May

Re: Using GeoGebra in WeBWorK problems

by Andrew Parker -
Alex is correct:

4) Get information from GeoGebra back as the right answer for WeBWorK.
You say that can't be done.

That's not what I'm saying at all. My comment was to correctly compute $ansc as the sum of A and B, in which case this problem does the following:
1) randomly generates a and b
2) renders a Geogebra applet - prepopulated with A and B, and so automatically computes the value for c (which triggers onUpdate)
3) onUpdate pulls values for a, b, and c - and sticks them in the invisible (hideAnswers: true) answer blanks "[_]" in the PGML
3) hitting "submit" evaluates this problem as correct on all three "answers" - a, b, and c

5) Figure out how to get GeoGebra to do answer checking, we we send back 1 or zero for correct or incorrect. In other applets, so I think I can make it work.

WeBWorK is already evaluating your answers accurately, as you have implemented scripts to pull the data from GG into the WW "AnSwEr" blanks whenever things change in GG (onUpdate). Which means that whenever the student hits "submit", the answers in the invisible answer blanks ("[_]") have already been updated with the current values from GeoGebra.

See the attached SS for clarification:
(notice that "C=" blank space, because $C cannot be pulled into WW as you requested; which is not the same as letting WW _evaluate_ variables from GG)

Attachment Screen_Shot_2019-05-29_at_11.13.33_AM.png
In reply to Andrew Parker

Re: Using GeoGebra in WeBWorK problems

by Alex Jordan -
One gotcha with using GGB applets in problems, is what happens when you are on a page that loads multiple problems? Like in the Library Browser, or a Set Details page?

First, multiple instances of the geogebra javascript are all going to be loaded.

Second, it's likely that the applet names collide. Like the same generic "ggb" from your example is used by multiple problems. So they will all look like whatever the last one is supposed to look like.

It's probably also a problem that the HTML on pages like that will have multiple divs that all have the same id, "ggb".

I believe that what I am doing with a GeoGebra MathObject "solves" the latter problem by automatically making the applet name (corresponding to "ggb") be generated as a random string.

I'm still not sure how to make sure that deployggb.js is not needlessly loaded 100 times in a Library Browser page. Any ideas? It's been several months since I was last thinking about this.
In reply to Alex Jordan

Re: Using GeoGebra in WeBWorK problems

by Michael Gage -
I've wondered what is the best javaScript practice to accomplish this. I think this is part of the 'singleton' pattern discussion: https://blog.mgechev.com/2014/04/16/singleton-in-javascript/. And it arises with all of the javaScript codes that we insert into pg problems so if you find a good solution think about implementing it generally.