Skip to content
Permalink
Browse files
Merge branch 'MDL-64063-35' of git://github.com/abgreeve/moodle into …
…MOODLE_35_STABLE
  • Loading branch information
David Monllao committed Nov 21, 2018
2 parents a23caa4 + 0073588 commit d699efb4ba5eb7dec1cb5c637edadba144e600ff

Some generated files are not rendered by default. Learn more.

@@ -147,6 +147,8 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
args.timesortto = args.endtime;
delete args.endtime;
}
// Don't show events related to courses that the user is suspended in.
args.limittononsuspendedevents = true;

var request = {
methodname: 'core_calendar_get_action_events_by_timesort',
@@ -118,14 +118,16 @@ public static function get_events(
* @param int|null $timesortto The end timesort value (inclusive)
* @param int|null $aftereventid Only return events after this one
* @param int $limitnum Limit results to this amount (between 1 and 50)
* @param bool $lmittononsuspendedevents Limit course events to courses the user is active in (not suspended).
* @return array A list of action_event_interface objects
* @throws \moodle_exception
*/
public static function get_action_events_by_timesort(
$timesortfrom = null,
$timesortto = null,
$aftereventid = null,
$limitnum = 20
$limitnum = 20,
$limittononsuspendedevents = false
) {
global $USER;

@@ -144,7 +146,8 @@ public static function get_action_events_by_timesort(
$afterevent = $event;
}

return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum);
return $vault->get_action_events_by_timesort($USER, $timesortfrom, $timesortto, $afterevent, $limitnum,
$limittononsuspendedevents);
}

/**
@@ -199,11 +199,12 @@ public function get_action_events_by_timesort(
$timesortfrom = null,
$timesortto = null,
event_interface $afterevent = null,
$limitnum = 20
$limitnum = 20,
$limittononsuspendedevents = false
) {
$courseids = array_map(function($course) {
return $course->id;
}, enrol_get_all_users_courses($user->id));
}, enrol_get_all_users_courses($user->id, $limittononsuspendedevents));

$groupids = array_reduce($courseids, function($carry, $courseid) use ($user) {
$groupings = groups_get_user_groups($courseid, $user->id);
@@ -93,14 +93,16 @@ public function get_events(
* @param int $timesortto Events with timesort until this value (inclusive)
* @param event_interface $afterevent Only return events after this one
* @param int $limitnum Return at most this number of events
* @param bool $lmittononsuspendedevents Limit course events to courses the user is active in (not suspended).
* @return event_interface
*/
public function get_action_events_by_timesort(
\stdClass $user,
$timesortfrom,
$timesortto,
event_interface $afterevent,
$limitnum
$limitnum,
$limittononsuspendedevents
);

/**
@@ -403,7 +403,9 @@ public static function get_calendar_action_events_by_timesort_parameters() {
'timesortfrom' => new external_value(PARAM_INT, 'Time sort from', VALUE_DEFAULT, 0),
'timesortto' => new external_value(PARAM_INT, 'Time sort to', VALUE_DEFAULT, null),
'aftereventid' => new external_value(PARAM_INT, 'The last seen event id', VALUE_DEFAULT, 0),
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 20)
'limitnum' => new external_value(PARAM_INT, 'Limit number', VALUE_DEFAULT, 20),
'limittononsuspendedevents' => new external_value(PARAM_BOOL,
'Limit the events to courses the user is not suspended in', VALUE_DEFAULT, false)
)
);
}
@@ -419,7 +421,7 @@ public static function get_calendar_action_events_by_timesort_parameters() {
* @return array
*/
public static function get_calendar_action_events_by_timesort($timesortfrom = 0, $timesortto = null,
$aftereventid = 0, $limitnum = 20) {
$aftereventid = 0, $limitnum = 20, $limittononsuspendedevents = false) {
global $CFG, $PAGE, $USER;

require_once($CFG->dirroot . '/calendar/lib.php');
@@ -432,6 +434,7 @@ public static function get_calendar_action_events_by_timesort($timesortfrom = 0,
'timesortto' => $timesortto,
'aftereventid' => $aftereventid,
'limitnum' => $limitnum,
'limittononsuspendedevents' => $limittononsuspendedevents
]
);
$context = \context_user::instance($USER->id);
@@ -446,7 +449,8 @@ public static function get_calendar_action_events_by_timesort($timesortfrom = 0,
$params['timesortfrom'],
$params['timesortto'],
$params['aftereventid'],
$params['limitnum']
$params['limitnum'],
$params['limittononsuspendedevents']
);

$exportercache = new events_related_objects_cache($events);
@@ -557,6 +557,37 @@ public function test_get_action_events_by_timesort_with_identical_group_override
$this->assertEquals('Assignment 1 due date', $usersevents['For user in no groups'][0]->get_name());
}

/**
* Test that if a user is suspended that events related to that course are not shown.
* User 1 is suspended. User 2 is active.
*/
public function test_get_action_events_by_timesort_with_suspended_user() {
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$this->setAdminuser();
$lesson = $this->getDataGenerator()->create_module('lesson', [
'name' => 'Lesson 1',
'course' => $course->id,
'available' => time(),
'deadline' => (time() + (60 * 60 * 24 * 5))
]
);
$this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);

$factory = new action_event_test_factory();
$strategy = new raw_event_retrieval_strategy();
$vault = new event_vault($factory, $strategy);

$user1events = $vault->get_action_events_by_timesort($user1, null, null, null, 20, true);
$this->assertEmpty($user1events);
$user2events = $vault->get_action_events_by_timesort($user2, null, null, null, 20, true);
$this->assertCount(1, $user2events);
$this->assertEquals('Lesson 1 closes', $user2events[0]->get_name());
}

/**
* Test that get_action_events_by_course returns events after the
* provided timesort value.
@@ -937,6 +937,34 @@ public function test_get_calendar_action_events_by_timesort_time_limit_offset()
$this->assertNull($result['lastid']);
}

/**
* Check that it is possible to restrict the calendar events to events where the user is not suspended in the course.
*/
public function test_get_calendar_action_events_by_timesort_suspended_course() {
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$this->setAdminUser();
$lesson = $this->getDataGenerator()->create_module('lesson', [
'name' => 'Lesson 1',
'course' => $course->id,
'available' => time(),
'deadline' => (time() + (60 * 60 * 24 * 5))
]
);
$this->getDataGenerator()->enrol_user($user1->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
$this->getDataGenerator()->enrol_user($user2->id, $course->id);

$this->setUser($user1);
$result = core_calendar_external::get_calendar_action_events_by_timesort(0, null, 0, 20, true);
$this->assertEmpty($result->events);
$this->setUser($user2);
$result = core_calendar_external::get_calendar_action_events_by_timesort(0, null, 0, 20, true);
$this->assertCount(1, $result->events);
$this->assertEquals('Lesson 1 closes', $result->events[0]->name);
}

/**
* Requesting calendar events from a given course and time should return all
* events with a sort time at or after the requested time. All events prior

0 comments on commit d699efb

Please sign in to comment.