mirror of
https://github.com/moodle/moodle.git
synced 2025-04-23 09:23:09 +02:00
Merge branch 'MDL-66220-master' of git://github.com/junpataleta/moodle
This commit is contained in:
commit
a445417935
mod/forum
@ -61,6 +61,8 @@ class capability {
|
||||
private $forumrecord;
|
||||
/** @var context $context Module context for the forum */
|
||||
private $context;
|
||||
/** @var array $canviewpostcache Cache of discussion posts that can be viewed by a user. */
|
||||
protected $canviewpostcache = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
@ -361,12 +363,23 @@ class capability {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return cached can view if possible.
|
||||
if (isset($this->canviewpostcache[$user->id][$post->get_id()])) {
|
||||
return $this->canviewpostcache[$user->id][$post->get_id()];
|
||||
}
|
||||
|
||||
// Otherwise, check if the user can see this post.
|
||||
$forum = $this->get_forum();
|
||||
$forumrecord = $this->get_forum_record();
|
||||
$discussionrecord = $this->get_discussion_record($discussion);
|
||||
$postrecord = $this->get_post_record($post);
|
||||
$coursemodule = $forum->get_course_module_record();
|
||||
return forum_user_can_see_post($forumrecord, $discussionrecord, $postrecord, $user, $coursemodule, false);
|
||||
$canviewpost = forum_user_can_see_post($forumrecord, $discussionrecord, $postrecord, $user, $coursemodule, false);
|
||||
|
||||
// Then cache the result before returning.
|
||||
$this->canviewpostcache[$user->id][$post->get_id()] = $canviewpost;
|
||||
|
||||
return $canviewpost;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -520,6 +533,9 @@ class capability {
|
||||
if ($post->is_private_reply()) {
|
||||
// It is not possible to reply to a private reply.
|
||||
return false;
|
||||
} else if (!$this->can_view_post($user, $discussion, $post)) {
|
||||
// If the user cannot view the post in the first place, the user should not be able to reply to the post.
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->can_post_in_discussion($user, $discussion);
|
||||
|
@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die();
|
||||
global $CFG;
|
||||
require_once(__DIR__ . '/generator_trait.php');
|
||||
|
||||
use mod_forum\local\entities\forum;
|
||||
use mod_forum\local\managers\capability as capability_manager;
|
||||
|
||||
/**
|
||||
@ -122,7 +123,7 @@ class mod_forum_managers_capability_testcase extends advanced_testcase {
|
||||
* Helper function to create a forum entity.
|
||||
*
|
||||
* @param array $forumproperties List of properties to override the prebuilt forum
|
||||
* @return forum_entity
|
||||
* @return forum
|
||||
*/
|
||||
private function create_forum(array $forumproperties = []) {
|
||||
$forumrecord = (object) array_merge((array) $this->forumrecord, $forumproperties);
|
||||
@ -941,6 +942,52 @@ class mod_forum_managers_capability_testcase extends advanced_testcase {
|
||||
$this->assertFalse($capabilitymanager->can_reply_to_post($user, $discussion, $post));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for \mod_forum\local\managers\capability::can_reply_to_post() involving Q & A forums.
|
||||
*/
|
||||
public function test_can_reply_to_post_in_qanda_forum() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest();
|
||||
|
||||
// Set max editing time to 10 seconds.
|
||||
$CFG->maxeditingtime = 10;
|
||||
|
||||
$qandaforum = $this->create_forum(['type' => 'qanda']);
|
||||
$datagenerator = $this->getDataGenerator();
|
||||
$capabilitymanager = $this->managerfactory->get_capability_manager($qandaforum);
|
||||
|
||||
// Student 1.
|
||||
$student1 = $datagenerator->create_user(['firstname' => 'S1']);
|
||||
$datagenerator->enrol_user($student1->id, $this->course->id, 'student');
|
||||
// Confirm Student 1 can reply to the question.
|
||||
$this->assertTrue($capabilitymanager->can_reply_to_post($student1, $this->discussion, $this->post));
|
||||
|
||||
// Student 2.
|
||||
$student2 = $datagenerator->create_user(['firstname' => 'S2']);
|
||||
$datagenerator->enrol_user($student2->id, $this->course->id, 'student');
|
||||
// Confirm Student 2 can reply to the question.
|
||||
$this->assertTrue($capabilitymanager->can_reply_to_post($student2, $this->discussion, $this->post));
|
||||
|
||||
// Reply to the question as student 1.
|
||||
$now = time();
|
||||
$options = ['parent' => $this->post->get_id(), 'created' => $now - 100];
|
||||
$student1post = $this->helper_post_to_discussion($this->forumrecord, $this->discussionrecord, $student1, $options);
|
||||
$student1postentity = $this->entityfactory->get_post_from_stdclass($student1post);
|
||||
|
||||
// Confirm Student 2 cannot reply student 1's answer yet.
|
||||
$this->assertFalse($capabilitymanager->can_reply_to_post($student2, $this->discussion, $student1postentity));
|
||||
|
||||
// Reply to the question as student 2.
|
||||
$this->helper_post_to_discussion($this->forumrecord, $this->discussionrecord, $student2, $options);
|
||||
|
||||
// Reinitialise capability manager first to ensure we don't return cached values.
|
||||
$capabilitymanager = $this->managerfactory->get_capability_manager($qandaforum);
|
||||
|
||||
// Confirm Student 2 can now reply to student 1's answer.
|
||||
$this->assertTrue($capabilitymanager->can_reply_to_post($student2, $this->discussion, $student1postentity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that can_reply_privately_to_post works as expected.
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user