diff --git a/blocks/myoverview/amd/build/calendar_events_repository.min.js b/blocks/myoverview/amd/build/calendar_events_repository.min.js index 5c6e35a1419..d9f1640c5df 100644 --- a/blocks/myoverview/amd/build/calendar_events_repository.min.js +++ b/blocks/myoverview/amd/build/calendar_events_repository.min.js @@ -1 +1 @@ -define(["jquery","core/ajax","core/notification"],function(a,b,c){var d=20,e=function(a){a.hasOwnProperty("limit")||(a.limit=d),a.limitnum=a.limit,delete a.limit,a.hasOwnProperty("starttime")&&(a.timesortfrom=a.starttime,delete a.starttime),a.hasOwnProperty("endtime")&&(a.timesortto=a.endtime,delete a.endtime);var e={methodname:"core_calendar_get_action_events_by_course",args:a},f=b.call([e])[0];return f.fail(c.exception),f},f=function(a){a.hasOwnProperty("limit")||(a.limit=10),a.limitnum=a.limit,delete a.limit,a.hasOwnProperty("starttime")&&(a.timesortfrom=a.starttime,delete a.starttime),a.hasOwnProperty("endtime")&&(a.timesortto=a.endtime,delete a.endtime);var d={methodname:"core_calendar_get_action_events_by_courses",args:a},e=b.call([d])[0];return e.fail(c.exception),e},g=function(a){a.hasOwnProperty("limit")||(a.limit=d),a.limitnum=a.limit,delete a.limit,a.hasOwnProperty("starttime")&&(a.timesortfrom=a.starttime,delete a.starttime),a.hasOwnProperty("endtime")&&(a.timesortto=a.endtime,delete a.endtime);var e={methodname:"core_calendar_get_action_events_by_timesort",args:a},f=b.call([e])[0];return f.fail(c.exception),f};return{queryByTime:g,queryByCourse:e,queryByCourses:f}}); \ No newline at end of file +define(["jquery","core/ajax","core/notification"],function(a,b,c){var d=20,e=function(a){a.hasOwnProperty("limit")||(a.limit=d),a.limitnum=a.limit,delete a.limit,a.hasOwnProperty("starttime")&&(a.timesortfrom=a.starttime,delete a.starttime),a.hasOwnProperty("endtime")&&(a.timesortto=a.endtime,delete a.endtime);var e={methodname:"core_calendar_get_action_events_by_course",args:a},f=b.call([e])[0];return f.fail(c.exception),f},f=function(a){a.hasOwnProperty("limit")||(a.limit=10),a.limitnum=a.limit,delete a.limit,a.hasOwnProperty("starttime")&&(a.timesortfrom=a.starttime,delete a.starttime),a.hasOwnProperty("endtime")&&(a.timesortto=a.endtime,delete a.endtime);var d={methodname:"core_calendar_get_action_events_by_courses",args:a},e=b.call([d])[0];return e.fail(c.exception),e},g=function(a){a.hasOwnProperty("limit")||(a.limit=d),a.limitnum=a.limit,delete a.limit,a.hasOwnProperty("starttime")&&(a.timesortfrom=a.starttime,delete a.starttime),a.hasOwnProperty("endtime")&&(a.timesortto=a.endtime,delete a.endtime),a.limittononsuspendedevents=!0;var e={methodname:"core_calendar_get_action_events_by_timesort",args:a},f=b.call([e])[0];return f.fail(c.exception),f};return{queryByTime:g,queryByCourse:e,queryByCourses:f}}); \ No newline at end of file diff --git a/blocks/myoverview/amd/src/calendar_events_repository.js b/blocks/myoverview/amd/src/calendar_events_repository.js index 64c06e065b8..5e53e5ad407 100644 --- a/blocks/myoverview/amd/src/calendar_events_repository.js +++ b/blocks/myoverview/amd/src/calendar_events_repository.js @@ -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', diff --git a/calendar/classes/local/api.php b/calendar/classes/local/api.php index ac0f613b392..c182c55b90b 100644 --- a/calendar/classes/local/api.php +++ b/calendar/classes/local/api.php @@ -118,6 +118,7 @@ class api { * @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 */ @@ -125,7 +126,8 @@ class api { $timesortfrom = null, $timesortto = null, $aftereventid = null, - $limitnum = 20 + $limitnum = 20, + $limittononsuspendedevents = false ) { global $USER; @@ -144,7 +146,8 @@ class api { $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); } /** diff --git a/calendar/classes/local/event/data_access/event_vault.php b/calendar/classes/local/event/data_access/event_vault.php index 6c42d3c2e53..3773a0ec834 100644 --- a/calendar/classes/local/event/data_access/event_vault.php +++ b/calendar/classes/local/event/data_access/event_vault.php @@ -199,11 +199,12 @@ class event_vault implements event_vault_interface { $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); diff --git a/calendar/classes/local/event/data_access/event_vault_interface.php b/calendar/classes/local/event/data_access/event_vault_interface.php index 15c5a0cad14..28b98152c65 100644 --- a/calendar/classes/local/event/data_access/event_vault_interface.php +++ b/calendar/classes/local/event/data_access/event_vault_interface.php @@ -93,6 +93,7 @@ interface event_vault_interface { * @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( @@ -100,7 +101,8 @@ interface event_vault_interface { $timesortfrom, $timesortto, event_interface $afterevent, - $limitnum + $limitnum, + $limittononsuspendedevents ); /** diff --git a/calendar/externallib.php b/calendar/externallib.php index 80e60cd8667..983cb085bbe 100644 --- a/calendar/externallib.php +++ b/calendar/externallib.php @@ -403,7 +403,9 @@ class core_calendar_external extends external_api { '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 @@ class core_calendar_external extends external_api { * @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 @@ class core_calendar_external extends external_api { 'timesortto' => $timesortto, 'aftereventid' => $aftereventid, 'limitnum' => $limitnum, + 'limittononsuspendedevents' => $limittononsuspendedevents ] ); $context = \context_user::instance($USER->id); @@ -446,7 +449,8 @@ class core_calendar_external extends external_api { $params['timesortfrom'], $params['timesortto'], $params['aftereventid'], - $params['limitnum'] + $params['limitnum'], + $params['limittononsuspendedevents'] ); $exportercache = new events_related_objects_cache($events); diff --git a/calendar/tests/event_vault_test.php b/calendar/tests/event_vault_test.php index e4a1b21236e..5d90f93b3d8 100644 --- a/calendar/tests/event_vault_test.php +++ b/calendar/tests/event_vault_test.php @@ -557,6 +557,37 @@ class core_calendar_event_vault_testcase extends advanced_testcase { $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. diff --git a/calendar/tests/externallib_test.php b/calendar/tests/externallib_test.php index a63e3b1deac..9d6c6d17f9f 100644 --- a/calendar/tests/externallib_test.php +++ b/calendar/tests/externallib_test.php @@ -937,6 +937,34 @@ class core_calendar_externallib_testcase extends externallib_advanced_testcase { $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