## Question Authors' Forum

### Question with a random variable

Re: Question with a random variable

Dear all.

As Richard says, I have forked coderunner to allow scenario generation (and persistence of scenario and student data at the point when the question was attempted)

In order for the fork to work, you have to:

1) use a less restrictive (allows each task to use more memory) version of jobe: https://github.com/comqdhb/jobe.git

2) use the forked version of coderunner: https://github.com/comqdhb/moodle-qtype_coderunner.git (requires dos2unix to also be installed)

3) when creating the coderunner question, tick the box for using twig AND customisation, then  expand the ADVANCED CUSTOMISATION section  and add code to generate scenario data e.g.

<?php

//NB requires php_task.php in jobe to not use --no-php-ini and increase memory to 20000000

$s="{{ SCENARIO.json | e('c') }}";//this is where twig will insert the json of any feeder scenarios$scenario=json_decode($s);//try to convert the json into an object if (!isset($scenario->data)){// json may not have been valid

$scenario->data=new stdClass(); } date_default_timezone_set('UTC'); //stuff to stop php complaining$scenario->data->now=date(DATE_RFC2822);//the current date and time

srand(); //randomise the random number generator

$scenario->data->mon=rand(3,20);//nb a random number from 3 to 20$scenario->required=array();//specify that this needs no values in order to generate values

$scenario->provides=array('mon','date');//specify that this will generate the 'mon' and date variables$scenario->err_message="";//everything ok

print json_encode($scenario);// send the json back The above example scenario generator may appear over complicated. I have a long term use case which is that the scenario from one question will may be used as the starting point for the scenario data of the current question attempt. This will allow for questions to build on each other and have a common scenario. It therefore assumes that prior scenario data is populated in$s

$scenario->required indicates any prior data that must be provided for this generator to produce new data$scenario->provides indicates what data will be generated if it is provided with suitable data.

Why the level of complication? Well, it allows the quiz to decide which questions can be created and which are dependent on other scenarios.  As you can see in the example, required is empty and will therefore always generate data.

Additionally, you may notice that jobe has a Dockerfile, which will allow you to build a jobe image which can then be used to build a docker container.

What i really want to know is how to intercept the quiz building, so that the scenario data can be built at the start of the quiz, with scenario data being passed to all question attempts.  ANY HELP TIM?

David