Calculated question - if function as in excel

Calculated question - if function as in excel

by Katharina Herich -
Number of replies: 17

Hi

I am using Moodle 2.5.1.

I like to create a calculated question using wildcards and an 'if'-argument in the formula as in excel. Is this possible?

I am using 'shared wildcards' and the formula to be used depends on the result of a previous question.

Thanks,Kat

 

Average of ratings: -
In reply to Katharina Herich

Re: Calculated question - if function as in excel

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

It is tricky, but you can probaly do it because PHP considers true and false to be 1 and 0.

So something like

{=({test} == 42)*{value1} + ({test} != 42)*{value2}}

(I have not tried this.)

In reply to Tim Hunt

Re: Calculated question - if function as in excel

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

The following syntax should work 

What is {=({x}==10?11:{x})}

Which would check to see if x is equal to 10 and if it is then show 11 instead, otherwise show the value of x. I have tested that at some point though I am not sure why I came up with that expression. However in my experience there is little checking of the validity of these types of expressions when you save a question so you might consider turning on debugging via

Site Administration/Development/Debugging and select DEVELOPER:extra Moodle debug messages for developers

(best done on a test site and not a live site or you may make more information visible than is desirable)

A common error with formula is that brackets { } or parentheses ( and )  do not match. There are free tools to allow you to check these such as the programmers editor Notepad++ 

http://notepad-plus-plus.org/

If you key your formula into it and move your cursor over the { or ) it will highlight the matching closing character

 

In reply to Marcus Green

Re: Calculated question - if function as in excel

by Katharina Herich -

Hi Tim, Hi Marcus,

Thanks for the quick reply. I am not a programmer, I will first digest your suggestions and come back to you if it worked smile

What language is Moodle using? C++? Might be worth for me to take a basic course smile

 

Thanks,

Kat

 

In reply to Katharina Herich

Re: Calculated question - if function as in excel

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

I AM a programmer and I still make mistakes in expressions like thatsad. However if you can do logical expressions in Excel you can certainly do that type of expression in Moodle questions.

Moodle is written in PHP which is generally much, much easier than C++ and while it is certainly a interesting to know what is going on behind the scenes it is not essential.

Please post here again if you have problems or even if you get some nice question logic working as it is not widely documented at the moment.

In reply to Marcus Green

Re: Calculated question - if function as in excel

by Pierre Pichet -

Thanks Marcus for correctly handling this (? a : b  ) option which should be handled with care.

(MDL-26823)

Pierre

 

In reply to Marcus Green

Re: Calculated question - if function as in excel

by Katharina Herich -

Hi Markus,

Thanks for the tip with Notepad++. It really helps with all these brackets etc.

I finally got to try your suggestion above {=({x}==10?11:{x})}, but I need some more help. Like I said, I have idea about programming and the only program I know well is excel. I have following questions:

  1. Why do you put {} at the whole string above? I only use the {} bracket for identifying the wildcards;
  2. Why do you double up the equal sign? What does it mean? What is the difference to a single equal sign? Does Moodle understand '>=' for bigger or equal and '<=' for smaller or equal;

My formula is getting really long and I was looking for a way to shorten it. I split questions up in sub-questions and share & synchronize the wildcards, but the last questions includes all the previous formulae and is getting particularly long. Isn't it possible to define in the each of the sub-questions another wildcard, i.e.

{Sum1} = ({a}+{b}+{c})

{Sum2} = (2*{a}+{b}+3*{c})

Total = {Sum1}*{Sum2}

Thank you very much for all your help.

Katharina

In reply to Katharina Herich

Re: Calculated question - if function as in excel

by Pierre Pichet -

Hi Katharina,

You cannot define a wildcard as the sum (or other function) of other wildcards in calculated questions.

Pierre

 

 

In reply to Katharina Herich

Re: Calculated question - if function as in excel

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Glad to hear you found the notepad++ tip useful as when I wrote that in a document I was convinced nobody would ever try it. From my background as a programmer I assume that the double equals == is the comparison operator i.e. "are these two the same", whereas  a single = is the assignment operator, i.e. a variable such as x takes the value of 10 if you run x=10.

In some products the single = doubles up for both tasks and the meaning is inferred from the context but with that approach there is ambiguity. I will look into the other parts of your question. 

In reply to Marcus Green

Re: Calculated question - if function as in excel

by James Roberts -

Marcus, thank you!

The syntax of {=({x}==10?11:{x})} works like a charm.

