MDL-52942 forum: Allow posting to multiple groups in eachuser forums
This commit is contained in:
+15
-9
@@ -4907,22 +4907,28 @@ function forum_get_subscribe_link($forum, $context, $messages = array(), $cantac
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if user created new discussion already
|
||||
* Returns true if user created new discussion already.
|
||||
*
|
||||
* @global object
|
||||
* @global object
|
||||
* @param int $forumid
|
||||
* @param int $userid
|
||||
* @param int $forumid The forum to check for postings
|
||||
* @param int $userid The user to check for postings
|
||||
* @param int $groupid The group to restrict the check to
|
||||
* @return bool
|
||||
*/
|
||||
function forum_user_has_posted_discussion($forumid, $userid) {
|
||||
function forum_user_has_posted_discussion($forumid, $userid, $groupid = null) {
|
||||
global $CFG, $DB;
|
||||
|
||||
$sql = "SELECT 'x'
|
||||
FROM {forum_discussions} d, {forum_posts} p
|
||||
WHERE d.forum = ? AND p.discussion = d.id AND p.parent = 0 and p.userid = ?";
|
||||
WHERE d.forum = ? AND p.discussion = d.id AND p.parent = 0 AND p.userid = ?";
|
||||
|
||||
return $DB->record_exists_sql($sql, array($forumid, $userid));
|
||||
$params = [$forumid, $userid];
|
||||
|
||||
if ($groupid) {
|
||||
$sql .= " AND d.groupid = ?";
|
||||
$params[] = $groupid;
|
||||
}
|
||||
|
||||
return $DB->record_exists_sql($sql, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5038,7 +5044,7 @@ function forum_user_can_post_discussion($forum, $currentgroup=null, $unused=-1,
|
||||
}
|
||||
|
||||
if ($forum->type == 'eachuser') {
|
||||
if (forum_user_has_posted_discussion($forum->id, $USER->id)) {
|
||||
if (forum_user_has_posted_discussion($forum->id, $USER->id, $currentgroup)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1501,6 +1501,125 @@ class mod_forum_lib_testcase extends advanced_testcase {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test forum_user_has_posted_discussion with no groups.
|
||||
*/
|
||||
public function test_forum_user_has_posted_discussion_no_groups() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$author = self::getDataGenerator()->create_user();
|
||||
$other = self::getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($author->id, $course->id);
|
||||
$forum = self::getDataGenerator()->create_module('forum', (object) ['course' => $course->id ]);
|
||||
|
||||
self::setUser($author);
|
||||
|
||||
// Neither user has posted.
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $author->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $other->id));
|
||||
|
||||
// Post in the forum.
|
||||
$record = new stdClass();
|
||||
$record->course = $course->id;
|
||||
$record->userid = $author->id;
|
||||
$record->forum = $forum->id;
|
||||
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// The author has now posted, but the other user has not.
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum->id, $author->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $other->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test forum_user_has_posted_discussion with multiple forums
|
||||
*/
|
||||
public function test_forum_user_has_posted_discussion_multiple_forums() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$author = self::getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($author->id, $course->id);
|
||||
$forum1 = self::getDataGenerator()->create_module('forum', (object) ['course' => $course->id ]);
|
||||
$forum2 = self::getDataGenerator()->create_module('forum', (object) ['course' => $course->id ]);
|
||||
|
||||
self::setUser($author);
|
||||
|
||||
// No post in either forum.
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum1->id, $author->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum2->id, $author->id));
|
||||
|
||||
// Post in the forum.
|
||||
$record = new stdClass();
|
||||
$record->course = $course->id;
|
||||
$record->userid = $author->id;
|
||||
$record->forum = $forum1->id;
|
||||
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// The author has now posted in forum1, but not forum2.
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum1->id, $author->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum2->id, $author->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test forum_user_has_posted_discussion with multiple groups.
|
||||
*/
|
||||
public function test_forum_user_has_posted_discussion_multiple_groups() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$course = self::getDataGenerator()->create_course();
|
||||
$author = self::getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($author->id, $course->id);
|
||||
|
||||
$group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
||||
$group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
|
||||
groups_add_member($group1->id, $author->id);
|
||||
groups_add_member($group2->id, $author->id);
|
||||
|
||||
$forum = self::getDataGenerator()->create_module('forum', (object) ['course' => $course->id ], [
|
||||
'groupmode' => SEPARATEGROUPS,
|
||||
]);
|
||||
|
||||
self::setUser($author);
|
||||
|
||||
// The user has not posted in either group.
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $author->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $author->id, $group1->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $author->id, $group2->id));
|
||||
|
||||
// Post in one group.
|
||||
$record = new stdClass();
|
||||
$record->course = $course->id;
|
||||
$record->userid = $author->id;
|
||||
$record->forum = $forum->id;
|
||||
$record->groupid = $group1->id;
|
||||
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// The author has now posted in one group, but the other user has not.
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum->id, $author->id));
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum->id, $author->id, $group1->id));
|
||||
$this->assertFalse(forum_user_has_posted_discussion($forum->id, $author->id, $group2->id));
|
||||
|
||||
// Post in the other group.
|
||||
$record = new stdClass();
|
||||
$record->course = $course->id;
|
||||
$record->userid = $author->id;
|
||||
$record->forum = $forum->id;
|
||||
$record->groupid = $group2->id;
|
||||
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
|
||||
|
||||
// The author has now posted in one group, but the other user has not.
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum->id, $author->id));
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum->id, $author->id, $group1->id));
|
||||
$this->assertTrue(forum_user_has_posted_discussion($forum->id, $author->id, $group2->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the mod_forum_myprofile_navigation() function.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user