Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   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    search_simpledb
  21   * @copyright  2018 David Monllao
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace search_simpledb\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_privacy\local\metadata\collection;
  29  use core_privacy\local\request\contextlist;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\approved_userlist;
  32  use core_privacy\local\request\transform;
  33  use core_privacy\local\request\userlist;
  34  use core_privacy\local\request\writer;
  35  
  36  /**
  37   * Provider for the search_simpledb plugin.
  38   *
  39   * @copyright  2018 David Monllao
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider implements
  43          \core_privacy\local\metadata\provider,
  44          \core_privacy\local\request\core_userlist_provider,
  45          \core_privacy\local\request\plugin\provider {
  46  
  47      /**
  48       * Returns meta data about this system.
  49       *
  50       * @param   collection $collection The initialised collection to add items to.
  51       * @return  collection     A listing of user data stored through this system.
  52       */
  53      public static function get_metadata(collection $collection) : collection {
  54          $collection->add_database_table(
  55              'search_simpledb_index',
  56              [
  57                  'docid' => 'privacy:metadata:index:docid',
  58                  'itemid' => 'privacy:metadata:index:itemid',
  59                  'title' => 'privacy:metadata:index:title',
  60                  'content' => 'privacy:metadata:index:content',
  61                  'contextid' => 'privacy:metadata:index:contextid',
  62                  'areaid' => 'privacy:metadata:index:areaid',
  63                  'type' => 'privacy:metadata:index:type',
  64                  'courseid' => 'privacy:metadata:index:courseid',
  65                  'owneruserid' => 'privacy:metadata:index:owneruserid',
  66                  'modified' => 'privacy:metadata:index:modified',
  67                  'userid' => 'privacy:metadata:index:userid',
  68                  'description1' => 'privacy:metadata:index:description1',
  69                  'description2' => 'privacy:metadata:index:description2',
  70              ],
  71              'privacy:metadata:index'
  72          );
  73          return $collection;
  74      }
  75  
  76      /**
  77       * Get the list of contexts that contain user information for the specified user.
  78       *
  79       * @param   int         $userid     The user to search.
  80       * @return  contextlist   $contextlist  The contextlist containing the list of contexts used in this plugin.
  81       */
  82      public static function get_contexts_for_userid(int $userid) : contextlist {
  83          $contextlist = new \core_privacy\local\request\contextlist();
  84  
  85          $params = ['userid' => $userid, 'owneruserid' => $userid];
  86          $sql = "SELECT DISTINCT contextid
  87                    FROM {search_simpledb_index}
  88                   WHERE (userid = :userid OR owneruserid = :owneruserid)";
  89          $contextlist->add_from_sql($sql, $params);
  90  
  91          return $contextlist;
  92      }
  93  
  94      /**
  95       * Get the list of users who have data within a context.
  96       *
  97       * @param   userlist    $userlist   The userlist containing the list of users who have data in this context/plugin combination.
  98       */
  99      public static function get_users_in_context(userlist $userlist) {
 100          $context = $userlist->get_context();
 101  
 102          $params = [
 103              'contextid' => $context->id,
 104          ];
 105  
 106          $sql = "SELECT ssi.userid
 107                    FROM {search_simpledb_index} ssi
 108                   WHERE ssi.contextid = :contextid";
 109  
 110          $userlist->add_from_sql('userid', $sql, $params);
 111  
 112          $sql = "SELECT ssi.owneruserid AS userid
 113                    FROM {search_simpledb_index} ssi
 114                   WHERE ssi.contextid = :contextid";
 115  
 116          $userlist->add_from_sql('userid', $sql, $params);
 117      }
 118  
 119      /**
 120       * Export all user data for the specified user, in the specified contexts.
 121       *
 122       * @param   approved_contextlist    $contextlist    The approved contexts to export information for.
 123       */
 124      public static function export_user_data(approved_contextlist $contextlist) {
 125          global $DB;
 126  
 127          // Plugin search_simpledb uses the default document object (core_search\document) which uses FORMAT_PLAIN.
 128          $textformat = FORMAT_PLAIN;
 129  
 130          $userid = $contextlist->get_user()->id;
 131  
 132          $ctxfields = \context_helper::get_preload_record_columns_sql('ctx');
 133          list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
 134          $sql = "SELECT ssi.*, $ctxfields FROM {search_simpledb_index} ssi
 135                    JOIN {context} ctx ON ctx.id = ssi.contextid
 136                   WHERE ssi.contextid $contextsql AND (ssi.userid = :userid OR ssi.owneruserid = :owneruserid)";
 137          $params = ['userid' => $userid, 'owneruserid' => $userid] + $contextparams;
 138  
 139          $records = $DB->get_recordset_sql($sql, $params);
 140          foreach ($records as $record) {
 141  
 142              \context_helper::preload_from_record($record);
 143              $context = \context::instance_by_id($record->contextid);
 144              $document = (object)[
 145                  'title' => format_string($record->title, true, ['context' => $context]),
 146                  'content' => format_text($record->content, $textformat, ['context' => $context]),
 147                  'description1' => format_text($record->description1, $textformat, ['context' => $context]),
 148                  'description2' => format_text($record->description2, $textformat, ['context' => $context]),
 149                  'context' => $context->get_context_name(true, true),
 150                  'modified' => transform::datetime($record->modified),
 151  
 152              ];
 153  
 154              $path = [get_string('search', 'search'), $record->docid];
 155              writer::with_context($context)->export_data($path, $document);
 156          }
 157          $records->close();
 158      }
 159  
 160      /**
 161       * Delete all data for all users in the specified context.
 162       *
 163       * @param   context                 $context   The specific context to delete data for.
 164       */
 165      public static function delete_data_for_all_users_in_context(\context $context) {
 166          global $DB;
 167  
 168          $DB->delete_records('search_simpledb_index', ['contextid' => $context->id]);
 169  
 170          if ($context->contextlevel == CONTEXT_USER) {
 171              $select = "userid = :userid OR owneruserid = :owneruserid";
 172              $params = ['userid' => $context->instanceid, 'owneruserid' => $context->instanceid];
 173              $DB->delete_records_select('search_simpledb_index', $select, $params);
 174          }
 175      }
 176  
 177      /**
 178       * Delete all user data for the specified user, in the specified contexts.
 179       *
 180       * @param   approved_contextlist    $contextlist    The approved contexts and user information to delete information for.
 181       */
 182      public static function delete_data_for_user(approved_contextlist $contextlist) {
 183          global $DB;
 184  
 185          $userid = $contextlist->get_user()->id;
 186  
 187          list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
 188          $select = "contextid $contextsql AND (userid = :userid OR owneruserid = :owneruserid)";
 189          $params = ['userid' => $userid, 'owneruserid' => $userid] + $contextparams;
 190          $DB->delete_records_select('search_simpledb_index', $select, $params);
 191      }
 192  
 193      /**
 194       * Delete multiple users within a single context.
 195       *
 196       * @param   approved_userlist       $userlist The approved context and user information to delete information for.
 197       */
 198      public static function delete_data_for_users(approved_userlist $userlist) {
 199          global $DB;
 200          $context = $userlist->get_context();
 201  
 202          list($usersql, $userparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
 203          list($ownersql, $ownerparams) = $DB->get_in_or_equal($userlist->get_userids(), SQL_PARAMS_NAMED);
 204  
 205          $select = "contextid = :contextid AND (userid {$usersql} OR owneruserid {$ownersql})";
 206          $params = ['contextid' => $context->id] + $userparams + $ownerparams;
 207  
 208          $DB->delete_records_select('search_simpledb_index', $select, $params);
 209      }
 210  }