redirect("view.php?f={$discussion->twf}"); } else { if (twf_delete_post($post, has_capability('mod/twf:deleteanypost', $modcontext), $course, $cm, $twf)) { if ($twf->type == 'single') { // Single discussion twfs are an exception. We show // the twf itself since it only has one discussion // thread. $discussionurl = "view.php?f={$twf->id}"; } else { $discussionurl = "discuss.php?d={$post->discussion}"; } $params = array('context' => $modcontext, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'twfid' => $twf->id, 'twftype' => $twf->type)); if ($post->userid !== $USER->id) { $params['relateduserid'] = $post->userid; } $event = \mod_twf\event\post_deleted::create($params); $event->add_record_snapshot('twf_posts', $post); $event->add_record_snapshot('twf_discussions', $discussion); $event->trigger(); redirect(twf_go_back_to($discussionurl)); } else { print_error('errorwhiledelete', 'twf'); } } } } } else { // User just asked to delete something twf_set_return(); $PAGE->navbar->add(get_string('delete', 'twf')); $PAGE->set_title($course->shortname);
/** * Test post_deleted event for a single discussion twf. */ public function test_post_deleted_single() { // Setup test data. $course = $this->getDataGenerator()->create_course(); $twf = $this->getDataGenerator()->create_module('twf', array('course' => $course->id, 'type' => 'single')); $user = $this->getDataGenerator()->create_user(); // Add a discussion. $record = array(); $record['course'] = $course->id; $record['twf'] = $twf->id; $record['userid'] = $user->id; $discussion = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_discussion($record); // Add a post. $record = array(); $record['discussion'] = $discussion->id; $record['userid'] = $user->id; $post = $this->getDataGenerator()->get_plugin_generator('mod_twf')->create_post($record); $context = context_module::instance($twf->cmid); $params = array('context' => $context, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'twfid' => $twf->id, 'twftype' => $twf->type)); $event = \mod_twf\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_twf\\event\\post_deleted', $event); $this->assertEquals($context, $event->get_context()); $expected = array($course->id, 'twf', 'delete post', "view.php?f={$twf->id}", $post->id, $twf->cmid); $this->assertEventLegacyLogData($expected, $event); $url = new \moodle_url('/mod/twf/view.php', array('f' => $twf->id)); $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); $this->assertNotEmpty($event->get_name()); }