Skip to content
Snippets Groups Projects
Commit 2e1ed6d6 authored by Tim Hunt's avatar Tim Hunt
Browse files

Purge the question definition cache in Moodle 2.4+.

From Moodle 2.4 onwards, question definitions are cached, so when
we update the question definition in some way that Moodle core does
not know about (for example when deploying variants) we must remember
to notify the question cache, so that it can refresh the definition
of that question.
parent e3d1639d
No related branches found
No related tags found
No related merge requests found
...@@ -50,19 +50,14 @@ $PAGE->set_pagelayout('admin'); ...@@ -50,19 +50,14 @@ $PAGE->set_pagelayout('admin');
// Process deploy if applicable. // Process deploy if applicable.
$deploy = optional_param('deploy', null, PARAM_INT); $deploy = optional_param('deploy', null, PARAM_INT);
if (!is_null($deploy)) { if (!is_null($deploy)) {
$record = new stdClass(); $question->deploy_variant($deploy);
$record->questionid = $question->id;
$record->seed = $deploy;
$DB->insert_record('qtype_stack_deployed_seeds', $record);
redirect($nexturl); redirect($nexturl);
} }
// Process undeploy if applicable. // Process undeploy if applicable.
$undeploy = optional_param('undeploy', null, PARAM_INT); $undeploy = optional_param('undeploy', null, PARAM_INT);
if (!is_null($undeploy)) { if (!is_null($undeploy)) {
$DB->delete_records('qtype_stack_deployed_seeds', $question->undeploy_variant($undeploy);
array('questionid' => $question->id, 'seed' => $undeploy));
// As we redirect, switch to the undeployed variant, so it easy to re-deploy // As we redirect, switch to the undeployed variant, so it easy to re-deploy
// if you just made a mistake. // if you just made a mistake.
...@@ -83,7 +78,7 @@ if (!is_null($deploy)) { ...@@ -83,7 +78,7 @@ if (!is_null($deploy)) {
$failedattempts = 0; $failedattempts = 0;
$numberdeployed = 0; $numberdeployed = 0;
while ($failedattempts<$maxfailedattempts and $numberdeployed<$deploy) { while ($failedattempts < $maxfailedattempts && $numberdeployed < $deploy) {
// Genrate a new seed. // Genrate a new seed.
$seed = mt_rand(); $seed = mt_rand();
$variantdeployed = false; $variantdeployed = false;
...@@ -130,10 +125,7 @@ if (!is_null($deploy)) { ...@@ -130,10 +125,7 @@ if (!is_null($deploy)) {
} }
// Actually deploy the question. // Actually deploy the question.
$record = new stdClass(); $question->deploy_variant($seed);
$record->questionid = $question->id;
$record->seed = $seed;
$DB->insert_record('qtype_stack_deployed_seeds', $record);
$numberdeployed++; $numberdeployed++;
} }
} }
...@@ -145,4 +137,3 @@ if (!is_null($deploy)) { ...@@ -145,4 +137,3 @@ if (!is_null($deploy)) {
} }
redirect($nexturl); redirect($nexturl);
} }
...@@ -816,4 +816,20 @@ class qtype_stack_question extends question_graded_automatically_with_countback ...@@ -816,4 +816,20 @@ class qtype_stack_question extends question_graded_automatically_with_countback
return $classification; return $classification;
} }
/**
* Deploy a variant of this question.
* @param int $seed the seed to deploy.
*/
public function deploy_variant($seed) {
$this->qtype->deploy_variant($this->id, $seed);
}
/**
* Un-deploy a variant of this question.
* @param int $seed the seed to un-deploy.
*/
public function undeploy_variant($seed) {
$this->qtype->undeploy_variant($this->id, $seed);
}
} }
...@@ -557,6 +557,48 @@ class qtype_stack extends question_type { ...@@ -557,6 +557,48 @@ class qtype_stack extends question_type {
$transaction->allow_commit(); $transaction->allow_commit();
} }
/**
* Deploy a variant of a question.
* @param int $questionid the question id.
* @param int $seed the seed to deploy.
*/
public function deploy_variant($questionid, $seed) {
global $DB;
$record = new stdClass();
$record->questionid = $questionid;
$record->seed = $seed;
$DB->insert_record('qtype_stack_deployed_seeds', $record);
$this->notify_question_edited($questionid);
}
/**
* Un-deploy a variant of a question.
* @param int $questionid the question id.
* @param int $seed the seed to un-deploy.
*/
public function undeploy_variant($questionid, $seed) {
global $DB;
$DB->delete_records('qtype_stack_deployed_seeds',
array('questionid' => $questionid, 'seed' => $seed));
$this->notify_question_edited($questionid);
}
/**
* From Moodle 2.4 onwards, we need to clear the entry from the question
* cache if a question definition changes. This method deals with doing
* that without causing errors on earlier versions of Moodle.
* @param int $questionid the question id to clear from the cache.
*/
protected function notify_question_edited($questionid) {
if (method_exists('question_bank', 'notify_question_edited')) {
call_user_func(array('question_bank', 'notify_question_edited'), $questionid);
}
}
/** /**
* Load all the question tests for a question. * Load all the question tests for a question.
* @param int $questionid the id of the question to load the tests for. * @param int $questionid the id of the question to load the tests for.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment