Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / enrol / lti / manage_deployment.php
blob1c259d4ac47ff8c968863624fe62c278e82ab5e7
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 * 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
20 * registration.
22 * @package enrol_lti
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');
37 global $CFG, $OUTPUT;
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) {
53 global $PAGE;
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 {
63 return (object) [
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'));
86 $mform->set_data([
87 'registrationid' => $registrationid
88 ]);
89 $mform->display();
90 echo $OUTPUT->footer();
91 die();
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)) {
100 $continueparams = [
101 'action' => 'delete',
102 'id' => $id,
103 'registrationid' => $registrationid,
104 'sesskey' => sesskey(),
105 'confirm' => true
107 $continueurl = new moodle_url('/enrol/lti/manage_deployment.php', $continueparams);
108 $deploymentrepo = new deployment_repository();
109 $deployment = $deploymentrepo->find($id);
110 if (!$deployment) {
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())),
117 $continueurl,
118 $deploymentslisturl
120 echo $OUTPUT->footer();
121 } else {
122 require_sesskey();
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);