Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Critical
-
Resolution: Won't Fix
-
Affects Version/s: 1.9.6
-
Fix Version/s: None
-
Component/s: Questions
-
Labels:None
-
Difficulty:Easy
-
Affected Branches:MOODLE_19_STABLE
Description
When deleting questions Moodle tries to detect used ones but only query a table from the QUIZ module. This breaks the plug-ability of this part of Moodle.
Problematic code (editlib.php):
if (record_exists('quiz_question_instances', 'question', $key)) { $questionnames .= '* '; $inuse = true; }
We propose this set of changes to solve the issue:
Change above lines to be:
$inuse = question_is_question_in_use($key);
And define the new function:
function question_is_question_in_use($questionid)
{
global $CFG;
require_once($CFG->libdir . '/ddllib.php');
$modules = get_records("modules");
// Scan all modules looking for a table named modulename_question_instances
$question_in_use = false;
foreach ($modules as $module)
{
$table = new XMLDBTable($module->name.'_question_instances');
if (table_exists($table))
{
if (record_exists($module->name.'_question_instances', 'question', $questionid))
}
}
return false;
}
This way default functionality remains the same and new modules can register the use of question instances by defining a table named "mdlprefix_modulename_question_instances" (like QUIZ does).
The same piece of code is reused in the function question_showbank_actions:
if (record_exists('quiz_question_instances', 'question', $questionid)) {
if (!set_field('question', 'hidden', 1, 'id', $questionid)) {
question_require_capability_on($questionid, 'edit');
error('Was not able to hide question');
}
} else {
delete_question($questionid);
}
We propose to change it to:
question_delete_or_hide($questionid);
Being this function:
function question_delete_or_hide($questionid)
{
$question_in_use = question_is_question_in_use($questionid);
// If the question is instanced in any module do not delete it, just hide it.
if ($question_in_use == true)
{
if (!set_field('question', 'hidden', 1, 'id', $questionid))
}
else
}
This approach has been tested and we don't see any adverse side-effects.
I do not think this would be correct approach in 2.0, instead we have new plugin_supports(), you would get list of all plugins and then query them all to see if they use.
Also please note that this would not work for all plugins/modules because there is a limit on table name length - "_question_instances" suffix is definitely too long.