Permalink
Browse files

Merge branch 'MDL-63139-34' of git://github.com/rezaies/moodle into M…

…OODLE_34_STABLE
  • Loading branch information...
dmonllao committed Sep 18, 2018
2 parents 4a4f563 + 5eae317 commit 693959a524ff2ec959979fba1103869bd692b5b2
Showing with 132 additions and 6 deletions.
  1. +18 −6 mod/forum/lib.php
  2. +114 −0 mod/forum/tests/lib_test.php
@@ -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;
@@ -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;
}
@@ -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;
@@ -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;

0 comments on commit 693959a

Please sign in to comment.