Skip to content

Commit

Permalink
Merge branch 'MDL-64063-35' of git://github.com/abgreeve/moodle into …
Browse files Browse the repository at this point in the history
…MOODLE_35_STABLE
  • Loading branch information
David Monllao committed Nov 21, 2018
2 parents a23caa4 + 0073588 commit d699efb
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 9 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions blocks/myoverview/amd/src/calendar_events_repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 5 additions & 2 deletions calendar/classes/local/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions calendar/classes/local/event/data_access/event_vault.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

/**
Expand Down
10 changes: 7 additions & 3 deletions calendar/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
Expand All @@ -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');
Expand All @@ -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);
Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions calendar/tests/event_vault_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions calendar/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d699efb

Please sign in to comment.