All Collections
Authors
Authoring techniques
Multiple exercises from one exercise with selectors
Multiple exercises from one exercise with selectors

Learn how to use multiple exercises from one exercise with selectors

Updated over a week ago

We often find ourselves in a situation where we have one kind of exercise, but we want different variations on it. For example, say you have the following exercise which asks you to add two integers:

$a + $b

We may specify $a = rand(1,10) and $b= rand(1,10), for the first exercise in this package. And $a = rand(11,100) and $b= rand(11,100), for the second exercise in this package. And $a = rand(101,1000) and $b= rand(101,1000) for the third question.

For question 4,5 and 6, we do the same with the operator changed from plus to minus, and so on, and so forth. Because those 6 exercises are so similar in nature, they probably have similar feedback rules, a similar explanation in the solution, a similar solution field, similar hints, etc.

We often use the following strategy in those kind of exercises.

Step 1

 Call the first variable $selector, and give it as value an array with the keys being a description of the instance. We usually use integers as values.

We use the PHP 5.4+ array notation. For more info, see http://php.net/manual/en/language.types.array.php.

In the platform, variables which are arrays are interpreted as taking a random item from the array.

Step 2

The following variable is called json. Here, all the properties which are unique to the selector can be specified. You could also use conditionals ($selector == 0 ? rand(1,10) :$selectorr == 1 ? ... etc.etc.). The advantage of this method is that all the data specific to a given selector is given in one place.

The function json_encode is used to make a string from an array. At this moment, the SOWISO platform only understand variables which are strings. If you want to use an array you have to encode it as a string first.

Step 3

We can now extract the variables in the following way:

The function json_decode is used to make an array from a string, (if the second parameter is set to true). In this way you can access a value from the array which is encoded as a string.

Did this answer your question?