MDL-20154 quiz: Remove unused random questions
When a random question is removed from a quiz, and it is not in use elsewhere, it should be deleted. Conflicts: mod/quiz/editlib.php mod/quiz/lib.php
This commit is contained in:
@@ -76,6 +76,12 @@ function quiz_remove_question($quiz, $questionid) {
|
||||
$DB->set_field('quiz', 'questions', $quiz->questions, array('id' => $quiz->id));
|
||||
$DB->delete_records('quiz_question_instances',
|
||||
array('quiz' => $quiz->instance, 'question' => $questionid));
|
||||
|
||||
$qtype = $DB->get_field('question', 'qtype', array('id' => $questionid));
|
||||
if ($qtype === 'random') {
|
||||
// This function automatically checks if the question is in use, and won't delete if it is.
|
||||
question_delete_question($questionid);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -170,7 +170,20 @@ function quiz_delete_instance($id) {
|
||||
quiz_delete_all_attempts($quiz);
|
||||
quiz_delete_all_overrides($quiz);
|
||||
|
||||
// Look for random questions that may no longer be used when this quiz is gone.
|
||||
$sql = "SELECT q.id
|
||||
FROM {quiz_question_instances} instance
|
||||
JOIN {question} q ON q.id = instance.question
|
||||
WHERE instance.quiz = ? AND q.qtype = ?";
|
||||
$questionids = $DB->get_fieldset_sql($sql, array($quiz->id, 'random'));
|
||||
|
||||
// We need to do this before we try and delete randoms, otherwise they would still be 'in use'.
|
||||
$DB->delete_records('quiz_question_instances', array('quiz' => $quiz->id));
|
||||
|
||||
foreach ($questionids as $questionid) {
|
||||
question_delete_question($questionid);
|
||||
}
|
||||
|
||||
$DB->delete_records('quiz_feedback', array('quizid' => $quiz->id));
|
||||
|
||||
quiz_access_manager::delete_settings($quiz);
|
||||
|
||||
Reference in New Issue
Block a user