Complex-ish calculations for calculated questions

Complex-ish calculations for calculated questions

by Carl Brewer -
Number of replies: 8

G'day,

moodle 3.3 (current release)

I'm working on a slightly complex equation to use for calculated questions, specifically I am trying to generate density altitude given three variables, oat, qnh & altitude.

The formula is somewhat complex, this is what I have so far :


{=  ({alt} + ((1013 - {qnh})*30))   +   (120 * (-{oat} + (15 - abs( (  ({alt} + ((1013 - {qnh})*30))   / 500  ))  ) )  )  }


As you can see, it's pretty messy (and, not quite right) and a nightmare to debug - all these braces!  Is it at all possible to have a function or a variable embedded in this? For example, the ({alt} + ((1013 - {qnh})*30)) calculation occurs twice. It's the formula for pressure height, which gets used twice. I don't suppose there's a way to just do it once and then assign its value to another variable within the question?

Average of ratings: -
In reply to Carl Brewer

Re: Complex-ish calculations for calculated questions

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Carl,

I think that if you can install new question types, this would be much easier to set up using the Variable Numeric question. It can definitely let you set up a pressure height variable and use it in a calculation. In fact, you can set up random number generators for alt, qnh, and oat, so that each time the question is used, you would get a "different" question.

I retired from flying 24 years ago and no longer have any of my reference books, so if you could give me some actual alt, qnh, and oat ranges you are interested in using, I could possibly set up an example or two and give you direct access to them. If you are interested in trying out the question type and want access to a development Moodle with the question type installed, email me here: drachels at drachels dot com

In reply to AL Rachels

Re: Complex-ish calculations for calculated questions

by Carl Brewer -
G'day Al, and thank you for your reply. I managed to get it going, I'm really more interested in cleaning up the syntax.
My formula ended up as this :
{=  ({alt} + ((1013 - {qnh})*30))   +   (120 * ({oat} - (15 - abs( (  ({alt} + ((1013 - {qnh})*30))   / 500  ))  ) )  )  }
and I have used data sets to hold constrained values for oat, alt & QNH.  It works, but it's messy to read and debug while writing the question.
I wonder if there's  any plans within Moodle to incorporate a scripting language for these kinds of things that has a bit more power than the current setup  Mainly the ability to have if statements and maybe functions and variables on top of the current placeholder variables? 
Really, I'm being lazy! I want a bunch of questions from the same algorithm, which I've managed to do,  but it's messy.
In reply to Carl Brewer

Re: Complex-ish calculations for calculated questions

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers

Might you be able to attach a screen shot or scan of what this equation looks like from a textbook?  I am being a little lazy too trying to decipher your equation.

I can tell you that I have used Moodle's built-in "Calculated Multiple Choice" question type for these kinds of situations, and once you master it, it works really well.  Also, some of the "wrong" questions can be calculations.  Once you have the equation correct, it is the relatively easy to say "Make 40 variations of this question."  And the you can present questions that you don't need to grade.  However, I must warn you, mastering the Calculated Multiple Choice question type will take some time.  Yes, maybe messy.  But no other major LMS offers this kind of power.

In reply to Rick Jerz

Re: Complex-ish calculations for calculated questions

by Carl Brewer -

G'day Rick,

I've done exactly that, and have calculated right and wrong answers and solved the problem, albeit inelegantly  smile  It's pretty good, that's for sure and not that hard to get your head around. 

The doco is also very good (and, LaTeX, cool!).  I'm greedy, and the old programmer/systems admin in me wants more though.

"if" statements, local variables etc ... It would be a lot of work to make it happen and as I have no idea about how the back end of Moodle works, save from poking into the SQL a little to change some variables more efficiently, I guess I was asking to see if it had already been done with a plugin or was in the pipe for a future version, or if I'd just missed it with an errant bit of googling.

Thanks again for your reply.  The maths is here, it's a messy one-liner when compacted down - and it would be nice to be able to store pressure height as a variable, instead of filling out the one-liner with it two times.  The temperature, QNH and elevation are the three "given" variables that I have three data tables for, which works just fine and I'm very impressed by how well it all fits together - whoever designed the Moodle question engine did a very VERY good job. :

Image may contain: text
In reply to Carl Brewer

Re: Complex-ish calculations for calculated questions

by Rick Jerz -
Picture of Particularly helpful Moodlers Picture of Testers

Tim is the person who seems to know, and work on, the quiz engine.  Maybe he will see your post and reply.

There was some discussion about this that I recall, see this post in Moodle.org.

Yep, sometimes these equations get messy.

You have probably already found the moodle docs about this.

I too need a little more power for my statistics course.  Some calculations are easy to do in Excel because of Excel's functions, but hard to do in Moodle.

I have not found any other LMS that has "Calculated Multiple Choice" questions.  Once one begins using this question type, no other LMS will do, which is another reason why I use Moodle.

In reply to Carl Brewer

Re: Complex-ish calculations for calculated questions

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hi Carl,

"and it would be nice to be able to store pressure height as a variable, instead of filling out the one-liner with it two times" - That's one of the advantages of the variable numeric type question is that you CAN use variables. In fact, since you can also use a random number generator your variable for pressure altitude could be written as pa=rand_int(1,40)*1000 which would then randomly give you a pa from 1,000 to 40,000 feet. It would be just as easy to set up another variable to give an appropriate ISA for the PA by using another variable of isa=-(2*(pa/1000)-15).

Effectively, if your quiz was say for ten students, your one question would give each of them an entirely different version of the question.

By default, the question will give you space to create 5 different variables and the option to add two more at a time, if needed. Also, Variables can be calculated or predefined.

In reply to AL Rachels

Re: Complex-ish calculations for calculated questions

by lucia liljegren -

I like this feature of variable numeric also. Intermediate values being computed also makes it easier to document a brief solution in the feedback. That's good when questions are used to allow kids to self test for learning. If they get the question wrong then can see a solution with numbers.

In reply to Rick Jerz

Re: Complex-ish calculations for calculated questions

by lucia liljegren -

I like calculated multiple choice too. But I wish the ordering option included "sort by increasing value".   It's a bit odd to have numerical answers be non-sorted.