);
}
+ /**
+ * Returns description of method parameters
+ *
+ * @return external_function_parameters
+ * @since Moodle 3.1
+ */
+ public static function can_add_discussion_parameters() {
+ return new external_function_parameters(
+ array(
+ 'forumid' => new external_value(PARAM_INT, 'Forum instance ID'),
+ 'groupid' => new external_value(PARAM_INT, 'The group to check, default to active group.
+ Use -1 to check if the user can post in all the groups.', VALUE_DEFAULT, null)
+ )
+ );
+ }
+
+ /**
+ * Check if the current user can add discussions in the given forum (and optionally for the given group).
+ *
+ * @param int $forumid the forum instance id
+ * @param int $groupid the group to check, default to active group. Use -1 to check if the user can post in all the groups.
+ * @return array of warnings and the status (true if the user can add discussions)
+ * @since Moodle 3.1
+ * @throws moodle_exception
+ */
+ public static function can_add_discussion($forumid, $groupid = null) {
+ global $DB, $CFG;
+ require_once($CFG->dirroot . "/mod/forum/lib.php");
+
+ $params = self::validate_parameters(self::can_add_discussion_parameters(),
+ array(
+ 'forumid' => $forumid,
+ 'groupid' => $groupid,
+ ));
+ $warnings = array();
+
+ // Request and permission validation.
+ $forum = $DB->get_record('forum', array('id' => $params['forumid']), '*', MUST_EXIST);
+ list($course, $cm) = get_course_and_cm_from_instance($forum, 'forum');
+
+ $context = context_module::instance($cm->id);
+ self::validate_context($context);
+
+ $status = forum_user_can_post_discussion($forum, $params['groupid'], -1, $cm, $context);
+
+ $result = array();
+ $result['status'] = $status;
+ $result['warnings'] = $warnings;
+ return $result;
+ }
+
+ /**
+ * Returns description of method result value
+ *
+ * @return external_description
+ * @since Moodle 3.1
+ */
+ public static function can_add_discussion_returns() {
+ return new external_single_structure(
+ array(
+ 'status' => new external_value(PARAM_BOOL, 'True if the user can add discussions, false otherwise.'),
+ 'warnings' => new external_warnings()
+ )
+ );
+ }
+
}
}
+ /*
+ * Test can_add_discussion. A basic test since all the API functions are already covered by unit tests.
+ */
+ public function test_can_add_discussion() {
+
+ $this->resetAfterTest(true);
+
+ // Create courses to add the modules.
+ $course = self::getDataGenerator()->create_course();
+
+ $user = self::getDataGenerator()->create_user();
+
+ // First forum with tracking off.
+ $record = new stdClass();
+ $record->course = $course->id;
+ $record->type = 'news';
+ $forum = self::getDataGenerator()->create_module('forum', $record);
+
+ // User with no permissions to add in a news forum.
+ self::setUser($user);
+ $this->getDataGenerator()->enrol_user($user->id, $course->id);
+
+ $result = mod_forum_external::can_add_discussion($forum->id);
+ $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
+ $this->assertFalse($result['status']);
+
+ self::setAdminUser();
+ $result = mod_forum_external::can_add_discussion($forum->id);
+ $result = external_api::clean_returnvalue(mod_forum_external::can_add_discussion_returns(), $result);
+ $this->assertTrue($result['status']);
+
+ }
+
}
self::assertCount(0, $discussions);
}
+
+ /**
+ * Test forum_user_can_post_discussion
+ */
+ public function test_forum_user_can_post_discussion() {
+ global $CFG, $DB;
+
+ $this->resetAfterTest(true);
+
+ // Create course to add the module.
+ $course = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
+ $user = self::getDataGenerator()->create_user();
+ $this->getDataGenerator()->enrol_user($user->id, $course->id);
+
+ // Forum forcing separate gropus.
+ $record = new stdClass();
+ $record->course = $course->id;
+ $forum = self::getDataGenerator()->create_module('forum', $record, array('groupmode' => SEPARATEGROUPS));
+ $cm = get_coursemodule_from_instance('forum', $forum->id);
+ $context = context_module::instance($cm->id);
+
+ self::setUser($user);
+
+ // The user is not enroled in any group, try to post in a forum with separate groups.
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertFalse($can);
+
+ // Create a group.
+ $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
+
+ // Try to post in a group the user is not enrolled.
+ $can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
+ $this->assertFalse($can);
+
+ // Add the user to a group.
+ groups_add_member($group->id, $user->id);
+
+ // Try to post in a group the user is not enrolled.
+ $can = forum_user_can_post_discussion($forum, $group->id + 1, -1, $cm, $context);
+ $this->assertFalse($can);
+
+ // Now try to post in the user group. (null means it will guess the group).
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertTrue($can);
+
+ $can = forum_user_can_post_discussion($forum, $group->id, -1, $cm, $context);
+ $this->assertTrue($can);
+
+ // Test all groups.
+ $can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
+ $this->assertFalse($can);
+
+ $this->setAdminUser();
+ $can = forum_user_can_post_discussion($forum, -1, -1, $cm, $context);
+ $this->assertTrue($can);
+
+ // Change forum type.
+ $forum->type = 'news';
+ $DB->update_record('forum', $forum);
+
+ // Admin can post news.
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertTrue($can);
+
+ // Normal users don't.
+ self::setUser($user);
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertFalse($can);
+
+ // Change forum type.
+ $forum->type = 'eachuser';
+ $DB->update_record('forum', $forum);
+
+ // I didn't post yet, so I should be able to post.
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertTrue($can);
+
+ // Post now.
+ $record = new stdClass();
+ $record->course = $course->id;
+ $record->userid = $user->id;
+ $record->forum = $forum->id;
+ $discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
+
+ // I already posted, I shouldn't be able to post.
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertFalse($can);
+
+ // Last check with no groups, normal forum and course.
+ $course->groupmode = NOGROUPS;
+ $course->groupmodeforce = 0;
+ $DB->update_record('course', $course);
+
+ $forum->type = 'general';
+ $forum->groupmode = NOGROUPS;
+ $DB->update_record('forum', $forum);
+
+ $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context);
+ $this->assertTrue($can);
+ }
}