Create a variable in a Snippet based on a field in a particular resource

This is part of a quiz I’m building - but I’m stuck on this.
I have a snippet which contains the below scoring system:

if($one == '1974') { $score1 = '1'; }
if($two == '195cm') { $score2 = '1'; }
if($three == 'Brown') { $score3 = '1'; }
if($four == 'Melted') { $score4 = '1'; }

So the answers are hard-coded into the snippet. (1974,195cm,Brown,Melted)
I want to replace those values with the longtitle tag from various resources, to make it easier to manage the answers.

e.g. I want to replace 1974 with the longtitle field of resource 10 (who’s value is 1974).

So I’m thinking of something like this:
$answerOne = $modx->getObject(‘modResource’, 10)->get(‘description’);

if($one == '$answerOne') { $score1 = '1'; }
if($two == '195cm') { $score2 = '1'; }
if($three == 'Brown') { $score3 = '1'; }
if($four == 'Melted') { $score4 = '1'; }

But I know the syntax is all wrong…

Anyone help?



I’ve made progress on this one. Below is my snippet.
The only thing I’m not too happy with is that the resource IDs are hard coded -

// get date of submissions
$datestamp = date('d-m-Y H:i:s');
$hook->setValue('datestamp_submitted', $datestamp);

// get values from form
$one = $hook->getValue('one');
$two = $hook->getValue('two');
$three = $hook->getValue('three');
$four = $hook->getValue('four');

// get correct answers (stored in description field of resources)

 $resource = $modx->getObject('modResource','1029');
 $answerOne = $resource->get('description');
 $resource = $modx->getObject('modResource','1034');
 $answerTwo = $resource->get('description');
 $resource = $modx->getObject('modResource','1035');
 $answerThree = $resource->get('description');
  $resource = $modx->getObject('modResource','1030');
 $answerFour = $resource->get('description');
// calculate scores

if($one == $answerOne) { $score1 = '1'; }
if($two == $answerTwo) { $score2 = '1'; }
if($three == $answerThree) { $score3 = '1'; }
if($four == $answerFour) { $score4 = '1'; }

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

How’s does this look for a first ever snippet attempt?
Hoping to improve it as I’m sure it’s not as efficient as it should be.

Looks good (though that’s not the longtitle field). Do you need separate scores? Otherwise you could total them in the snippet, using just one value $score and $score++.

Thanks bob. Yes I changed the field because I was using the longtitle for the question in the end.
I will try what you said about combining the scores though. Thanks.


I’ve just thought - If I store all the questions and answers in ClientConfig then I wouldn’t have to look up all the resources individually.

Would they be more efficient?

It would certainly work, but it would make editing them kind of tedious.

I think I’d store all the questions and answers in one place, say a resource or chunk with tokens at the beginning of each question and answer (Q: and A:), then parse that. The EZfaq extra uses that method.

Another way, if your writing them yourself, would be to just create a .php file with an associative array of questions and answers and “include” it in your code.

Thanks Bob - I think i’ll go with using clientConfig because I can get my head roudn the method!!

So I’ve created my config values and pulled them into the form.
But in the snippet which calculates the score, I have this - which I think must be wrong, because it’s not scoring correctly:

if($one == '[[++Q1-correct]]') { $score1 = '1'; }

So [[++Q1-correct]]’ Is the correct score in clientConfig.

What have a done wrong here?


I think I’ve cracked it actually bob - I used this in the snippet to pull in the setting;

$answerOne = $modx->getOption('A1-correct');

Then this to calculate the score:

if($one == $answerOne) { $score1 = '1'; }

It seems to work fine - thanks for your advice though!

Sorry Bob - I tried to simlify the scoring as you suggest by changing to this:

if($one == $answerOne) { $score = '1'; }
if($two == $answerTwo) { $score = '1'; }
if($three == $answerThree) { $score = '1'; }
if($four == $answerFour) { $score = '1'; }

$hook->setValue('Score', $score++);

But that didn’t work - I assume I’ve interpreted your suggestion wrongly. It only scores 1 of the 4 scores.

The ++ goes above:

$score = 0;
if($one == $answerOne) { $score++; }
if($two == $answerTwo) { $score++; }
if($three == $answerThree) { $score++; }
if($four == $answerFour) { $score++; }

$hook->setValue('Score', $score);

Aha! Thank you. I feel so dumb!!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.