Commit 9bfb76c4 authored by David Monllaó's avatar David Monllaó
Browse files

Merge branch 'MDL-52942-master' of git://github.com/andrewnicols/moodle

parents 468ef667 c0e3c8ae
......@@ -4813,22 +4813,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 = ?";
$params = [$forumid, $userid];
if ($groupid) {
$sql .= " AND d.groupid = ?";
$params[] = $groupid;
}
return $DB->record_exists_sql($sql, array($forumid, $userid));
return $DB->record_exists_sql($sql, $params);
}
/**
......@@ -4944,7 +4950,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;
}
}
......
......@@ -2196,6 +2196,7 @@ class mod_forum_lib_testcase extends advanced_testcase {
$record->course = $course->id;
$record->userid = $user->id;
$record->forum = $forum->id;
$record->groupid = $group->id;
$discussion = self::getDataGenerator()->get_plugin_generator('mod_forum')->create_discussion($record);
// I already posted, I shouldn't be able to post.
......@@ -2215,6 +2216,125 @@ class mod_forum_lib_testcase extends advanced_testcase {
$this->assertTrue($can);
}
/**
* 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.
*/
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment