From faf4222edcb0baea646df932dbf400f0f539c54f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Fri, 5 Oct 2018 11:00:30 +0200 Subject: [PATCH] MDL-62309 tool_policy: Add api::get_agreement_optional() method This method allows to quickly check if the given policy version is marked as optional or compulsory. This will be needed in other places such as permissions check. --- admin/tool/policy/classes/api.php | 29 ++++++++++++++++++ admin/tool/policy/classes/policy_version.php | 22 ++++++++++++++ admin/tool/policy/db/caches.php | 32 ++++++++++++++++++++ admin/tool/policy/lang/en/tool_policy.php | 1 + admin/tool/policy/tests/api_test.php | 17 +++++++++++ admin/tool/policy/version.php | 2 +- 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 admin/tool/policy/db/caches.php diff --git a/admin/tool/policy/classes/api.php b/admin/tool/policy/classes/api.php index 7add9d45b4a..1a7b9aba63b 100644 --- a/admin/tool/policy/classes/api.php +++ b/admin/tool/policy/classes/api.php @@ -131,6 +131,8 @@ class api { $policies = []; $versions = []; + $optcache = \cache::make('tool_policy', 'policy_optional'); + $rs = $DB->get_recordset_sql($sql, $params); foreach ($rs as $r) { @@ -149,6 +151,8 @@ class api { } $versions[$r->id][$versiondata->id] = $versiondata; + + $optcache->set($versiondata->id, $versiondata->optional); } $rs->close(); @@ -1040,4 +1044,29 @@ class api { $DB->insert_records('tool_policy_acceptances', $acceptances); } } + + /** + * Returns the value of the optional flag for the given policy version. + * + * Optimised for being called multiple times by making use of a request cache. The cache is normally populated as a + * side effect of calling {@link self::list_policies()} and in most cases should be warm enough for hits. + * + * @param int $versionid + * @return int policy_version::AGREEMENT_COMPULSORY | policy_version::AGREEMENT_OPTIONAL + */ + public static function get_agreement_optional($versionid) { + global $DB; + + $optcache = \cache::make('tool_policy', 'policy_optional'); + + $hit = $optcache->get($versionid); + + if ($hit === false) { + $flags = $DB->get_records_menu('tool_policy_versions', null, '', 'id, optional'); + $optcache->set_many($flags); + $hit = $flags[$versionid]; + } + + return $hit; + } } diff --git a/admin/tool/policy/classes/policy_version.php b/admin/tool/policy/classes/policy_version.php index 9f6872beb67..40617ddcf2e 100644 --- a/admin/tool/policy/classes/policy_version.php +++ b/admin/tool/policy/classes/policy_version.php @@ -168,4 +168,26 @@ class policy_version extends persistent { ], ]; } + + /** + * Hook to execute after an update. + * + * @param bool $result Whether or not the update was successful (but it always is) + */ + protected function after_update($result) { + + $optcache = \cache::make('tool_policy', 'policy_optional'); + $optcache->delete($this->raw_get('id')); + } + + /** + * Hook to execute after an update. + * + * @param bool $result Whether or not the update was successful (but it always is) + */ + protected function after_delete($result) { + + $optcache = \cache::make('tool_policy', 'policy_optional'); + $optcache->delete($this->raw_get('id')); + } } diff --git a/admin/tool/policy/db/caches.php b/admin/tool/policy/db/caches.php new file mode 100644 index 00000000000..359118057c2 --- /dev/null +++ b/admin/tool/policy/db/caches.php @@ -0,0 +1,32 @@ +. + +/** + * Defined caches used internally by the plugin. + * + * @package tool_policy + * @category cache + * @copyright 2018 David Mudrák + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$definitions = [ + 'policy_optional' => [ + 'mode' => cache_store::MODE_REQUEST, + ], +]; diff --git a/admin/tool/policy/lang/en/tool_policy.php b/admin/tool/policy/lang/en/tool_policy.php index c771c80d78c..7442ce1667e 100644 --- a/admin/tool/policy/lang/en/tool_policy.php +++ b/admin/tool/policy/lang/en/tool_policy.php @@ -52,6 +52,7 @@ $string['agreedyeswithlinkall'] = 'Consent given; click to withdraw user consent $string['agreepolicies'] = 'Please agree to the following policies'; $string['backtoprevious'] = 'Go back to previous page'; $string['backtotop'] = 'Back to top'; +$string['cachedef_policy_optional'] = 'Cache of the optional/compulsory flag for policy versions'; $string['consentbulk'] = 'Consent'; $string['consentdetails'] = 'Give consent on behalf of user(s)'; $string['consentpagetitle'] = 'Consent'; diff --git a/admin/tool/policy/tests/api_test.php b/admin/tool/policy/tests/api_test.php index 5626a3bbfb8..47543f6af48 100644 --- a/admin/tool/policy/tests/api_test.php +++ b/admin/tool/policy/tests/api_test.php @@ -668,4 +668,21 @@ class tool_policy_api_testcase extends advanced_testcase { $this->assertFalse(api::is_user_version_accepted(13, 6, $preloadedacceptances)); $this->assertNull(api::is_user_version_accepted(13, 5, $preloadedacceptances)); } + + /** + * Test the functionality of {@link api::get_agreement_optional()}. + */ + public function test_get_agreement_optional() { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + + $policy1 = $this->add_policy(['optional' => policy_version::AGREEMENT_OPTIONAL])->to_record(); + api::make_current($policy1->id); + $policy2 = $this->add_policy(['optional' => policy_version::AGREEMENT_COMPULSORY])->to_record(); + api::make_current($policy2->id); + + $this->assertEquals(api::get_agreement_optional($policy1->id), policy_version::AGREEMENT_OPTIONAL); + $this->assertEquals(api::get_agreement_optional($policy2->id), policy_version::AGREEMENT_COMPULSORY); + } } diff --git a/admin/tool/policy/version.php b/admin/tool/policy/version.php index 5fd80fc9dbe..7a66827024a 100644 --- a/admin/tool/policy/version.php +++ b/admin/tool/policy/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2018091800; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2018100100; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2018050800; // Requires this Moodle version. $plugin->component = 'tool_policy'; // Full name of the plugin (used for diagnostics).