PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/report/customsql/classes/privacy/provider.php

https://github.com/CLAMP-IT/moodle
PHP | 237 lines | 128 code | 22 blank | 87 comment | 8 complexity | 08af81e66fecaf90247c5298a865cc5d MD5 | raw file
  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. * Privacy Subsystem implementation for report_customsql.
  18. *
  19. * @package report_customsql
  20. * @copyright 2018 The Open University
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace report_customsql\privacy;
  24. use context;
  25. use core_privacy\local\metadata\collection;
  26. use core_privacy\local\request;
  27. /**
  28. * Privacy Subsystem for report_customsql implementing null_provider.
  29. *
  30. * @copyright 2018 The Open University
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class provider implements
  34. // This plugin has data.
  35. \core_privacy\local\metadata\provider,
  36. // This plugin currently implements the original plugin\provider interface.
  37. \core_privacy\local\request\plugin\provider,
  38. \core_privacy\local\request\core_userlist_provider {
  39. /**
  40. * Returns meta data about this system.
  41. *
  42. * @param collection $items The initialised collection to add items to.
  43. * @return collection A listing of user data stored through this system.
  44. */
  45. public static function get_metadata(collection $items): collection {
  46. $items->add_database_table(
  47. 'report_customsql_queries',
  48. [
  49. 'displayname' => 'privacy:metadata:reportcustomsqlqueries:displayname',
  50. 'description' => 'privacy:metadata:reportcustomsqlqueries:description',
  51. 'descriptionformat' => 'privacy:metadata:reportcustomsqlqueries:descriptionformat',
  52. 'querysql' => 'privacy:metadata:reportcustomsqlqueries:querysql',
  53. 'queryparams' => 'privacy:metadata:reportcustomsqlqueries:queryparams',
  54. 'querylimit' => 'privacy:metadata:reportcustomsqlqueries:querylimit',
  55. 'capability' => 'privacy:metadata:reportcustomsqlqueries:capability',
  56. 'lastrun' => 'privacy:metadata:reportcustomsqlqueries:lastrun',
  57. 'lastexecutiontime' => 'privacy:metadata:reportcustomsqlqueries:lastexecutiontime',
  58. 'runable' => 'privacy:metadata:reportcustomsqlqueries:runable',
  59. 'singlerow' => 'privacy:metadata:reportcustomsqlqueries:singlerow',
  60. 'at' => 'privacy:metadata:reportcustomsqlqueries:at',
  61. 'emailto' => 'privacy:metadata:reportcustomsqlqueries:emailto',
  62. 'emailwhat' => 'privacy:metadata:reportcustomsqlqueries:emailwhat',
  63. 'categoryid' => 'privacy:metadata:reportcustomsqlqueries:categoryid',
  64. 'customdir' => 'privacy:metadata:reportcustomsqlqueries:customdir',
  65. 'usermodified' => 'privacy:metadata:reportcustomsqlqueries:usermodified',
  66. 'timecreated' => 'privacy:metadata:reportcustomsqlqueries:timecreated',
  67. 'timemodified' => 'privacy:metadata:reportcustomsqlqueries:timemodified'
  68. ],
  69. 'privacy:metadata:reportcustomsqlqueries'
  70. );
  71. return $items;
  72. }
  73. /**
  74. * This function gets the contexts containing data for a userid.
  75. *
  76. * @param int $userid The userid to get contexts for.
  77. * @return request\contextlist the context list for the user.
  78. */
  79. public static function get_contexts_for_userid(int $userid): request\contextlist {
  80. $contextlist = new request\contextlist();
  81. // The report is in context system.
  82. $contextlist->add_system_context();
  83. return $contextlist;
  84. }
  85. /**
  86. * This gets the list of users inside of the provided context. In this case, its only system context
  87. * which contains users.
  88. *
  89. * @param request\userlist $userlist
  90. * @return void
  91. */
  92. public static function get_users_in_context(request\userlist $userlist) {
  93. $context = $userlist->get_context();
  94. if ($context->contextlevel === CONTEXT_SYSTEM) {
  95. // If we are checking system context, we need to get all distinct usermodified from the table.
  96. $sql = 'SELECT DISTINCT usermodified
  97. FROM {report_customsql_queries}';
  98. $userlist->add_from_sql('usermodified', $sql, []);
  99. }
  100. }
  101. /**
  102. * Export all user data for the specified user, in the specified contexts.
  103. *
  104. * @param request\approved_contextlist $contextlist The approved contexts to export information for.
  105. * @throws coding_exception
  106. * @throws dml_exception
  107. * @throws \moodle_exception
  108. */
  109. public static function export_user_data(request\approved_contextlist $contextlist) {
  110. global $DB;
  111. $user = $contextlist->get_user();
  112. foreach ($contextlist as $context) {
  113. // We only export from system context.
  114. if ($context->contextlevel === CONTEXT_SYSTEM) {
  115. $records = $DB->get_records(
  116. 'report_customsql_queries',
  117. ['usermodified' => $user->id],
  118. 'displayname'
  119. );
  120. $exportdata = [];
  121. foreach ($records as $record) {
  122. $data = [];
  123. $data['displayname'] = $record->displayname;
  124. $data['description'] = $record->description;
  125. $data['descriptionformat'] = $record->descriptionformat;
  126. $data['querysql'] = $record->querysql;
  127. $data['queryparams'] = $record->queryparams;
  128. $data['querylimit'] = $record->querylimit;
  129. $data['capability'] = $record->capability;
  130. $data['lastrun'] = userdate($record->lastrun);
  131. $data['lastexecutiontime'] = $record->lastexecutiontime;
  132. $data['runable'] = $record->runable;
  133. $data['singlerow'] = $record->singlerow;
  134. $data['at'] = $record->at;
  135. $data['emailto'] = $record->emailto;
  136. $data['emailwhat'] = $record->emailwhat;
  137. $data['categoryid'] = $record->categoryid;
  138. $data['customdir'] = $record->customdir;
  139. $data['usermodified'] = self::you_or_somebody_else($record->usermodified, $user);
  140. $data['timecreated'] = userdate($record->timecreated);
  141. $data['timemodified'] = userdate($record->timemodified);
  142. $exportdata[] = $data;
  143. }
  144. $subcontext = [
  145. get_string('privacy:metadata:reportcustomsqlqueries', 'report_customsql')
  146. ];
  147. request\writer::with_context($context)->export_data($subcontext, (object)$exportdata);
  148. }
  149. }
  150. }
  151. /**
  152. * Delete all data for all users in the specified context.
  153. *
  154. * @param context $context The specific context to delete data for.
  155. * @throws \dml_exception
  156. */
  157. public static function delete_data_for_all_users_in_context(context $context) {
  158. global $DB;
  159. if ($context->contextlevel === CONTEXT_SYSTEM) {
  160. $adminuserid = get_admin()->id;
  161. $DB->set_field('report_customsql_queries', 'usermodified', $adminuserid);
  162. }
  163. }
  164. /**
  165. * Delete all user data for the specified user, in the specified contexts.
  166. *
  167. * @param request\approved_contextlist $contextlist The approved contexts and user information to delete information for.
  168. * @throws \dml_exception
  169. */
  170. public static function delete_data_for_user(request\approved_contextlist $contextlist) {
  171. global $DB;
  172. foreach ($contextlist as $context) {
  173. // We only delete data from system context.
  174. if ($context->contextlevel === CONTEXT_SYSTEM) {
  175. $userid = $contextlist->get_user()->id;
  176. $adminuserid = get_admin()->id;
  177. $DB->set_field('report_customsql_queries', 'usermodified',
  178. $adminuserid, ['usermodified' => $userid]);
  179. }
  180. }
  181. }
  182. /**
  183. * Delete multiple users within a single context.
  184. *
  185. * @param request\approved_userlist $userlist The approved context and user information to delete information for.
  186. *
  187. * @throws \dml_exception|\coding_exception
  188. */
  189. public static function delete_data_for_users(request\approved_userlist $userlist) {
  190. global $DB;
  191. $context = $userlist->get_context();
  192. if ($context->contextlevel === CONTEXT_SYSTEM) {
  193. $userids = $userlist->get_userids();
  194. list($sqlcondition, $params) = $DB->get_in_or_equal($userids);
  195. $adminuserid = get_admin()->id;
  196. $DB->set_field_select('report_customsql_queries', 'usermodified', $adminuserid,
  197. 'usermodified ' . $sqlcondition, $params);
  198. }
  199. }
  200. /**
  201. * Removes personally-identifiable data from a user id for export.
  202. *
  203. * @param int $userid User id of a person
  204. * @param \stdClass $user Object representing current user being considered
  205. * @return string 'You' if the two users match, 'Somebody else' otherwise
  206. * @throws \coding_exception
  207. */
  208. protected static function you_or_somebody_else($userid, $user) {
  209. if ($userid == $user->id) {
  210. return get_string('privacy_you', 'report_customsql');
  211. } else {
  212. return get_string('privacy_somebodyelse', 'report_customsql');
  213. }
  214. }
  215. }