Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   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.
   8  //
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Contains helper class for digital consent.
  19   *
  20   * @package     core_auth
  21   * @copyright   2018 Mihail Geshoski <mihail@moodle.com>
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_auth;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Helper class for digital consent.
  31   *
  32   * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class digital_consent {
  36  
  37      /**
  38       * Returns true if age and location verification is enabled in the site.
  39       *
  40       * @return bool
  41       */
  42      public static function is_age_digital_consent_verification_enabled() {
  43          global $CFG;
  44  
  45          return !empty($CFG->agedigitalconsentverification);
  46      }
  47  
  48      /**
  49       * Checks if a user is a digital minor.
  50       *
  51       * @param int $age
  52       * @param string $country The country code (ISO 3166-2)
  53       * @return bool
  54       */
  55      public static function is_minor($age, $country) {
  56          global $CFG;
  57  
  58          $ageconsentmap = $CFG->agedigitalconsentmap;
  59          $agedigitalconsentmap = self::parse_age_digital_consent_map($ageconsentmap);
  60  
  61          return array_key_exists($country, $agedigitalconsentmap) ?
  62              $age < $agedigitalconsentmap[$country] : $age < $agedigitalconsentmap['*'];
  63      }
  64  
  65      /**
  66       * Parse the agedigitalconsentmap setting into an array.
  67       *
  68       * @param  string $ageconsentmap The value of the agedigitalconsentmap setting
  69       * @return array $ageconsentmapparsed
  70       */
  71      public static function parse_age_digital_consent_map($ageconsentmap) {
  72  
  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);
  82              }
  83              $country = trim($arr[0]);
  84              $age = trim($arr[1]);
  85              // Check if default.
  86              if ($country == "*") {
  87                  $isdefaultvaluepresent = true;
  88              }
  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);
  92              }
  93              // Handle if the presented value for age is not valid.
  94              if (!is_numeric($age)) {
  95                  throw new \moodle_exception('agedigitalconsentmapinvalidage', 'error', '', $age);
  96              }
  97              $ageconsentmapparsed[$country] = $age;
  98          }
  99          // Handle if a default value does not exist.
 100          if (!$isdefaultvaluepresent) {
 101              throw new \moodle_exception('agedigitalconsentmapinvaliddefault');
 102          }
 103  
 104          return $ageconsentmapparsed;
 105      }
 106  }