|
|
|
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 true;
}
}
}
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))
{
question_require_capability_on($questionid, 'edit');
error('Was not able to hide question');
}
}
else
{
delete_question($questionid);
}
}
This approach has been tested and we don't see any adverse side-effects.
|
|
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 true;
}
}
}
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))
{
question_require_capability_on($questionid, 'edit');
error('Was not able to hide question');
}
}
else
{
delete_question($questionid);
}
}
This approach has been tested and we don't see any adverse side-effects. |
Show » |
made changes - 31/Oct/09 08:23 PM
| Field |
Original Value |
New Value |
|
Summary
|
Question Engine can delete questions used by modules other than QUIZ
|
Question Engine delete questions in use by modules other than QUIZ
|
|
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.