2 // This file is part of Moodle - http://moodle.org/
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.
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 * LTI 1.3 page to create or delete deployments.
19 * This page is only used by LTI 1.3. Older versions do not require platforms to be registered with the tool during
23 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 use core\output\notification
;
28 use enrol_lti\local\ltiadvantage\form\deployment_form
;
29 use enrol_lti\local\ltiadvantage\repository\application_registration_repository
;
30 use enrol_lti\local\ltiadvantage\repository\context_repository
;
31 use enrol_lti\local\ltiadvantage\repository\deployment_repository
;
32 use enrol_lti\local\ltiadvantage\repository\resource_link_repository
;
33 use enrol_lti\local\ltiadvantage\repository\user_repository
;
34 use enrol_lti\local\ltiadvantage\service\tool_deployment_service
;
36 require_once(__DIR__
. '/../../config.php');
38 require_once($CFG->libdir
. '/adminlib.php');
39 require_once($CFG->dirroot
. '/enrol/lti/lib.php');
41 $action = required_param('action', PARAM_ALPHA
);
42 $registrationid = required_param('registrationid', PARAM_INT
);
43 if (!in_array($action, ['add', 'delete'])) {
44 throw new coding_exception("Invalid action param '$action'");
47 // The page to go back to when the respective action has been performed.
48 $deploymentslisturl = new moodle_url($CFG->wwwroot
. "/enrol/lti/register_platform.php",
49 ['regid' => $registrationid, 'action' => 'view', 'tabselect' => 'tooldeployments']);
51 // Local anon helper to extend the nav for this page and call admin_externalpage_setup.
52 $pagesetup = function(string $pagetitle) {
54 navigation_node
::override_active_url(
55 new moodle_url('/admin/settings.php', ['section' => 'enrolsettingslti_registrations'])
57 admin_externalpage_setup('enrolsettingslti_deployment_manage', '', null, '', ['pagelayout' => 'admin']);
58 $PAGE->navbar
->add($pagetitle);
61 // Local anon helper to map the formdata to the dto required for the domain layer.
62 $maptodto = function($formdata): stdClass
{
64 'registration_id' => $formdata->registrationid
,
65 'deployment_name' => $formdata->name
,
66 'deployment_id' => $formdata->deploymentid
,
70 if ($action === 'add') {
71 $pagesetup(get_string('deploymentadd', 'enrol_lti'));
73 $pageurl = new moodle_url('/enrol/lti/manage_deployment.php', ['action' => 'add']);
74 $mform = new deployment_form($pageurl->out(false));
75 if ($data = $mform->get_data()) {
76 $deploymentservice = new tool_deployment_service(new application_registration_repository(),
77 new deployment_repository(), new resource_link_repository(), new context_repository(),
78 new user_repository());
79 $deploymentservice->add_tool_deployment($maptodto($data));
80 redirect($deploymentslisturl, get_string('deploymentaddnotice', 'enrol_lti'), null,
81 notification
::NOTIFY_SUCCESS
);
82 } else if (!$mform->is_cancelled()) {
84 echo $OUTPUT->header();
85 echo $OUTPUT->heading(get_string('deploymentadd', 'enrol_lti'));
87 'registrationid' => $registrationid
90 echo $OUTPUT->footer();
93 redirect($deploymentslisturl);
95 } else if ($action === 'delete') {
96 $id = required_param('id', PARAM_INT
);
97 $pagesetup(get_string('deploymentdelete', 'enrol_lti'));
99 if (!optional_param('confirm', false, PARAM_BOOL
)) {
101 'action' => 'delete',
103 'registrationid' => $registrationid,
104 'sesskey' => sesskey(),
107 $continueurl = new moodle_url('/enrol/lti/manage_deployment.php', $continueparams);
108 $deploymentrepo = new deployment_repository();
109 $deployment = $deploymentrepo->find($id);
111 throw new coding_exception("Cannot delete non existent deployment '{$id}'.");
114 echo $OUTPUT->header();
115 echo $OUTPUT->confirm(
116 get_string('deploymentdeleteconfirm', 'enrol_lti', format_string($deployment->get_deploymentid())),
120 echo $OUTPUT->footer();
123 $deploymentservice = new tool_deployment_service(new application_registration_repository(),
124 new deployment_repository(), new resource_link_repository(), new context_repository(),
125 new user_repository());
126 $deploymentservice->delete_tool_deployment($id);
128 redirect($deploymentslisturl,
129 get_string('deploymentdeletenotice', 'enrol_lti'), null, notification
::NOTIFY_SUCCESS
);