MODX Community

Trying to create a fun quiz for the lockdown

Hi There,
During this strange time in our world, I’m trying to set up a fun quiz for people to play while they are on lockdown. I wanted to offer a small prize, so they would need to enter some contact details to play.
I’m thinking of having a container as a quiz, then resources as question. The pagetitle could be the question an the longtitle could be the anwser.

I’m just not sure how to pull it all together so that they get a percentage score, and their details are added to the database so i can choose a winner from the best scores.

Anyone got any ideas on how to pull this together?

Stay safe people!

Andy

I personally would probably set this up through a multi-page form with formit. That way you can ask for any contact details as well as provide questions on individual pages and carry a score by giving the answers either 1 or 0 as a value which you can add up. When the user submits the form you could also use the formitSaveForm option so you have all submitted entries and can easily pick a winner by the person with the highest score.

That’s just my attempt, though :wink:

Hmm that sends interesting. I haven’t used Formit much so will need to read the docs on how to implement a multi page form and how to carry across the values etc.

Thanks for the idea!
Andy

So I’m getting somewhere with this. I have set up a multi page form, which multiple choice questions using radio buttons.

But what I’m not sure about is ho to add the ‘scoring’ to the questions.
For example:

Q1: How tall is Andy?
[A ] 185cm
[B] 195cm
[C] 205cm

The answer is B, but how do I translate that to a score of 1, and all others get a score of zero?
Finally I need a way to add them all up in a hidden field at the end to give them a score.

Can anyone help?

Thanks!
Andy

On the email I can use output modifiers to score the quiz - so:
[[+fi.one:is=1974:then=1:else=0]]
[[+fi.two:is=195cm:then=1:else=0]]

But how would I add those up to a final score?
And then pass that final score to a final hidden field so that it’s stored in the database of submissions?

Getting there slowly!
Andy

So = I’ve sort of worked it out - but using loads of output modifiers, which I do’t think is very good for processor etc?

<p>Total Score is:

[[+fi.one:is=1974:then=1:else=0
:add=[[+fi.two:is=195cm:then=1:else=0]] :multiply=100:divide=2]]%

Is there a better way?

I think that if i have 10 questions, my solution is going be be very messy and inefficient.
I feel that i need a snippet which takes the raw values of each answer and checks it against a list of answers, and then creates a score from those.

I just don’t know how to do it (I’m not a PHP person at all, so don’t know where to start).

Any help would be much appreciated!

Thanks
Andy

OK - so I’ve been playing around with this for hours, and have written a snippet which i’m sure isn’t great - but it SORT OF WORKS! (I’m trying!! lol)

So this is my Snippet:

// calculate score
$one = $hook->getValue('one');
$two = $hook->getValue('two');

if($one == '1974') { $score1 = '1'; }
if($two == '195cm') { $score2 = '1'; }

$modx->setPlaceholder('Score', $score1+$score2);
$hook->setValue('Score', $score1+$score2);
return true;

Are you can see, I’ve used setPlaceholder AND setValue.

I’m using these hooks in my Formit call:
calculate,email,FormItSaveForm,redirect

The reason for this (and I’m sure it’s wrong) is that if I use ‘setPlaceholder’ only, the email contains the score, but the database doesn’t. But if I use ‘setValue’ then the database also captures the score.

I have a ‘thanks’ page which is supposed to show the users score.
I’ve put the [[+Score]] placeholder on that page, and have used [[!FormItRetriever]] to pull the data in, but the placeholder is empty.

So basically, what i want to do is pull together the combined score from all the questions, email them to the user, show them on the thanks page, and also store them in the database.

I think I’m pretty close - but not quite there!!

Thanks
Andy

I’m also not the best in php, but for carrying the score, can’t you just use the value tag within the radio options? That way you can name the the answers whatever by a label but the right score would be transferred further.

You’d have to prohibit cheating by looking at the source code though.

Thanks - yes the reason I didn’t do that was because it would be easy to cheat.