/** * Verify and delete the post. The post can be a discussion post. * * @param object $course * @param object $cm * @param object $forum * @param context_module $modcontext * @param object $discussion * @param object $post * @return string The URL to redirect to */ function hsuforum_verify_and_delete_post($course, $cm, $forum, $modcontext, $discussion, $post) { global $CFG, $USER; // Check user capability to delete post. $timepassed = time() - $post->created; if ($timepassed > $CFG->maxeditingtime && !has_capability('mod/hsuforum:deleteanypost', $modcontext)) { print_error("cannotdeletepost", "hsuforum", hsuforum_go_back_to("discuss.php?d={$post->discussion}")); } if ($post->totalscore) { print_error('couldnotdeleteratings', 'rating', hsuforum_go_back_to("discuss.php?d={$post->discussion}")); } if (hsuforum_count_replies($post) && !has_capability('mod/hsuforum:deleteanypost', $modcontext)) { print_error("couldnotdeletereplies", "hsuforum", hsuforum_go_back_to("discuss.php?d={$post->discussion}")); } if (!$post->parent) { // post is a discussion topic as well, so delete discussion if ($forum->type == 'single') { print_error('cannnotdeletesinglediscussion', 'hsuforum', hsuforum_go_back_to("discuss.php?d={$post->discussion}")); } hsuforum_delete_discussion($discussion, false, $course, $cm, $forum); $params = array('objectid' => $discussion->id, 'context' => $modcontext, 'other' => array('forumid' => $forum->id)); $event = \mod_hsuforum\event\discussion_deleted::create($params); $event->add_record_snapshot('hsuforum_discussions', $discussion); $event->trigger(); return $CFG->wwwroot . "/mod/hsuforum/view.php?id={$cm->id}"; } if (!hsuforum_delete_post($post, has_capability('mod/hsuforum:deleteanypost', $modcontext), $course, $cm, $forum)) { print_error('errorwhiledelete', 'hsuforum'); } 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}"; } $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)); if ($post->userid !== $USER->id) { $params['relateduserid'] = $post->userid; } $event = \mod_hsuforum\event\post_deleted::create($params); $event->add_record_snapshot('hsuforum_posts', $post); $event->add_record_snapshot('hsuforum_discussions', $discussion); $event->trigger(); return hsuforum_go_back_to($discussionurl); }
/** * Test post_deleted event for a single discussion forum. */ public function test_post_deleted_single() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $forum = $this->getDataGenerator()->create_module('hsuforum', array('course' => $course->id, 'type' => 'single')); $user = $this->getDataGenerator()->create_user(); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['forum'] = $forum->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_hsuforum')->create_discussion($record); // Add a post. $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_hsuforum')->create_post($record); $context = context_module::instance($forum->cmid); $params = array('context' => $context, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type)); $event = \mod_hsuforum\event\post_deleted::create($params); // Trigger and capture the event. $sink = $this->redirectEvents(); $event->trigger(); $events = $sink->get_events(); $this->assertCount(1, $events); $event = reset($events); // Checking that the event contains the expected values. $this->assertInstanceOf('\\mod_hsuforum\\event\\post_deleted', $event); $this->assertEquals($context, $event->get_context()); $expected = array($course->id, 'hsuforum', 'delete post', "view.php?f={$forum->id}", $post->id, $forum->cmid); $this->assertEventLegacyLogData($expected, $event); $url = new \moodle_url('/mod/hsuforum/view.php', array('f' => $forum->id)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); }