2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Contains helper class for digital consent.
21 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Helper class for digital consent.
32 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class digital_consent
{
38 * Returns true if age and location verification is enabled in the site.
42 public static function is_age_digital_consent_verification_enabled() {
45 return !empty($CFG->agedigitalconsentverification
);
49 * Checks if a user is a digital minor.
52 * @param string $country The country code (ISO 3166-2)
55 public static function is_minor($age, $country) {
58 $ageconsentmap = $CFG->agedigitalconsentmap
;
59 $agedigitalconsentmap = self
::parse_age_digital_consent_map($ageconsentmap);
61 return array_key_exists($country, $agedigitalconsentmap) ?
62 $age < $agedigitalconsentmap[$country] : $age < $agedigitalconsentmap['*'];
66 * Parse the agedigitalconsentmap setting into an array.
68 * @param string $ageconsentmap The value of the agedigitalconsentmap setting
69 * @return array $ageconsentmapparsed
71 public static function parse_age_digital_consent_map($ageconsentmap) {
73 $ageconsentmapparsed = array();
74 $countries = get_string_manager()->get_list_of_countries(true);
75 $isdefaultvaluepresent = false;
76 $lines = preg_split('/\r|\n/', $ageconsentmap, -1, PREG_SPLIT_NO_EMPTY
);
77 foreach ($lines as $line) {
78 $arr = explode(",", $line);
79 // Handle if there is more or less than one comma separator.
80 if (count($arr) != 2) {
81 throw new \
moodle_exception('agedigitalconsentmapinvalidcomma', 'error', '', $line);
83 $country = trim($arr[0]);
86 if ($country == "*") {
87 $isdefaultvaluepresent = true;
89 // Handle if the presented value for country is not valid.
90 if ($country !== "*" && !array_key_exists($country, $countries)) {
91 throw new \
moodle_exception('agedigitalconsentmapinvalidcountry', 'error', '', $country);
93 // Handle if the presented value for age is not valid.
94 if (!is_numeric($age)) {
95 throw new \
moodle_exception('agedigitalconsentmapinvalidage', 'error', '', $age);
97 $ageconsentmapparsed[$country] = $age;
99 // Handle if a default value does not exist.
100 if (!$isdefaultvaluepresent) {
101 throw new \
moodle_exception('agedigitalconsentmapinvaliddefault');
104 return $ageconsentmapparsed;