diff --git a/admin/tool/policy/classes/api.php b/admin/tool/policy/classes/api.php index 39d49a6f328..fc60543ebe7 100644 --- a/admin/tool/policy/classes/api.php +++ b/admin/tool/policy/classes/api.php @@ -309,8 +309,8 @@ class api { return true; } - // Users have access to all the policies they have ever accepted. - if (static::is_user_version_accepted($userid, $policy->id)) { + // Users have access to all the policies they have ever accepted/declined. + if (static::is_user_version_accepted($userid, $policy->id) !== null) { return true; } @@ -719,20 +719,22 @@ class api { } /** - * Returns version acceptance for this user. + * Did the user accept the given policy version? * * @param int $userid User identifier. * @param int $versionid Policy version identifier. - * @param array|null $acceptances Iist of policy version acceptances indexed by versionid. - * @return bool True if this user has accepted this policy version; false otherwise. + * @param array|null $acceptances Pre-loaded list of policy version acceptances indexed by versionid. + * @return bool|null True/false if this user accepted/declined the policy; null otherwise. */ public static function is_user_version_accepted($userid, $versionid, $acceptances = null) { + $acceptance = static::get_user_version_acceptance($userid, $versionid, $acceptances); + if (!empty($acceptance)) { - return $acceptance->status; + return (bool) $acceptance->status; } - return false; + return null; } /** diff --git a/admin/tool/policy/tests/api_test.php b/admin/tool/policy/tests/api_test.php index ed5f3cb6403..d91794f66c5 100644 --- a/admin/tool/policy/tests/api_test.php +++ b/admin/tool/policy/tests/api_test.php @@ -632,4 +632,27 @@ class tool_policy_api_testcase extends advanced_testcase { require_login(null, false, null, false, true); } + + /** + * Test the three-state logic of the value returned by {@link api::is_user_version_accepted()}. + */ + public function test_is_user_version_accepted() { + + $preloadedacceptances = [ + 4 => (object) [ + 'policyversionid' => 4, + 'mainuserid' => 13, + 'status' => 1, + ], + 6 => (object) [ + 'policyversionid' => 6, + 'mainuserid' => 13, + 'status' => 0, + ], + ]; + + $this->assertTrue(api::is_user_version_accepted(13, 4, $preloadedacceptances)); + $this->assertFalse(api::is_user_version_accepted(13, 6, $preloadedacceptances)); + $this->assertNull(api::is_user_version_accepted(13, 5, $preloadedacceptances)); + } }