-
Notifications
You must be signed in to change notification settings - Fork 10
/
edit.php
128 lines (105 loc) · 4.38 KB
/
edit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Dataflow settings.
*
* @package tool_dataflows
* @author Kevin Pham <[email protected]>
* @copyright Catalyst IT, 2022
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_dataflows\dataflow;
use tool_dataflows\form\dataflow_form;
use tool_dataflows\visualiser;
require_once(dirname(__FILE__) . '/../../../config.php');
require_once($CFG->libdir . '/adminlib.php');
defined('MOODLE_INTERNAL') || die();
// The dataflow id, if not provided, it is as if the user is creating a new dataflow.
$id = optional_param('id', 0, PARAM_INT);
require_login();
$overviewurl = new moodle_url("/admin/tool/dataflows/index.php");
$url = new moodle_url("/admin/tool/dataflows/edit.php", ['id' => $id]);
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL);
$context = context_system::instance();
// Check capabilities and setup page.
require_capability('tool/dataflows:managedataflows', $context);
// Set the PAGE URL (and mandatory context). Note the ID being recorded, this is important.
$PAGE->set_context($context);
$PAGE->set_url($url);
$persistent = null;
if (!empty($id)) {
$persistent = new dataflow($id);
}
// Render the specific dataflow form.
$customdata = [
'persistent' => $persistent ?? null, // An instance, or null.
'userid' => $USER->id, // For the hidden userid field.
'concurrencyallowed' => $persistent ? $persistent->is_concurrency_supported() : true,
];
$form = new dataflow_form($PAGE->url->out(false), $customdata);
if ($form->is_cancelled()) {
redirect($returnurl ?? $overviewurl);
}
if (($data = $form->get_data())) {
try {
if (empty($data->id)) {
// If we don't have an ID, we know that we must create a new record.
// Call your API to create a new persistent from this data.
// Or, do the following if you don't want capability checks (discouraged).
$persistent = new dataflow(0, $data);
$persistent->create();
} else {
// Reset the config hash so it can be recalculated with the next run.
$data->confighash = '';
// We had an ID, this means that we are going to update a record.
// Call your API to update the persistent from the data.
// Or, do the following if you don't want capability checks (discouraged).
$persistent->from_record($data);
$persistent->update();
}
\core\notification::success(get_string('changessaved'));
} catch (Exception $e) {
\core\notification::error($e->getMessage());
}
// We are done, so let's redirect somewhere.
redirect($returnurl ?? $overviewurl);
}
// Display the mandatory header and footer.
$crumbs = [[get_string('pluginmanage', 'tool_dataflows'), new moodle_url('/admin/tool/dataflows/index.php')]];
$heading = get_string('new_dataflow', 'tool_dataflows');
if (isset($persistent)) {
$heading = get_string('update_dataflow', 'tool_dataflows');
$crumbs[] = [$persistent->name, new moodle_url('/admin/tool/dataflows/view.php', ['id' => $persistent->id])];
}
$crumbs[] = [$heading, $url];
// Configure the breadcrumb navigation.
// New - Dataflows > Manage Flows > New dataflow.
// Update - Dataflows > Manage Flows > $dataflow->name > Update dataflow.
visualiser::breadcrumb_navigation($crumbs);
$title = implode(': ', array_filter([
get_string('pluginname', 'tool_dataflows'),
$heading,
$persistent->name ?? '',
]));
$PAGE->set_title($title);
$PAGE->set_heading(get_string('pluginname', 'tool_dataflows'));
echo $OUTPUT->header();
// Output headings.
echo $OUTPUT->heading($heading);
// And display the form, and its validation errors if there are any.
$form->display();
// Display footer.
echo $OUTPUT->footer();