-
Bug
-
Resolution: Fixed
-
Minor
-
4.3.6
I'm seeing this coding error on the quiz editing page:
Coding error detected, it must be fixed by a programmer: Invalid context id specified context::instance_by_id()
on mod/quiz/edit.php when attempting to edit a quiz.
As mentioned in MDL-82499 this is a misleading message, but the root cause is a "can't find record in database" error in question_has_capability_on(). I've tracked this down to the fact that the quiz contains a question where the createdby field is null.
I appreciate that this is probably bad data in our database (I suspect from an automated migration from WebCT in 2009), and I'm going to fix it (all 100,000+ of them!), but it surfaces a couple of issues with the Moodle code:
- mod_quiz\structure::has_use_capability() is passing a value to the $questionorid parameter of question_has_capability_on() that is neither a question nor a question id - it's a "question structure" object which appears to have all the fields from the question table but the id is the slot id, not the question id
- question_has_capability_on() is using isset($questionorid->createdby) to check whether the passed object has the required data from the question table, so if createdby exists but is null it returns false and goes and looks up the question by the id provided (which is actually the slot id if it's been called from structure::has_use_capability())
It looks like question_has_capability_on() will work as expected with null createdby if the isset() call is changed to property_exists(), although this would just cover up issue #1 again.
I can create a patch for issue #2 but I'm not sure what to do about issue #1.
I also wonder if it would be worth changing the definition of the question table so that createdby is not allowed to be null. I'm assuming it isn't supposed to be null under normal operation of Moodle, although I don't know what knock-on effects this could have.
It's also probably worth noting that this didn't happen in Moodle 4.1, where these quizzes can be edited fine.
- has a non-specific relationship to
-
MDL-82499 question_has_capability_on() gives misleading error if question not found
-
- Closed
-