Calculated questions setting parameters to be > 0

Calculated questions setting parameters to be > 0

by John L -
Number of replies: 19

Hey everyone,

Thanks in advance for any assistance to my questions. Currently I have two separate problems when trying to set up questions:

1) I'm trying to set up a simple addition, subtraction question for question bank/quiz. Something like {a} + {b} - {c}. However, I would like to set the question up so that {a} + {b} is > {c} and also that the final answer is > 0. Young students are not able to grasp negative concepts yet so I've been trying to figure out how to format the formula when creating the question, but not able to come up with an answer.

2) I'm trying to set up another question, maybe moreso numerical question type than calculated question type. The question will be to list a multiple of a certain number (i.e. 2). There won't be one answer to this question as there are many different multiples. Is there a way to set up a formula to take all correct multiples of a certain parameter? I dread typing in answers for all the different multiples of the question (it's not just up to x12).

Thanks again!

Moodle version: 3.8 (latest)

Average of ratings: -
In reply to John L

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers
Hello John,

For your first question, should all numbers be integers?
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by John L -
Hi Dominique,

Thanks for your response. Yes, for both questions, all are integers. No decimals whatsoever.
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello John,

For your first question, you can use the old trick of doing the math backwards, that is, starting with the answer. I will assume that you want integers. You have a + b - c = d and you want the answer d to be positive. Thus, starting with the answer d, you must define c = a + b - d.

You also want a + b > d, that is, you don't want c to be negative in case |c| > a + b. You can do this in different ways. Here's one way: define {x} as a random variable between say 0.1 and 0.9, and define y = 1.5 - x.

More precisely:

  • Define {d} as a random variable of any positive numbers, for example between 11 and 99.
  • Define {x} as a random variable between 0.1 and 0.9.
  • Write the question as follows : What is {=ceil({x}*{d})} + {=ceil((1.5-{x})*{d})} - {=ceil({x}*{d})+ceil((1.5-{x})*{d})-{d}}?
  • The correct answer is {d}.

You should get something like this:

XML file attached:

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

For your second question, see MoodleFormulas:

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello John,

Doing the math backwards, as I presented above for your first question, is a nice trick. You can use it in many quizzes.

However, this question is simple enough to address it more directly:

  • Define {a} and {b} as a random variables between say 5 and 95.
  • Define {x} as a random variable between 0.100 and 0.900.
  • Write the question as follows: What is {a} + {b} - {={a}+{b}-ceil({x}*({a}+{b}))}?
  • The correct answer is ceil({x}*({a}+{b}))

Note that the Formulas and STACK question types are more powerful than the Numerical and Calculated question types for STEM (science, technology, engineering and mathematics) quizzes.

Average of ratings: Useful (1)
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello John,

As you mentioned in your initial post, the Numerical question type might very well suit your second question. In fact, I must admit that here it is probably better suited than the Formulas question. In any case, I believe you will need to write several different questions for some multiples of different numbers and then choose these questions randomly.

In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

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

Hi Dominique and John,

The Variable Numeric, Variable numeric set, and Variable numeric set with units type of questions are great for these types of questions. You only need to set up one question using a random number range for the variables, to get hundreds of variations of the same problem.

I have a bunch of them still left over from my teaching days on an old Moodle. Let me know if you would like a set, and I'll export and upload them here.

Average of ratings: Useful (1)
In reply to AL Rachels

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello AL,

Let me know if you would like a set,...

Thank you very much for your offer. I would definitely like to have a set.

In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

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

Re: Calculated questions setting parameters to be > 0

by John L -
Hello AL,

Thank you for the Moodle XML format and your assistance. I don't think my Moodle has the Variable Numeric question type since this error message popped up: "Error importing question Question type varnumeric is not supported by XML import." Do you have any suggestions on how this error could be rectified?

In terms of what I'm using, it is the latest Moodle version 3.8.2 using the Fordson theme.

Thanks in advance!
In reply to John L

Re: Calculated questions setting parameters to be > 0

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

Hi john,

To make the questions import, you need to install the three Variable numeric type questions along with the Parameter matching type question. It is required by the Variable numeric with units question type.

In reply to AL Rachels

Re: Calculated questions setting parameters to be > 0

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

Oops...bad memory. That last one should be Pattern matching.

In reply to AL Rachels

Re: Calculated questions setting parameters to be > 0

by John L -
Hey AL,

Just installed the Variable numeric sets + Pattern matching and was able to upload your XML files. Thanks!

I was playing around with the parameters and I can't seem to rearrange it to what I need.
The student's question would be as follows: Please list a multiple of 2.
The student's answer should be any multiple of 2.

Would I be able to use this Variable numeric question type to fulfill that role? Perhaps by putting some formula into the answer field where any answer the student answers would be divided by 2?

I've entertained Dominique's idea, but that would require me to individually set up all the multiples of 2 (i.e. 2,4,6, etc.) which would take up a lot of answer fields since I'd like the multiples to go on to a very high max like 1000. I'm wondering if there would be any way to input a formula in the answer key which Moodle would divide the student's answer by 2 and let them know whether it's correct or not (whether the answer is a whole number or incorrect with decimals).

Any thoughts? Thanks again for your help AL and Dominique!
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by John L -
Hello Dominique,

Thank you for your help! I really appreciate it as the coding is really giving me a headache. I was experimenting around this weekend with the formula and managed to get it somewhat working. I have two questions that have arisen from tinkering around with the formula:

1) I've found that it is possible to scale up or down the formula to change the question longer and shorter. However, I've been doing it with adding addition of integers. For example, What is {a} + {b} - {={a}+{b}-ceil({x}*({a}+{b}))} + {c} + {d}? with the correct answer being ceil({x}*({a}+{b})) + {c} + {d}. I've had no problem with this.

Is it possible to add another subtraction element later in the question? I've tried: What is {a} + {b} - {={a}+{b}-ceil({x}*({a}+{b}))} + {c} + {d} - {={c}+{d}-ceil({x}*({c}+{d}))} ? For the code leading to the correct answer, I tried: ceil({x}*({a}+{b})) + {c} + {d} - ceil({x}*({c}+{d}) but that hasn't really worked out for me...

2) After I've entered the parameter's min/max values, generated/displayed wildcards, and saved the changes, I've noticed that the min/max values revert back to a default 0.1 min and 10.0 max when I click edit question at a later time. Therefore when I preview the question, it doesn't really seem to be saving my latest changes and displays old parameters or the default parameters for min/max values. Am I missing something here?

Thanks again so much for your help. I didn't think it would be this complicated to create simple addition/subtraction questions. I have not tried your solution for my original Question 2 as it appears I would have to enter all of the multiples in individual answer fields. This would be cumbersome if I had a question like: List a multiple between 0 < x < 200.
In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by John L -
Hey Dominique,

I looked into MoodleFormulas and it seems like the best option right now for my second question. However, can you confirm that the Formulas plugin is compatible with Moodle 3.8? It appears to only support up to Moodle 3.6 and thus, no direct installation button from the Plugins directory.

Thank you!
In reply to John L

Re: Calculated questions setting parameters to be > 0

by John L -
Just a follow up on this. I just went ahead and installed through plugins zip file via the site administration -> plugins -> install plugins option. It appears to be installed correctly. I will update as to whether Question 2 is resolved via your method Dominique.

I did further investigating with Question 1's wildcard issue. Seems that it's linked to a recurring bug MDL-37744 (http://tracker.moodle.org/browse/MDL-37744) and MDL-45915 (https://tracker.moodle.org/browse/MDL-45915) which appears to be unresolved.
In reply to John L

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello John,

Good news! In fact, your question 2 is very easy to do with the Formulas question (simply replace 7 by 2):

ForumQuiz_20200323_1227.png

MoodleFormulas.org (second example)

XML file attached:

In reply to Dominique Bauer

Re: Calculated questions setting parameters to be > 0

by Dominique Bauer -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Plugin developers

Hello John,

With the Formulas question type, your question 1 can be very easily worked out. In the following example, only the final answer is a positive number. If you proceed progressively from left to right, intermediate results may be negative. Any ways, you can easily modify this example to suit your specific needs.

Formulas_20200325_0047.png?time=1585111691218

See MoodleFormulas.

xml file attached;