Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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   * Privacy class for requesting user data.
  19   *
  20   * @package    core_cohort
  21   * @copyright  2018 Sara Arjona <sara@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_cohort\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\metadata\collection;
  30  use core_privacy\local\request\contextlist;
  31  use core_privacy\local\request\approved_contextlist;
  32  use core_privacy\local\request\transform;
  33  use core_privacy\local\request\writer;
  34  use core_privacy\local\request\userlist;
  35  use core_privacy\local\request\approved_userlist;
  36  
  37  /**
  38   * Privacy class for requesting user data.
  39   *
  40   * @copyright  2018 Sara Arjona <sara@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class provider implements
  44          \core_privacy\local\metadata\provider,
  45          \core_privacy\local\request\core_userlist_provider,
  46          \core_privacy\local\request\plugin\provider {
  47  
  48      /**
  49       * Return the fields which contain personal data.
  50       *
  51       * @param   collection $collection The initialised collection to add items to.
  52       * @return  collection A listing of user data stored through this system.
  53       */
  54      public static function get_metadata(collection $collection) : collection {
  55          $collection->add_database_table('cohort_members', [
  56                  'cohortid' => 'privacy:metadata:cohort_members:cohortid',
  57                  'userid' => 'privacy:metadata:cohort_members:userid',
  58                  'timeadded' => 'privacy:metadata:cohort_members:timeadded'
  59              ], 'privacy:metadata:cohort_members');
  60          return $collection;
  61      }
  62  
  63      /**
  64       * Get the list of contexts that contain user information for the specified user.
  65       *
  66       * @param int $userid The user to search.
  67       * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  68       */
  69      public static function get_contexts_for_userid(int $userid) : contextlist {
  70          $sql = "SELECT ctx.id
  71                    FROM {context} ctx
  72              INNER JOIN {cohort} c ON c.contextid = ctx.id
  73              INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
  74                   WHERE cm.userid = :userid AND (ctx.contextlevel = :contextlevel1 OR ctx.contextlevel = :contextlevel2)";
  75          $params = [
  76              'userid'        => $userid,
  77              'contextlevel1' => CONTEXT_SYSTEM,
  78              'contextlevel2' => CONTEXT_COURSECAT,
  79          ];
  80          $contextlist = new contextlist();
  81          $contextlist->add_from_sql($sql, $params);
  82  
  83          return $contextlist;
  84      }
  85  
  86      /**
  87       * Get the list of users within a specific context.
  88       *
  89       * @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
  90       */
  91      public static function get_users_in_context(userlist $userlist) {
  92          $context = $userlist->get_context();
  93  
  94          if (!$context instanceof \context_system && !$context instanceof \context_coursecat) {
  95              return;
  96          }
  97  
  98          $params = ['contextid' => $context->id];
  99  
 100          $sql = "SELECT cm.userid
 101                    FROM {cohort_members} cm
 102                    JOIN {cohort} c ON cm.cohortid = c.id
 103                   WHERE c.contextid = :contextid";
 104  
 105          $userlist->add_from_sql('userid', $sql, $params);
 106      }
 107  
 108      /**
 109       * Export all user data for the specified user, in the specified contexts.
 110       *
 111       * @param approved_contextlist $contextlist The approved contexts to export information for.
 112       */
 113      public static function export_user_data(approved_contextlist $contextlist) {
 114          global $DB;
 115  
 116          // Remove contexts different from SYSTEM or COURSECAT.
 117          $contexts = array_reduce($contextlist->get_contexts(), function($carry, $context) {
 118              if ($context->contextlevel == CONTEXT_SYSTEM || $context->contextlevel == CONTEXT_COURSECAT) {
 119                  $carry[] = $context->id;
 120              }
 121              return $carry;
 122          }, []);
 123  
 124          if (empty($contexts)) {
 125              return;
 126          }
 127  
 128          // Get cohort data.
 129          $userid = $contextlist->get_user()->id;
 130          list($contextsql, $contextparams) = $DB->get_in_or_equal($contexts, SQL_PARAMS_NAMED);
 131          $sql = "SELECT c.name,
 132                         c.idnumber,
 133                         c.description,
 134                         c.visible,
 135                         cm.timeadded,
 136                         ctx.id as contextid
 137                    FROM {context} ctx
 138              INNER JOIN {cohort} c ON c.contextid = ctx.id
 139              INNER JOIN {cohort_members} cm ON cm.cohortid = c.id
 140                   WHERE ctx.id {$contextsql}
 141                         AND cm.userid = :userid";
 142          $params = [
 143                  'userid'        => $userid
 144              ] + $contextparams;
 145  
 146          $cohorts = $DB->get_recordset_sql($sql, $params);
 147          foreach ($cohorts as $cohort) {
 148              $alldata[$cohort->contextid][] = (object)[
 149                      'name' => $cohort->name,
 150                      'idnumber' => $cohort->idnumber,
 151                      'visible' => transform::yesno($cohort->visible),
 152                      'timeadded' => transform::datetime($cohort->timeadded),
 153                  ];
 154          }
 155          $cohorts->close();
 156  
 157          // Export cohort data.
 158          array_walk($alldata, function($data, $contextid) {
 159              $context = \context::instance_by_id($contextid);
 160              writer::with_context($context)->export_related_data([], 'cohort', $data);
 161          });
 162  
 163      }
 164  
 165      /**
 166       * Delete all use data which matches the specified context.
 167       *
 168       * @param context $context A user context.
 169       */
 170      public static function delete_data_for_all_users_in_context(\context $context) {
 171          if (!$context instanceof \context_system && !$context instanceof \context_coursecat) {
 172              return;
 173          }
 174  
 175          static::delete_data($context);
 176      }
 177  
 178      /**
 179       * Delete multiple users within a single context.
 180       *
 181       * @param approved_userlist $userlist The approved context and user information to delete information for.
 182       */
 183      public static function delete_data_for_users(approved_userlist $userlist) {
 184          $context = $userlist->get_context();
 185  
 186          if ($context instanceof \context_system || $context instanceof \context_coursecat) {
 187              foreach ($userlist->get_userids() as $userid) {
 188                  static::delete_data($context, $userid);
 189              }
 190          }
 191      }
 192  
 193      /**
 194       * Delete all user data for the specified user, in the specified contexts.
 195       *
 196       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 197       */
 198      public static function delete_data_for_user(approved_contextlist $contextlist) {
 199          if (empty($contextlist->count())) {
 200              return;
 201          }
 202  
 203          $userid = $contextlist->get_user()->id;
 204          foreach ($contextlist->get_contexts() as $context) {
 205              if (!$context instanceof \context_system && !$context instanceof \context_coursecat) {
 206                  continue;
 207              }
 208              static::delete_data($context, $userid);
 209          }
 210      }
 211  
 212      /**
 213       * Delete data related to a context and user (if defined).
 214       *
 215       * @param context $context A context.
 216       * @param int $userid The user ID.
 217       */
 218      protected static function delete_data(\context $context, int $userid = null) {
 219          global $DB;
 220  
 221          $cohortids = $DB->get_fieldset_select('cohort', 'id', 'contextid = :contextid', ['contextid' => $context->id]);
 222          foreach ($cohortids as $cohortid) {
 223              $params = ['cohortid' => $cohortid];
 224              if (!empty($userid)) {
 225                  $params['userid'] = $userid;
 226              }
 227              $DB->delete_records('cohort_members', $params);
 228          }
 229      }
 230  }