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.
This commit is contained in:
David Mudrák 2018-10-05 11:00:30 +02:00
parent 1617fde9a8
commit faf4222edc
6 changed files with 102 additions and 1 deletions

View file

@ -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;
}
}

View file

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

View file

@ -0,0 +1,32 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defined caches used internally by the plugin.
*
* @package tool_policy
* @category cache
* @copyright 2018 David Mudrák <david@moodle.com>
* @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,
],
];

View file

@ -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';

View file

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

View file

@ -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).