MDL-56998 blog: Remove associated blog posts when module is deleted

This commit is contained in:
Mihail Geshoski
2018-09-03 08:40:02 +08:00
parent 0416ad83f6
commit 69e15470dc
5 changed files with 108 additions and 1 deletions
+19
View File
@@ -135,6 +135,25 @@ function blog_remove_associations_for_course($courseid) {
$DB->delete_records('blog_association', array('contextid' => $context->id));
}
/**
* Remove module associated blogs and blog tag instances.
*
* @param int $modcontextid Module context ID.
*/
function blog_remove_associations_for_module($modcontextid) {
global $DB;
if (!empty($assocblogids = $DB->get_fieldset_select('blog_association', 'blogid',
'contextid = :contextid', ['contextid' => $modcontextid]))) {
list($sql, $params) = $DB->get_in_or_equal($assocblogids, SQL_PARAMS_NAMED);
$DB->delete_records_select('tag_instance', "itemid $sql", $params);
$DB->delete_records_select('post', "id $sql AND module = :module",
array_merge($params, ['module' => 'blog']));
$DB->delete_records('blog_association', ['contextid' => $modcontextid]);
}
}
/**
* Given a record in the {blog_external} table, checks the blog's URL
* for new entries not yet copied into Moodle.
+3
View File
@@ -1218,6 +1218,9 @@ function course_delete_module($cmid, $async = false) {
}
}
// Delete associated blogs and blog tag instances.
blog_remove_associations_for_module($modcontext->id);
// Delete completion and availability data; it is better to do this even if the
// features are not turned on, in case they were turned on previously (these will be
// very quick on an empty table).
+58
View File
@@ -303,6 +303,50 @@ class core_course_courselib_testcase extends advanced_testcase {
return $moduleinfo;
}
/**
* Create module associated blog and tags.
*
* @param object $course Course.
* @param object $modulecontext The context of the module.
*/
private function create_module_asscociated_blog($course, $modulecontext) {
global $DB, $CFG;
// Create default group.
$group = new stdClass();
$group->courseid = $course->id;
$group->name = 'Group';
$group->id = $DB->insert_record('groups', $group);
// Create default user.
$user = $this->getDataGenerator()->create_user(array(
'username' => 'testuser',
'firstname' => 'Firsname',
'lastname' => 'Lastname'
));
// Create default post.
$post = new stdClass();
$post->userid = $user->id;
$post->groupid = $group->id;
$post->content = 'test post content text';
$post->module = 'blog';
$post->id = $DB->insert_record('post', $post);
// Create default tag.
$tag = $this->getDataGenerator()->create_tag(array('userid' => $user->id,
'rawname' => 'Testtagname', 'isstandard' => 1));
// Apply the tag to the blog.
$DB->insert_record('tag_instance', array('tagid' => $tag->id, 'itemtype' => 'user',
'component' => 'core', 'itemid' => $post->id, 'ordering' => 0));
require_once($CFG->dirroot . '/blog/locallib.php');
$blog = new blog_entry($post->id);
$blog->add_association($modulecontext->id);
return $blog;
}
/**
* Test create_module() for multiple modules defined in the $modules array (first declaration of the function).
*/
@@ -1521,6 +1565,8 @@ class core_course_courselib_testcase extends advanced_testcase {
// Get the module context.
$modcontext = context_module::instance($module->cmid);
$assocblog = $this->create_module_asscociated_blog($course, $modcontext);
// Verify context exists.
$this->assertInstanceOf('context_module', $modcontext);
@@ -1565,6 +1611,18 @@ class core_course_courselib_testcase extends advanced_testcase {
$cmcount = $DB->count_records('course_modules', array('id' => $module->cmid));
$this->assertEmpty($cmcount);
// Verify the blog_association record has been deleted.
$this->assertCount(0, $DB->get_records('blog_association',
array('contextid' => $modcontext->id)));
// Verify the blog post record has been deleted.
$this->assertCount(0, $DB->get_records('post',
array('id' => $assocblog->id)));
// Verify the tag instance record has been deleted.
$this->assertCount(0, $DB->get_records('tag_instance',
array('itemid' => $assocblog->id)));
// Test clean up of module specific messes.
switch ($type) {
case 'assign':
+27
View File
@@ -2871,5 +2871,32 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2017111303.08);
}
if ($oldversion < 2017111304.10) {
// Remove module associated blog posts for non-existent (deleted) modules.
$sql = "SELECT ba.contextid as modcontextid
FROM {blog_association} ba
JOIN {post} p
ON p.id = ba.blogid
LEFT JOIN {context} c
ON c.id = ba.contextid
WHERE p.module = :module
AND c.contextlevel IS NULL
GROUP BY ba.contextid";
if ($deletedmodules = $DB->get_records_sql($sql, array('module' => 'blog'))) {
foreach ($deletedmodules as $module) {
$assocblogids = $DB->get_fieldset_select('blog_association', 'blogid',
'contextid = :contextid', ['contextid' => $module->modcontextid]);
list($sql, $params) = $DB->get_in_or_equal($assocblogids, SQL_PARAMS_NAMED);
$DB->delete_records_select('tag_instance', "itemid $sql", $params);
$DB->delete_records_select('post', "id $sql AND module = :module",
array_merge($params, ['module' => 'blog']));
$DB->delete_records('blog_association', ['contextid' => $module->modcontextid]);
}
}
// Main savepoint reached.
upgrade_main_savepoint(true, 2017111304.10);
}
return true;
}
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2017111304.09; // 20171113 = branching date YYYYMMDD - do not modify!
$version = 2017111304.10; // 20171113 = branching date YYYYMMDD - do not modify!
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.