MDL-62134 tool_dataprivacy: Add a manager_observer
[moodle.git] / admin / tool / dataprivacy / classes / metadata_registry.php
blob6282cc4129108837a537ad3036570b643af638a2
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.
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/>.
17 /**
18 * Class containing helper methods for processing data requests.
20 * @package tool_dataprivacy
21 * @copyright 2018 Adrian Greeve
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy;
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Class containing helper methods for processing data requests.
31 * @copyright 2018 Adrian Greeve
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class metadata_registry {
36 /**
37 * Returns plugin types / plugins and the user data that it stores in a format that can be sent to a template.
39 * @return array An array with all of the plugin types / plugins and the user data they store.
41 public function get_registry_metadata() {
42 $manager = new \core_privacy\manager();
43 $manager->set_observer(new \tool_dataprivacy\manager_observer());
45 $pluginman = \core_plugin_manager::instance();
46 $contributedplugins = $this->get_contrib_list();
47 $metadata = $manager->get_metadata_for_components();
48 $fullyrichtree = $this->get_full_component_list();
49 foreach ($fullyrichtree as $branch => $leaves) {
50 $plugintype = $leaves['plugin_type'];
51 $plugins = array_map(function($component) use ($manager, $metadata, $contributedplugins, $plugintype, $pluginman) {
52 // Use the plugin name for the plugins, ignore for core subsystems.
53 $internaldata = ($plugintype == 'core') ? ['component' => $component] :
54 ['component' => $pluginman->plugin_name($component)];
55 $internaldata['raw_component'] = $component;
56 if ($manager->component_is_compliant($component)) {
57 $internaldata['compliant'] = true;
58 if (isset($metadata[$component])) {
59 $collection = $metadata[$component]->get_collection();
60 $internaldata = $this->format_metadata($collection, $component, $internaldata);
61 } else if ($manager->is_empty_subsystem($component)) {
62 // This is an unused subsystem.
63 // Use the generic string.
64 $internaldata['nullprovider'] = get_string('privacy:subsystem:empty', 'core_privacy');
65 } else {
66 // Call get_reason for null provider.
67 $internaldata['nullprovider'] = get_string($manager->get_null_provider_reason($component), $component);
69 } else {
70 $internaldata['compliant'] = false;
72 // Check to see if we are an external plugin.
73 $componentshortname = explode('_', $component);
74 $shortname = array_pop($componentshortname);
75 if (isset($contributedplugins[$plugintype][$shortname])) {
76 $internaldata['external'] = true;
78 return $internaldata;
79 }, $leaves['plugins']);
80 $fullyrichtree[$branch]['plugin_type_raw'] = $plugintype;
81 // We're done using the plugin type. Convert it to a readable string.
82 $fullyrichtree[$branch]['plugin_type'] = $pluginman->plugintype_name($plugintype);
83 $fullyrichtree[$branch]['plugins'] = $plugins;
85 return $fullyrichtree;
88 /**
89 * Formats the metadata for use with a template.
91 * @param array $collection The collection associated with the component that we want to expand and format.
92 * @param string $component The component that we are dealing in
93 * @param array $internaldata The array to add the formatted metadata to.
94 * @return array The internal data array with the formatted metadata.
96 protected function format_metadata($collection, $component, $internaldata) {
97 foreach ($collection as $collectioninfo) {
98 $privacyfields = $collectioninfo->get_privacy_fields();
99 $fields = '';
100 if (!empty($privacyfields)) {
101 $fields = array_map(function($key, $field) use ($component) {
102 return [
103 'field_name' => $key,
104 'field_summary' => get_string($field, $component)
106 }, array_keys($privacyfields), $privacyfields);
108 // Can the metadata types be located somewhere else besides core?
109 $items = explode('\\', get_class($collectioninfo));
110 $type = array_pop($items);
111 $typedata = [
112 'name' => $collectioninfo->get_name(),
113 'type' => $type,
114 'fields' => $fields,
115 'summary' => get_string($collectioninfo->get_summary(), $component)
117 if (strpos($type, 'subsystem_link') === 0 || strpos($type, 'plugintype_link') === 0) {
118 $typedata['link'] = true;
120 $internaldata['metadata'][] = $typedata;
122 return $internaldata;
126 * Return the full list of components.
128 * @return array An array of plugin types which contain plugin data.
130 protected function get_full_component_list() {
131 global $CFG;
133 $list = \core_component::get_component_list();
134 $list['core']['core'] = "{$CFG->dirroot}/lib";
135 $formattedlist = [];
136 foreach ($list as $plugintype => $plugin) {
137 $formattedlist[] = ['plugin_type' => $plugintype, 'plugins' => array_keys($plugin)];
140 return $formattedlist;
144 * Returns a list of contributed plugins installed on the system.
146 * @return array A list of contributed plugins installed.
148 protected function get_contrib_list() {
149 return array_map(function($plugins) {
150 return array_filter($plugins, function($plugindata) {
151 return !$plugindata->is_standard();
153 }, \core_plugin_manager::instance()->get_plugins());