/**
  * Test the post_created event for a single discussion forum.
  */
 public function test_post_created_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_created::create($params);
     // Trigger and capturing 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_created', $event);
     $this->assertEquals($context, $event->get_context());
     $expected = array($course->id, 'hsuforum', 'add post', "view.php?f={$forum->id}#p{$post->id}", $forum->id, $forum->cmid);
     $this->assertEventLegacyLogData($expected, $event);
     $url = new \moodle_url('/mod/hsuforum/view.php', array('f' => $forum->id));
     $url->set_anchor('p' . $event->objectid);
     $this->assertEquals($url, $event->get_url());
     $this->assertEventContextNotUsed($event);
     $this->assertNotEmpty($event->get_name());
 }
Esempio n. 2
0
             $timemessage = 4;
         } else {
             $message .= '<p>' . get_string("postaddedsuccess", "hsuforum") . '</p>';
             $message .= '<p>' . get_string("postaddedtimeleft", "hsuforum", format_time($CFG->maxeditingtime)) . '</p>';
         }
         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={$discussion->id}";
         }
         $post = $DB->get_record('hsuforum_posts', array('id' => $fromform->id), '*', MUST_EXIST);
         $params = array('context' => $modcontext, 'objectid' => $fromform->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type));
         $event = \mod_hsuforum\event\post_created::create($params);
         $event->add_record_snapshot('hsuforum_posts', $post);
         $event->add_record_snapshot('hsuforum_discussions', $discussion);
         $event->trigger();
         // Update completion state
         $completion = new completion_info($course);
         if ($completion->is_enabled($cm) && ($forum->completionreplies || $forum->completionposts)) {
             $completion->update_state($cm, COMPLETION_COMPLETE);
         }
         redirect(hsuforum_go_back_to("{$discussionurl}#p{$fromform->id}"), $message . $subscribemessage, $timemessage);
     } else {
         print_error("couldnotadd", "hsuforum", $errordestination);
     }
     exit;
 } else {
     // Adding a new discussion.
 /**
  * Update completion info and trigger event
  *
  * @param object $course
  * @param \context_module $context
  * @param object $cm
  * @param object $forum
  * @param object $discussion
  * @param object $post
  */
 public function trigger_post_created($course, \context_module $context, $cm, $forum, $discussion, $post)
 {
     global $CFG;
     require_once $CFG->libdir . '/completionlib.php';
     // Update completion state
     $completion = new \completion_info($course);
     if ($completion->is_enabled($cm) && ($forum->completionreplies || $forum->completionposts)) {
         $completion->update_state($cm, COMPLETION_COMPLETE);
     }
     $params = array('context' => $context, 'objectid' => $post->id, 'other' => array('discussionid' => $discussion->id, 'forumid' => $forum->id, 'forumtype' => $forum->type));
     $event = post_created::create($params);
     $event->add_record_snapshot('hsuforum_posts', $post);
     $event->add_record_snapshot('hsuforum_discussions', $discussion);
     $event->trigger();
 }