Skip to content

Commit

Permalink
Merge branch 'MDL-63139-35' of git://github.com/rezaies/moodle into M…
Browse files Browse the repository at this point in the history
…OODLE_35_STABLE
  • Loading branch information
David Monllao committed Sep 18, 2018
2 parents a266d8a + 7e973b2 commit 4e6ec59
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 6 deletions.
24 changes: 18 additions & 6 deletions mod/forum/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -8331,22 +8331,34 @@ function mod_forum_core_calendar_event_action_shows_item_count(calendar_event $e
*
* @param calendar_event $event
* @param \core_calendar\action_factory $factory
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
* @return \core_calendar\local\event\entities\action_interface|null
*/
function mod_forum_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
\core_calendar\action_factory $factory,
int $userid = 0) {
global $DB, $USER;

$cm = get_fast_modinfo($event->courseid)->instances['forum'][$event->instance];
if (!$userid) {
$userid = $USER->id;
}

$cm = get_fast_modinfo($event->courseid, $userid)->instances['forum'][$event->instance];

if (!$cm->uservisible) {
// The module is not visible to the user for any reason.
return null;
}

$context = context_module::instance($cm->id);

if (!has_capability('mod/forum:viewdiscussion', $context)) {
if (!has_capability('mod/forum:viewdiscussion', $context, $userid)) {
return null;
}

$completion = new \completion_info($cm->get_course());

$completiondata = $completion->get_data($cm, false);
$completiondata = $completion->get_data($cm, false, $userid);

if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
return null;
Expand All @@ -8363,10 +8375,10 @@ function mod_forum_core_calendar_provide_event_action(calendar_event $event,
INNER JOIN {forum_discussions} fd ON fp.discussion=fd.id
WHERE
fp.userid=:userid AND fd.forum=:forumid";
$postcountparams = array('userid' => $USER->id, 'forumid' => $forum->id);
$postcountparams = array('userid' => $userid, 'forumid' => $forum->id);

if ($forum->completiondiscussions) {
$count = $DB->count_records('forum_discussions', array('forum' => $forum->id, 'userid' => $USER->id));
$count = $DB->count_records('forum_discussions', array('forum' => $forum->id, 'userid' => $userid));
$itemcount += ($forum->completiondiscussions >= $count) ? ($forum->completiondiscussions - $count) : 0;
}

Expand Down
114 changes: 114 additions & 0 deletions mod/forum/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3350,6 +3350,81 @@ public function test_forum_core_calendar_provide_event_action() {
$this->assertTrue($actionevent->is_actionable());
}

public function test_forum_core_calendar_provide_event_action_in_hidden_section() {
global $CFG;

$this->resetAfterTest();
$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();

// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');

// Create the activity.
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id,
'completionreplies' => 5, 'completiondiscussions' => 2));

// Create a calendar event.
$event = $this->create_action_event($course->id, $forum->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);

// Set sections 0 as hidden.
set_section_visible($course->id, 0, 0);

// Now, log out.
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
$this->setUser();

// Create an action factory.
$factory = new \core_calendar\action_factory();

// Decorate action event for the student.
$actionevent = mod_forum_core_calendar_provide_event_action($event, $factory, $student->id);

// Confirm the event is not shown at all.
$this->assertNull($actionevent);
}

public function test_forum_core_calendar_provide_event_action_for_user() {
global $CFG;

$this->resetAfterTest();
$this->setAdminUser();

// Create a course.
$course = $this->getDataGenerator()->create_course();

// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');

// Create the activity.
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id,
'completionreplies' => 5, 'completiondiscussions' => 2));

// Create a calendar event.
$event = $this->create_action_event($course->id, $forum->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);

// Now log out.
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
$this->setUser();

// Create an action factory.
$factory = new \core_calendar\action_factory();

// Decorate action event for the student.
$actionevent = mod_forum_core_calendar_provide_event_action($event, $factory, $student->id);

// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('view'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(7, $actionevent->get_item_count());
$this->assertTrue($actionevent->is_actionable());
}

public function test_forum_core_calendar_provide_event_action_as_non_user() {
global $CFG;

Expand Down Expand Up @@ -3412,6 +3487,45 @@ public function test_forum_core_calendar_provide_event_action_already_completed(
$this->assertNull($actionevent);
}

public function test_forum_core_calendar_provide_event_action_already_completed_for_user() {
global $CFG;

$this->resetAfterTest();
$this->setAdminUser();

$CFG->enablecompletion = 1;

// Create a course.
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));

// Create a student.
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');

// Create the activity.
$forum = $this->getDataGenerator()->create_module('forum', array('course' => $course->id),
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));

// Get some additional data.
$cm = get_coursemodule_from_instance('forum', $forum->id);

// Create a calendar event.
$event = $this->create_action_event($course->id, $forum->id,
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);

// Mark the activity as completed for the student.
$completion = new completion_info($course);
$completion->set_module_viewed($cm, $student->id);

// Create an action factory.
$factory = new \core_calendar\action_factory();

// Decorate action event.
$actionevent = mod_forum_core_calendar_provide_event_action($event, $factory, $student->id);

// Ensure result was null.
$this->assertNull($actionevent);
}

public function test_mod_forum_get_tagged_posts() {
global $DB;

Expand Down

0 comments on commit 4e6ec59

Please sign in to comment.