Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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_course
  21   * @copyright  2018 Adrian Greeve <adrian@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_course\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\approved_userlist;
  33  use \core_privacy\local\request\transform;
  34  use \core_privacy\local\request\userlist;
  35  use \core_privacy\local\request\writer;
  36  
  37  /**
  38   * Privacy class for requesting user data.
  39   *
  40   * @copyright  2018 Adrian Greeve <adrian@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\context_aware_provider,
  46          \core_privacy\local\request\core_userlist_provider,
  47          \core_privacy\local\request\plugin\provider,
  48          \core_privacy\local\request\user_preference_provider {
  49  
  50      /**
  51       * Returns meta data about this system.
  52       *
  53       * @param   collection $collection The initialised collection to add items to.
  54       * @return  collection A listing of user data stored through this system.
  55       */
  56      public static function get_metadata(collection $collection) : collection {
  57          $collection->add_subsystem_link('core_completion', [], 'privacy:metadata:completionsummary');
  58          $collection->add_subsystem_link('core_favourites', [], 'privacy:metadata:favouritessummary');
  59          $collection->add_subsystem_link('core_favourites', [], 'privacy:metadata:activityfavouritessummary');
  60          $collection->add_user_preference('coursecat_management_perpage', 'privacy:perpage');
  61          return $collection;
  62      }
  63  
  64      /**
  65       * Get the list of contexts that contain user information for the specified user.
  66       *
  67       * @param   int $userid The user to search.
  68       * @return  contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
  69       */
  70      public static function get_contexts_for_userid(int $userid) : contextlist {
  71          list($join, $where, $params) = \core_completion\privacy\provider::get_course_completion_join_sql($userid, 'cc', 'c.id');
  72          $sql = "SELECT ctx.id
  73                  FROM {context} ctx
  74                  JOIN {course} c ON ctx.instanceid = c.id AND ctx.contextlevel = :contextcourse
  75                  {$join}
  76                  WHERE {$where}";
  77          $params['contextcourse'] = CONTEXT_COURSE;
  78          $contextlist = new contextlist();
  79          $contextlist->add_from_sql($sql, $params);
  80  
  81          \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $userid, 'core_course', 'courses');
  82  
  83          return $contextlist;
  84      }
  85  
  86      /**
  87       * Get the list of users who have data within a 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_course) {
  95              return;
  96          }
  97  
  98          \core_completion\privacy\provider::add_course_completion_users_to_userlist($userlist);
  99          \core_favourites\privacy\provider::add_userids_for_context($userlist, 'courses');
 100      }
 101  
 102      /**
 103       * Export all user data for the specified user, in the specified contexts.
 104       *
 105       * @param approved_contextlist $contextlist The approved contexts to export information for.
 106       */
 107      public static function export_user_data(approved_contextlist $contextlist) {
 108          global $DB;
 109  
 110          // Get the course.
 111          list($select, $params) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
 112          $params['contextcourse'] = CONTEXT_COURSE;
 113  
 114          $sql = "SELECT c.*
 115                  FROM {course} c
 116                  JOIN {context} ctx ON c.id = ctx.instanceid AND ctx.contextlevel = :contextcourse
 117                  WHERE ctx.id $select";
 118  
 119          $courses = $DB->get_recordset_sql($sql, $params);
 120  
 121          foreach ($courses as $course) {
 122              $coursecompletion = \core_completion\privacy\provider::get_course_completion_info($contextlist->get_user(), $course);
 123              writer::with_context(\context_course::instance($course->id))->export_data(
 124                      [get_string('privacy:completionpath', 'course')], (object) $coursecompletion);
 125              // Get user's favourites information for the particular course.
 126              $coursefavourite = \core_favourites\privacy\provider::get_favourites_info_for_user($contextlist->get_user()->id,
 127                      \context_course::instance($course->id), 'core_course', 'courses', $course->id);
 128              if ($coursefavourite) { // If the course has been favourited by the user, include it in the export.
 129                  writer::with_context(\context_course::instance($course->id))->export_data(
 130                          [get_string('privacy:favouritespath', 'course')], (object) $coursefavourite);
 131              }
 132          }
 133          $courses->close();
 134      }
 135  
 136      /**
 137       * Give the component a chance to include any contextual information deemed relevant to any child contexts which are
 138       * exporting personal data.
 139       *
 140       * By giving the component access to the full list of contexts being exported across all components, it can determine whether a
 141       * descendant context is being exported, and decide whether to add relevant contextual information about itself. Having access
 142       * to the full list of contexts being exported is what makes this component a context aware provider.
 143       *
 144       * E.g.
 145       * If, during the core export process, a course module is included in the contextlist_collection but the course containing the
 146       * module is not (perhaps there's no longer a user enrolment), then the course should include general contextual information in
 147       * the export so we know basic details about which course the module belongs to. This method allows the course to make that
 148       * decision, based on the existence of any decendant module contexts in the collection.
 149       *
 150       * @param \core_privacy\local\request\contextlist_collection $contextlistcollection
 151       */
 152      public static function export_context_data(\core_privacy\local\request\contextlist_collection $contextlistcollection) {
 153          global $DB;
 154  
 155          $coursecontextids = $DB->get_records_menu('context', ['contextlevel' => CONTEXT_COURSE], '', 'id, instanceid');
 156          $courseids = [];
 157          foreach ($contextlistcollection as $component) {
 158              foreach ($component->get_contexts() as $context) {
 159                  // All course contexts have been accounted for, so skip all checks.
 160                  if (empty($coursecontextids)) {
 161                      break;
 162                  }
 163                  // Only course, module, and block contexts are checked.
 164                  if (in_array($context->contextlevel, [CONTEXT_USER, CONTEXT_SYSTEM, CONTEXT_COURSECAT])) {
 165                      continue;
 166                  }
 167                  // If the context is a course, then we just add it without the need to check context path.
 168                  if ($context->contextlevel == CONTEXT_COURSE) {
 169                      $courseids[$context->id] = $context->instanceid;
 170                      unset($coursecontextids[$context->id]);
 171                      continue;
 172                  }
 173                  // Otherwise, we need to check all the course context paths, to see if this context is a descendant.
 174                  foreach ($coursecontextids as $contextid => $instanceid) {
 175                      if (stripos($context->path, '/' . $contextid . '/') !== false) {
 176                          $courseids[$contextid] = $instanceid;
 177                          unset($coursecontextids[$contextid]);
 178                      }
 179                  }
 180              }
 181          }
 182          if (empty($courseids)) {
 183              return;
 184          }
 185  
 186          // Export general data for these contexts.
 187          list($sql, $params) = $DB->get_in_or_equal($courseids);
 188          $sql = 'id ' . $sql;
 189          $coursedata = $DB->get_records_select('course', $sql, $params);
 190  
 191          foreach ($coursedata as $course) {
 192              $context = \context_course::instance($course->id);
 193              $courseformat = $course->format !== 'site' ? get_string('pluginname', 'format_' . $course->format) : get_string('site');
 194              $data = (object) [
 195                  'fullname' => $course->fullname,
 196                  'shortname' => $course->shortname,
 197                  'idnumber' => $course->idnumber,
 198                  'summary' => writer::with_context($context)->rewrite_pluginfile_urls([], 'course', 'summary', 0,
 199                                                                                       format_string($course->summary)),
 200                  'format' => $courseformat,
 201                  'startdate' => transform::datetime($course->startdate),
 202                  'enddate' => transform::datetime($course->enddate)
 203              ];
 204              writer::with_context($context)
 205                      ->export_area_files([], 'course', 'summary', 0)
 206                      ->export_area_files([], 'course', 'overviewfiles', 0)
 207                      ->export_data([], $data);
 208          }
 209      }
 210  
 211      /**
 212       * Export all user preferences for the plugin.
 213       *
 214       * @param int $userid The userid of the user whose data is to be exported.
 215       */
 216      public static function export_user_preferences(int $userid) {
 217          $perpage = get_user_preferences('coursecat_management_perpage', null, $userid);
 218          if (isset($perpage)) {
 219              writer::export_user_preference('core_course',
 220                  'coursecat_management_perpage',
 221                  $perpage,
 222                  get_string('privacy:perpage', 'course')
 223              );
 224          }
 225      }
 226  
 227      /**
 228       * Delete all data for all users in the specified context.
 229       *
 230       * @param context $context The specific context to delete data for.
 231       */
 232      public static function delete_data_for_all_users_in_context(\context $context) {
 233          // Check what context we've been delivered.
 234          if (!$context instanceof \context_course) {
 235              return;
 236          }
 237          // Delete course completion data.
 238          \core_completion\privacy\provider::delete_completion(null, $context->instanceid);
 239          // Delete course favourite data.
 240          \core_favourites\privacy\provider::delete_favourites_for_all_users($context, 'core_course',
 241                  'courses');
 242      }
 243  
 244      /**
 245       * Delete all user data for the specified user, in the specified contexts.
 246       *
 247       * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
 248       */
 249      public static function delete_data_for_user(approved_contextlist $contextlist) {
 250          foreach ($contextlist as $context) {
 251              // Check what context we've been delivered.
 252              if ($context instanceof \context_course) {
 253                  // Delete course completion data.
 254                  \core_completion\privacy\provider::delete_completion($contextlist->get_user(), $context->instanceid);
 255                  // Delete course favourite data.
 256                  \core_favourites\privacy\provider::delete_favourites_for_user($contextlist, 'core_course',
 257                      'courses');
 258              }
 259          }
 260      }
 261  
 262      /**
 263       * Delete multiple users within a single context.
 264       *
 265       * @param   approved_userlist       $userlist The approved context and user information to delete information for.
 266       */
 267      public static function delete_data_for_users(approved_userlist $userlist) {
 268          $context = $userlist->get_context();
 269  
 270          // Check what context we've been delivered.
 271          if (!$context instanceof \context_course) {
 272              return;
 273          }
 274          // Delete course completion data.
 275          \core_completion\privacy\provider::delete_completion_by_approved_userlist($userlist, $context->instanceid);
 276          // Delete course favourite data.
 277          \core_favourites\privacy\provider::delete_favourites_for_userlist($userlist, 'courses');
 278      }
 279  }