Incidentally Katharina, when I have a complicated formula in a calculated question, I have resorted to creating a formula in a single cell in Excel as a model.  Then, in a  different Excel cell, I translate the code to Moodle functions/syntax and finally copy-paste this translation into the smallish textbox in Moodle.

In reply to James Roberts

Re: Calculated question - if function as in excel

by James Roberts -

Charm?  I spoke/posted too soon.

Sorry Marcus, your solution doesn't work.  In Moodle, your php syntax simply returns (always) the first (True) option.

To return to the OP, can someone provide an "If function" for Moodle?

In reply to James Roberts

Re: Calculated question - if function as in excel

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Can you give me the exact syntax you are using.

In reply to Marcus Green

Re: Calculated question - if function as in excel

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Here is a shot of the following code with the values of X being set to either 1 or 10.

What is {=({X}==10?11:{X}) }

{X}

calcdemo

In reply to Marcus Green

Re: Calculated question - if function as in excel

by Marcus Green -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

What is the full scenario of your question? My example is just to demonstrate the idea and I cannot think of why or where this feature might be used.

In reply to Marcus Green

Re: Calculated question - if function as in excel

by James Roberts -

Marcus,

I thought that I had replied to your post weeks ago but apparently my reply was not saved.

The context is adding something as mundane as a "0" to a price so that $2.1 appears as "$2.10".

Your code works.  (Thank you!)  It seems that I faced two hurdles.  One was dealing with/learning how Moodle generates/saves random wild cards in a calculated question.  The second was the infamous Intel "zero".  It seems that Moodle (because of php?) occasionally decides that, accurately, a remainder is zero, but sometimes it's a smidgen above/below zero.  Your "if statement" gave false readings because of this.  I solved the problem by rounding.

For the record, in case anyone comes this way, here's the code:

{Fut11}{=(round(fmod(({Fut11}+0.01)*10,10))==(0)?".00":"0") }

I used Pierre's trick to add a small number (0.01), then multiply by 10 and then round.  If Fut11 is an integer (eg. $7), it gets ".00" concatenated.  If it has a non-zero decimal (eg. $5.3), "0" is concatenated.

In reply to James Roberts

Re: Calculated question - if function as in excel

by K Morrow -

I don't think it's so much an "intel" zero, as the fact that you're probably using floating point math. Not to get into the specifics, but 0.10 actually cannot be exactly represented in "normal" floating point no matter how many bits you use. It's a value very very close, but not exactly 0.10. For floating point comparisons, best practice instead of "value == answer" as a test is to do what you say above, or to test abs(value-answer) < tolerance, where tolerance is something very small.

I skimmed the replies, but didn't see if someone answered this part: the double-equals is a "is equal to" as opposed to a single equals being "gets the value of"--a common usage in programming.

Be warned that the use of text in the conditional (the stuff in quotes) is not dependable. I have been playing with this because I want to display values in base-16, which requires using the letters A-F. I'd have a conditional that displayed text, but it would work in some questions, and not in others, or work for some text, but not for other text (for the exact same condition!).

Last note: In my install, I can do == with the conditional operator, but not <, >, <=, or >= because the > and < characters are translated to &gt; and &lt; respectively by the editor, which is not re-interpreted back as > or < by the calculated question engine. So I end up having to do it the clunky way of checking abs(value1-value2)==(value1-value2) or various other work-arounds. Same for bit-masking -- I can't use value1&1 to find the value of the least significant bit because the & is changed to &amp;

You might be able to tell that I've played with this type of thing a lot. :P

It really be nice to be able to express a format string for a calculated value (like printf), both in the question text and the answers... It's a bit unfortunate that, for calculated questions that require an answer in hexadecimal, I have to restrict the datasets to those that produce hexadecimal results that use only digit values 0-9, and I have to hard-code an occasional A, B, C, D, E, or F into the question text just so that students don't start thinking that hex is another name for decimal.

Yes, I can use a set of questions and pick randomly from that. But that's clunky when I want 30+ versions--like if I need to fix a typo, etc. I also need to separately verify each one in that case instead of verifying the corner cases with calculated and then just adding more datasets. More questions == more places for there to be a problem.

In reply to K Morrow

Re: Calculated question - if function as in excel

by Pierre Pichet -

"You might be able to tell that I've played with this type of thing a lot"

I agree smile

 

In reply to Katharina Herich

Re: Calculated question - if function as in excel

by James Roberts -

I have upgraded from Moodle 2.4 to Moodle 2.8 and now I am getting errors. Is the previously accepted syntax:

{=({Day03}==1?"+":"") }

causing the errors?