MDL-56595 tool_usertours: set page params when configuring tours/steps.
[moodle.git] / admin / tool / dataprivacy / editpurpose.php
CommitLineData
5efc1f9e
DM
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 * This page lets users manage purposes.
19 *
20 * @package tool_dataprivacy
21 * @copyright 2018 David Monllao
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once(__DIR__ . '/../../../config.php');
26
dd26f1be
JP
27require_login(null, false);
28
5efc1f9e
DM
29$id = optional_param('id', 0, PARAM_INT);
30
31$url = new \moodle_url('/admin/tool/dataprivacy/editpurpose.php', array('id' => $id));
32if ($id) {
33 $title = get_string('editpurpose', 'tool_dataprivacy');
34} else {
35 $title = get_string('addpurpose', 'tool_dataprivacy');
36}
37
38\tool_dataprivacy\page_helper::setup($url, $title, 'dataregistry');
39
40$purpose = new \tool_dataprivacy\purpose($id);
41$form = new \tool_dataprivacy\form\purpose($PAGE->url->out(false),
42 array('persistent' => $purpose, 'showbuttons' => true));
43
44$returnurl = new \moodle_url('/admin/tool/dataprivacy/purposes.php');
45if ($form->is_cancelled()) {
46 redirect($returnurl);
bf347c4d
AN
47} else if ($alldata = $form->get_data()) {
48 $data = $form->filter_data_for_persistent($alldata);
49
5efc1f9e 50 if (empty($data->id)) {
bf347c4d 51 $purpose = \tool_dataprivacy\api::create_purpose($data);
5efc1f9e
DM
52 $messagesuccess = get_string('purposecreated', 'tool_dataprivacy');
53 } else {
bf347c4d 54 $purpose = \tool_dataprivacy\api::update_purpose($data);
5efc1f9e
DM
55 $messagesuccess = get_string('purposeupdated', 'tool_dataprivacy');
56 }
bf347c4d
AN
57
58 $currentoverrides = [];
59 foreach ($purpose->get_purpose_overrides() as $override) {
60 $currentoverrides[$override->get('id')] = $override;
61 }
62
63 $overrides = $form->get_role_overrides_from_data($alldata);
64 $submittedoverrides = [];
65 $tosave = [];
66
67 foreach ($overrides as $overridedata) {
68 $overridedata->purposeid = $purpose->get('id');
69 $override = new \tool_dataprivacy\purpose_override($overridedata->id, $overridedata);
70
71 $tosave[] = $override;
72
73 if (!empty($overridedata->id)) {
74 $submittedoverrides[$overridedata->id] = true;
75 }
76 }
77
78 foreach ($currentoverrides as $id => $override) {
79 if (!isset($submittedoverrides[$id])) {
80 $override->delete();
81 }
82 }
83
84 foreach ($tosave as $override) {
85 $override->save();
86 }
87
5efc1f9e
DM
88 redirect($returnurl, $messagesuccess, 0, \core\output\notification::NOTIFY_SUCCESS);
89}
90
91$output = $PAGE->get_renderer('tool_dataprivacy');
92echo $output->header();
93$form->display();
94echo $output->footer();