mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 08:26:37 +02:00
MDL-74427 question: Re-use get_real_question_ids_in_category()
This commit is contained in:
parent
b0e09b34ae
commit
6a19223290
2 changed files with 23 additions and 35 deletions
|
@ -475,13 +475,7 @@ class question_category_object {
|
||||||
// If the category name has changed, rename any random questions in that category.
|
// If the category name has changed, rename any random questions in that category.
|
||||||
if ($oldcat->name != $cat->name) {
|
if ($oldcat->name != $cat->name) {
|
||||||
// Get the question ids for each question category.
|
// Get the question ids for each question category.
|
||||||
$sql = "SELECT q.id
|
$questionids = $this->get_real_question_ids_in_category($cat->id);
|
||||||
FROM {question} q
|
|
||||||
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
||||||
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
||||||
WHERE qbe.questioncategoryid = ?";
|
|
||||||
|
|
||||||
$questionids = $DB->get_records_sql($sql, [$cat->id]);
|
|
||||||
|
|
||||||
foreach ($questionids as $question) {
|
foreach ($questionids as $question) {
|
||||||
$where = "qtype = 'random' AND id = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";
|
$where = "qtype = 'random' AND id = ? AND " . $DB->sql_compare_text('questiontext') . " = ?";
|
||||||
|
|
|
@ -16,6 +16,9 @@
|
||||||
|
|
||||||
namespace qbank_managecategories;
|
namespace qbank_managecategories;
|
||||||
|
|
||||||
|
use moodle_url;
|
||||||
|
use core_question\local\bank\question_edit_contexts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for helper class.
|
* Unit tests for helper class.
|
||||||
*
|
*
|
||||||
|
@ -47,6 +50,11 @@ class helper_test extends \advanced_testcase {
|
||||||
*/
|
*/
|
||||||
protected $quiz;
|
protected $quiz;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var question_category_object used in the tests.
|
||||||
|
*/
|
||||||
|
protected $qcobject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests initial setup.
|
* Tests initial setup.
|
||||||
*/
|
*/
|
||||||
|
@ -60,6 +68,12 @@ class helper_test extends \advanced_testcase {
|
||||||
$this->quiz = $datagenerator->create_module('quiz', ['course' => $this->course->id]);
|
$this->quiz = $datagenerator->create_module('quiz', ['course' => $this->course->id]);
|
||||||
$this->qgenerator = $datagenerator->get_plugin_generator('core_question');
|
$this->qgenerator = $datagenerator->get_plugin_generator('core_question');
|
||||||
$this->context = \context_module::instance($this->quiz->cmid);
|
$this->context = \context_module::instance($this->quiz->cmid);
|
||||||
|
|
||||||
|
$contexts = new question_edit_contexts($this->context);
|
||||||
|
$this->qcobject = new question_category_object(null,
|
||||||
|
new moodle_url('/question/bank/managecategories/category.php', ['courseid' => SITEID]),
|
||||||
|
$contexts->having_one_edit_tab_cap('categories'), 0, null, 0,
|
||||||
|
$contexts->having_cap('moodle/question:add'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -98,45 +112,25 @@ class helper_test extends \advanced_testcase {
|
||||||
$q1b = $this->qgenerator->create_question('random', null, ['category' => $qcat1->id]); // Will not be used.
|
$q1b = $this->qgenerator->create_question('random', null, ['category' => $qcat1->id]); // Will not be used.
|
||||||
$q2c = $this->qgenerator->create_question('random', null, ['category' => $qcat2->id]); // Will not be used.
|
$q2c = $this->qgenerator->create_question('random', null, ['category' => $qcat2->id]); // Will not be used.
|
||||||
|
|
||||||
$sql = "SELECT count(q.id)
|
$this->assertEquals(2, count($this->qcobject->get_real_question_ids_in_category($qcat1->id)));
|
||||||
FROM {question} q
|
$this->assertEquals(3, count($this->qcobject->get_real_question_ids_in_category($qcat2->id)));
|
||||||
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
||||||
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
||||||
WHERE qbe.questioncategoryid = ?";
|
|
||||||
$this->assertEquals(2, $DB->count_records_sql($sql, [$qcat1->id]));
|
|
||||||
$this->assertEquals(3, $DB->count_records_sql($sql, [$qcat2->id]));
|
|
||||||
|
|
||||||
// Non-existing category, nothing will happen.
|
// Non-existing category, nothing will happen.
|
||||||
helper::question_remove_stale_questions_from_category(0);
|
helper::question_remove_stale_questions_from_category(0);
|
||||||
$sql = "SELECT count(q.id)
|
$this->assertEquals(2, count($this->qcobject->get_real_question_ids_in_category($qcat1->id)));
|
||||||
FROM {question} q
|
$this->assertEquals(3, count($this->qcobject->get_real_question_ids_in_category($qcat2->id)));
|
||||||
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
||||||
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
||||||
WHERE qbe.questioncategoryid = ?";
|
|
||||||
$this->assertEquals(2, $DB->count_records_sql($sql, [$qcat1->id]));
|
|
||||||
$this->assertEquals(3, $DB->count_records_sql($sql, [$qcat2->id]));
|
|
||||||
|
|
||||||
// First category, should be empty afterwards.
|
// First category, should be empty afterwards.
|
||||||
helper::question_remove_stale_questions_from_category($qcat1->id);
|
helper::question_remove_stale_questions_from_category($qcat1->id);
|
||||||
$sql = "SELECT count(q.id)
|
$this->assertEquals(0, count($this->qcobject->get_real_question_ids_in_category($qcat1->id)));
|
||||||
FROM {question} q
|
$this->assertEquals(3, count($this->qcobject->get_real_question_ids_in_category($qcat2->id)));
|
||||||
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
||||||
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
||||||
WHERE qbe.questioncategoryid = ?";
|
|
||||||
$this->assertEquals(0, $DB->count_records_sql($sql, [$qcat1->id]));
|
|
||||||
$this->assertEquals(3, $DB->count_records_sql($sql, [$qcat2->id]));
|
|
||||||
$this->assertFalse($DB->record_exists('question', ['id' => $q1a->id]));
|
$this->assertFalse($DB->record_exists('question', ['id' => $q1a->id]));
|
||||||
$this->assertFalse($DB->record_exists('question', ['id' => $q1b->id]));
|
$this->assertFalse($DB->record_exists('question', ['id' => $q1b->id]));
|
||||||
|
|
||||||
// Second category, used questions should be left untouched.
|
// Second category, used questions should be left untouched.
|
||||||
helper::question_remove_stale_questions_from_category($qcat2->id);
|
helper::question_remove_stale_questions_from_category($qcat2->id);
|
||||||
$sql = "SELECT count(q.id)
|
$this->assertEquals(0, count($this->qcobject->get_real_question_ids_in_category($qcat1->id)));
|
||||||
FROM {question} q
|
$this->assertEquals(1, count($this->qcobject->get_real_question_ids_in_category($qcat2->id)));
|
||||||
JOIN {question_versions} qv ON qv.questionid = q.id
|
|
||||||
JOIN {question_bank_entries} qbe ON qbe.id = qv.questionbankentryid
|
|
||||||
WHERE qbe.questioncategoryid = ?";
|
|
||||||
$this->assertEquals(0, $DB->count_records_sql($sql, [$qcat1->id]));
|
|
||||||
$this->assertEquals(1, $DB->count_records_sql($sql, [$qcat2->id]));
|
|
||||||
$this->assertFalse($DB->record_exists('question', ['id' => $q2a->id]));
|
$this->assertFalse($DB->record_exists('question', ['id' => $q2a->id]));
|
||||||
$this->assertTrue($DB->record_exists('question', ['id' => $q2b->id]));
|
$this->assertTrue($DB->record_exists('question', ['id' => $q2b->id]));
|
||||||
$this->assertFalse($DB->record_exists('question', ['id' => $q2c->id]));
|
$this->assertFalse($DB->record_exists('question', ['id' => $q2c->id]));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue