diff --git a/lib/questionlib.php b/lib/questionlib.php index f717ebdcb7c..e039ad9c534 100644 --- a/lib/questionlib.php +++ b/lib/questionlib.php @@ -389,9 +389,6 @@ function question_delete_question($questionid): void { // Delete questiontype-specific data. question_bank::get_qtype($question->qtype, false)->delete_question($question->id, $questiondata->contextid); - // Delete all tag instances. - core_tag_tag::remove_all_item_tags('core_question', 'question', $question->id); - // Delete the custom filed data for the question. $customfieldhandler = qbank_customfields\customfield\question_handler::create(); $customfieldhandler->delete_instance($question->id); diff --git a/question/bank/tagquestion/classes/event/question_deleted_observer.php b/question/bank/tagquestion/classes/event/question_deleted_observer.php new file mode 100644 index 00000000000..056acb40bd2 --- /dev/null +++ b/question/bank/tagquestion/classes/event/question_deleted_observer.php @@ -0,0 +1,45 @@ +. + +namespace qbank_tagquestion\event; + +use core\context; +use core\event\question_deleted; + +/** + * Event observer for question deletion + * + * @package qbank_tagquestion + * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net} + * @author Mark Johnson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class question_deleted_observer { + + /** + * Delete any tags defined for the deleted question. + * + * This uses {@see \core_tag_tag::set_item_tags} rather than {@see \core_tag_tag::remove_all_item_tags} since the latter + * will always pass the system context, not the question context that the tag was set in. + * + * @param question_deleted $event + * @return void + */ + public static function delete_question_tags(question_deleted $event): void { + $questioncontext = context::instance_by_id($event->contextid); + \core_tag_tag::set_item_tags('core_question', 'question', $event->objectid, $questioncontext, null, $event->userid); + } +} diff --git a/question/bank/tagquestion/db/events.php b/question/bank/tagquestion/db/events.php new file mode 100644 index 00000000000..911b9e9b591 --- /dev/null +++ b/question/bank/tagquestion/db/events.php @@ -0,0 +1,33 @@ +. + +/** + * Question tag events + * + * @package qbank_tagquestion + * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net} + * @author Mark Johnson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$observers = [ + [ + 'eventname' => '\core\event\question_deleted', + 'callback' => '\qbank_tagquestion\event\question_deleted_observer::delete_question_tags' + ] +]; diff --git a/question/bank/tagquestion/tests/event/question_deleted_observer_test.php b/question/bank/tagquestion/tests/event/question_deleted_observer_test.php new file mode 100644 index 00000000000..a4d13efa22b --- /dev/null +++ b/question/bank/tagquestion/tests/event/question_deleted_observer_test.php @@ -0,0 +1,50 @@ +. + +namespace qbank_tagquestion\event; + +/** + * Tests for question_deleted_observer + * + * @package qbank_tagquestion + * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net} + * @author Mark Johnson + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \qbank_tagquestion\event\question_deleted_observer + */ +class question_deleted_observer_test extends \advanced_testcase { + + /** + * Deleting a question with tags should also delete the tags. + * + * @return void + */ + public function test_delete_question_with_tags(): void { + $this->resetAfterTest(); + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); + [, , $qcat, $questions] = $questiongenerator->setup_course_and_questions(); + $questioncontext = \context::instance_by_id($qcat->contextid); + $question = reset($questions); + $tag = random_string(); + \core_tag_tag::add_item_tag('core_question', 'question', $question->id, $questioncontext, $tag); + + $this->assertCount(1, \core_tag_tag::get_item_tags('core_question', 'question', $question->id)); + + question_delete_question($question->id); + + $this->assertEmpty(\core_tag_tag::get_item_tags('core_question', 'question', $question->id)); + } +} diff --git a/question/bank/tagquestion/version.php b/question/bank/tagquestion/version.php index ff37e0241a4..eda3baf8ad2 100644 --- a/question/bank/tagquestion/version.php +++ b/question/bank/tagquestion/version.php @@ -26,6 +26,6 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qbank_tagquestion'; -$plugin->version = 2023042400; +$plugin->version = 2023042401; $plugin->requires = 2023041800; $plugin->maturity = MATURITY_STABLE;