MDL-62134 core_privacy: Allow for a failure handler
[moodle.git] / admin / tool / dataprivacy / classes / manager.php
blob16c46a68fe70020ada9a942ed6e87148d8b2221e
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/>.
16 /**
17 * Class \tool_dataprivacy\manager
19 * @package tool_dataprivacy
20 * @copyright 2018 Marina Glancy
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace tool_dataprivacy;
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Wrapper for \core_privacy\manager that sends notifications about exceptions to DPO
30 * @package tool_dataprivacy
31 * @copyright 2018 Marina Glancy
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class manager extends \core_privacy\manager {
36 /**
37 * Call the named method with the specified params on the supplied component if it implements the relevant interface on its provider.
39 * @param string $component The component to call
40 * @param string $interface The interface to implement
41 * @param string $methodname The method to call
42 * @param array $params The params to call
43 * @return mixed
45 public static function component_class_callback(string $component, string $interface, string $methodname, array $params) {
46 try {
47 return parent::component_class_callback($component, $interface, $methodname, $params);
48 } catch (\Throwable $e) {
49 debugging($e->getMessage(), DEBUG_DEVELOPER, $e->getTrace());
50 self::notify_dpo($e, $component, $interface, $methodname, $params);
52 return null;
55 /**
56 * Notifies all DPOs about exception occurred
58 * @param \Throwable $e
59 * @param string $component
60 * @param string $interface
61 * @param string $methodname
62 * @param array $params
63 * @return mixed
65 protected static function notify_dpo(\Throwable $e, string $component, string $interface, string $methodname, array $params) {
67 // Get the list of the site Data Protection Officers.
68 $dpos = api::get_site_dpos();
70 $messagesubject = get_string('exceptionnotificationsubject', 'tool_dataprivacy');
71 $a = (object)[
72 'fullmethodname' => static::get_provider_classname_for_component($component) . '::' . $methodname,
73 'component' => $component,
74 'message' => $e->getMessage(),
75 'backtrace' => $e->getTraceAsString()
77 $messagebody = get_string('exceptionnotificationbody', 'tool_dataprivacy', $a);
79 // Email the data request to the Data Protection Officer(s)/Admin(s).
80 foreach ($dpos as $dpo) {
81 $message = new \core\message\message();
82 $message->courseid = SITEID;
83 $message->component = 'tool_dataprivacy';
84 $message->name = 'notifyexceptions';
85 $message->userfrom = \core_user::get_noreply_user();
86 $message->subject = $messagesubject;
87 $message->fullmessageformat = FORMAT_HTML;
88 $message->notification = 1;
89 $message->userto = $dpo;
90 $message->fullmessagehtml = $messagebody;
91 $message->fullmessage = html_to_text($messagebody);
93 // Send message.
94 return message_send($message);