/** * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * * @global object * @param int $id forum instance id * @return bool success */ function forum_delete_instance($id) { global $DB; if (!$forum = $DB->get_record('forum', array('id'=>$id))) { return false; } if (!$cm = get_coursemodule_from_instance('forum', $forum->id)) { return false; } if (!$course = $DB->get_record('course', array('id'=>$cm->course))) { return false; } $context = context_module::instance($cm->id); // now get rid of all files $fs = get_file_storage(); $fs->delete_area_files($context->id); $result = true; if ($discussions = $DB->get_records('forum_discussions', array('forum'=>$forum->id))) { foreach ($discussions as $discussion) { if (!forum_delete_discussion($discussion, true, $course, $cm, $forum)) { $result = false; } } } if (!$DB->delete_records('forum_subscriptions', array('forum'=>$forum->id))) { $result = false; } forum_tp_delete_read_records(-1, -1, -1, $forum->id); if (!$DB->delete_records('forum', array('id'=>$forum->id))) { $result = false; } forum_grade_item_delete($forum); return $result; }
$timepassed = time() - $post->created; if ($timepassed > $CFG->maxeditingtime && !has_capability('mod/forum:deleteanypost', $modcontext)) { print_error("cannotdeletepost", "forum", forum_go_back_to("discuss.php?d={$post->discussion}")); } if ($post->totalscore) { notice(get_string('couldnotdeleteratings', 'rating'), forum_go_back_to("discuss.php?d={$post->discussion}")); } else { if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) { print_error("couldnotdeletereplies", "forum", forum_go_back_to("discuss.php?d={$post->discussion}")); } else { if (!$post->parent) { // post is a discussion topic as well, so delete discussion if ($forum->type == 'single') { notice("Sorry, but you are not allowed to delete that discussion!", forum_go_back_to("discuss.php?d={$post->discussion}")); } forum_delete_discussion($discussion, false, $course, $cm, $forum); add_to_log($discussion->course, "forum", "delete discussion", "view.php?id={$cm->id}", "{$forum->id}", $cm->id); redirect("view.php?f={$discussion->forum}"); } else { if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum)) { if ($forum->type == 'single') { // Single discussion forums are an exception. We show // the forum itself since it only has one discussion // thread. $discussionurl = "view.php?f={$forum->id}"; } else { $discussionurl = "discuss.php?d={$post->discussion}"; } add_to_log($discussion->course, "forum", "delete post", $discussionurl, "{$post->id}", $cm->id); redirect(forum_go_back_to($discussionurl)); } else {
/** * Given an ID of an instance of this module, * this function will permanently delete the instance * and any data that depends on it. * @param int forum instance id * @return bool success */ function forum_delete_instance($id) { if (!($forum = get_record('forum', 'id', $id))) { return false; } $result = true; if ($discussions = get_records('forum_discussions', 'forum', $forum->id)) { foreach ($discussions as $discussion) { if (!forum_delete_discussion($discussion, true)) { $result = false; } } } if (!delete_records('forum_subscriptions', 'forum', $forum->id)) { $result = false; } forum_tp_delete_read_records(-1, -1, -1, $forum->id); if (!delete_records('forum', 'id', $forum->id)) { $result = false; } forum_grade_item_delete($forum); return $result; }
} $replycount = forum_count_replies($post); if (!empty($confirm) && confirm_sesskey()) { // User has confirmed the delete if ($post->totalscore) { notice(get_string("couldnotdeleteratings", "forum"), forum_go_back_to("discuss.php?d={$post->discussion}")); } else { if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) { error(get_string("couldnotdeletereplies", "forum"), forum_go_back_to("discuss.php?d={$post->discussion}")); } else { if (!$post->parent) { // post is a discussion topic as well, so delete discussion if ($forum->type == 'single') { notice("Sorry, but you are not allowed to delete that discussion!", forum_go_back_to("discuss.php?d={$post->discussion}")); } forum_delete_discussion($discussion); add_to_log($discussion->course, "forum", "delete discussion", "view.php?id={$cm->id}", "{$forum->id}", $cm->id); redirect("view.php?f={$discussion->forum}"); } else { if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext))) { if ($forum->type == 'single') { // Single discussion forums are an exception. We show // the forum itself since it only has one discussion // thread. $discussionurl = "view.php?f={$forum->id}"; } else { $discussionurl = "discuss.php?d={$post->discussion}"; } add_to_log($discussion->course, "forum", "delete post", $discussionurl, "{$post->id}", $cm->id); redirect(forum_go_back_to($discussionurl)); } else {
/** * Test post_deleted event. */ public function test_post_deleted() { global $DB; // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id)); $user = $this->getDataGenerator()->create_user(); $cm = get_coursemodule_from_instance('forum', $forum->id, $forum->course); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record); // When creating a discussion we also create a post, so get the post. $discussionpost = $DB->get_records('forum_posts'); // Will only be one here. $discussionpost = reset($discussionpost); // Add a few posts. $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; $posts = array(); $posts[$discussionpost->id] = $discussionpost; for ($i = 0; $i < 3; $i++) { $post = $this->getDataGenerator()->get_plugin_generator('mod_forum')->create_post($record); $posts[$post->id] = $post; } // Delete the last post and capture the event. $lastpost = end($posts); $sink = $this->redirectEvents(); forum_delete_post($lastpost, true, $course, $cm, $forum); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Check that the events contain the expected values. $this->assertInstanceOf('\\mod_forum\\event\\post_deleted', $event); $this->assertEquals(context_module::instance($forum->cmid), $event->get_context()); $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $lastpost->id, $forum->cmid); $this->assertEventLegacyLogData($expected, $event); $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); // Delete the whole discussion and capture the events. $sink = $this->redirectEvents(); forum_delete_discussion($discussion, true, $course, $cm, $forum); $events = $sink->get_events(); // We will have 3 events. One for the discussion (creating a discussion creates a post), and two for the posts. $this->assertCount(3, $events); // Loop through the events and check they are valid. foreach ($events as $event) { $post = $posts[$event->objectid]; // Check that the event contains the expected values. $this->assertInstanceOf('\\mod_forum\\event\\post_deleted', $event); $this->assertEquals(context_module::instance($forum->cmid), $event->get_context()); $expected = array($course->id, 'forum', 'delete post', "discuss.php?d={$discussion->id}", $post->id, $forum->cmid); $this->assertEventLegacyLogData($expected, $event); $url = new \moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); } }