1 |
efrain |
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 |
namespace tool_licensemanager;
|
|
|
18 |
|
|
|
19 |
use tool_licensemanager\form\edit_license;
|
|
|
20 |
use license_manager;
|
|
|
21 |
use stdClass;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* License manager, main controller for tool_licensemanager.
|
|
|
25 |
*
|
|
|
26 |
* @package tool_licensemanager
|
|
|
27 |
* @copyright 2019 Tom Dickman <tomdickman@catalyst-au.net>
|
|
|
28 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
29 |
*/
|
|
|
30 |
class manager {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Action for creating a new custom license.
|
|
|
34 |
*/
|
|
|
35 |
const ACTION_CREATE = 'create';
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Action for updating a custom license's details.
|
|
|
39 |
*/
|
|
|
40 |
const ACTION_UPDATE = 'update';
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Action for deleting a custom license.
|
|
|
44 |
*/
|
|
|
45 |
const ACTION_DELETE = 'delete';
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Action for disabling a custom license.
|
|
|
49 |
*/
|
|
|
50 |
const ACTION_DISABLE = 'disable';
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Action for enabling a custom license.
|
|
|
54 |
*/
|
|
|
55 |
const ACTION_ENABLE = 'enable';
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Action for displaying the license list view.
|
|
|
59 |
*/
|
|
|
60 |
const ACTION_VIEW_LICENSE_MANAGER = 'viewlicensemanager';
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Action for moving a license up order.
|
|
|
64 |
*/
|
|
|
65 |
const ACTION_MOVE_UP = 'moveup';
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Action for moving a license down order.
|
|
|
69 |
*/
|
|
|
70 |
const ACTION_MOVE_DOWN = 'movedown';
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Entry point for internal license manager.
|
|
|
74 |
*
|
|
|
75 |
* @param string $action the api action to carry out.
|
|
|
76 |
* @param string|object $license the license object or shortname of license to carry action out on.
|
|
|
77 |
*/
|
|
|
78 |
public function execute(string $action, $license): void {
|
|
|
79 |
|
|
|
80 |
admin_externalpage_setup('licensemanager');
|
|
|
81 |
|
|
|
82 |
// Convert license to a string if it's a full license object.
|
|
|
83 |
if (is_object($license)) {
|
|
|
84 |
$license = $license->shortname;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
$viewmanager = true;
|
|
|
88 |
$message = null;
|
|
|
89 |
$redirect = helper::get_licensemanager_url();
|
|
|
90 |
|
|
|
91 |
switch ($action) {
|
|
|
92 |
case self::ACTION_DISABLE:
|
|
|
93 |
require_sesskey();
|
|
|
94 |
license_manager::disable($license);
|
|
|
95 |
redirect($redirect);
|
|
|
96 |
break;
|
|
|
97 |
|
|
|
98 |
case self::ACTION_ENABLE:
|
|
|
99 |
require_sesskey();
|
|
|
100 |
license_manager::enable($license);
|
|
|
101 |
redirect($redirect);
|
|
|
102 |
break;
|
|
|
103 |
|
|
|
104 |
case self::ACTION_DELETE:
|
|
|
105 |
require_sesskey();
|
|
|
106 |
try {
|
|
|
107 |
license_manager::delete($license);
|
|
|
108 |
} catch (\moodle_exception $e) {
|
|
|
109 |
$message = $e->getMessage();
|
|
|
110 |
}
|
|
|
111 |
redirect($redirect, $message);
|
|
|
112 |
break;
|
|
|
113 |
|
|
|
114 |
case self::ACTION_CREATE:
|
|
|
115 |
case self::ACTION_UPDATE:
|
|
|
116 |
$viewmanager = $this->edit($action, $license);
|
|
|
117 |
break;
|
|
|
118 |
|
|
|
119 |
case self::ACTION_MOVE_UP:
|
|
|
120 |
case self::ACTION_MOVE_DOWN:
|
|
|
121 |
require_sesskey();
|
|
|
122 |
$this->change_license_order($action, $license);
|
|
|
123 |
redirect($redirect);
|
|
|
124 |
break;
|
|
|
125 |
|
|
|
126 |
case self::ACTION_VIEW_LICENSE_MANAGER:
|
|
|
127 |
default:
|
|
|
128 |
break;
|
|
|
129 |
}
|
|
|
130 |
if ($viewmanager) {
|
|
|
131 |
$this->view_license_manager($message);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Edit an existing license or create a new license.
|
|
|
137 |
*
|
|
|
138 |
* @param string $action the form action to carry out.
|
|
|
139 |
* @param string $licenseshortname the shortname of the license to edit.
|
|
|
140 |
*
|
|
|
141 |
* @return bool true if license editing complete, false otherwise.
|
|
|
142 |
*/
|
|
|
143 |
private function edit(string $action, string $licenseshortname): bool {
|
|
|
144 |
|
|
|
145 |
if ($action != self::ACTION_CREATE && $action != self::ACTION_UPDATE) {
|
|
|
146 |
throw new \coding_exception('license edit actions are limited to create and update');
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$form = new form\edit_license($action, $licenseshortname);
|
|
|
150 |
|
|
|
151 |
if ($form->is_cancelled()) {
|
|
|
152 |
return true;
|
|
|
153 |
} else if ($data = $form->get_data()) {
|
|
|
154 |
|
|
|
155 |
$license = new stdClass();
|
|
|
156 |
if ($action == self::ACTION_CREATE) {
|
|
|
157 |
// Check that license shortname isn't already in use.
|
|
|
158 |
if (!empty(license_manager::get_license_by_shortname($data->shortname))) {
|
|
|
159 |
throw new \moodle_exception('duplicatelicenseshortname', 'tool_licensemanager',
|
|
|
160 |
helper::get_licensemanager_url(),
|
|
|
161 |
$data->shortname);
|
|
|
162 |
}
|
|
|
163 |
$license->shortname = $data->shortname;
|
|
|
164 |
} else {
|
|
|
165 |
if (empty(license_manager::get_license_by_shortname($licenseshortname))) {
|
|
|
166 |
throw new \moodle_exception('licensenotfoundshortname', 'license',
|
|
|
167 |
helper::get_licensemanager_url(),
|
|
|
168 |
$licenseshortname);
|
|
|
169 |
}
|
|
|
170 |
$license->shortname = $licenseshortname;
|
|
|
171 |
}
|
|
|
172 |
$license->fullname = $data->fullname;
|
|
|
173 |
$license->source = $data->source;
|
|
|
174 |
// Legacy date format maintained to prevent breaking on upgrade.
|
|
|
175 |
$license->version = date('Ymd', $data->version) . '00';
|
|
|
176 |
|
|
|
177 |
license_manager::save($license);
|
|
|
178 |
|
|
|
179 |
return true;
|
|
|
180 |
} else {
|
|
|
181 |
$this->view_license_editor($action, $licenseshortname, $form);
|
|
|
182 |
|
|
|
183 |
return false;
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Change license order by moving up or down license order.
|
|
|
189 |
*
|
|
|
190 |
* @param string $direction which direction to move, up or down.
|
|
|
191 |
* @param string $licenseshortname the shortname of the license to move up or down order.
|
|
|
192 |
*/
|
|
|
193 |
private function change_license_order(string $direction, string $licenseshortname): void {
|
|
|
194 |
|
|
|
195 |
if (!empty($licenseshortname)) {
|
|
|
196 |
if ($direction == self::ACTION_MOVE_UP) {
|
|
|
197 |
license_manager::change_license_sortorder(license_manager::LICENSE_MOVE_UP, $licenseshortname);
|
|
|
198 |
} else if ($direction == self::ACTION_MOVE_DOWN) {
|
|
|
199 |
license_manager::change_license_sortorder(license_manager::LICENSE_MOVE_DOWN, $licenseshortname);
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/**
|
|
|
205 |
* View the license editor to create or edit a license.
|
|
|
206 |
*
|
|
|
207 |
* @param string $action
|
|
|
208 |
* @param string $licenseshortname the shortname of the license to create/edit.
|
|
|
209 |
* @param \tool_licensemanager\form\edit_license $form the form for submitting edit data.
|
|
|
210 |
*/
|
|
|
211 |
private function view_license_editor(string $action, string $licenseshortname, edit_license $form): void {
|
|
|
212 |
global $PAGE;
|
|
|
213 |
|
|
|
214 |
$renderer = $PAGE->get_renderer('tool_licensemanager');
|
|
|
215 |
|
|
|
216 |
if ($action == self::ACTION_UPDATE && $license = license_manager::get_license_by_shortname($licenseshortname)) {
|
|
|
217 |
$return = $renderer->render_edit_licence_headers($licenseshortname);
|
|
|
218 |
|
|
|
219 |
$form->set_data(['shortname' => $license->shortname]);
|
|
|
220 |
$form->set_data(['fullname' => $license->fullname]);
|
|
|
221 |
$form->set_data(['source' => $license->source]);
|
|
|
222 |
$form->set_data(['version' => helper::convert_version_to_epoch($license->version)]);
|
|
|
223 |
|
|
|
224 |
} else {
|
|
|
225 |
$return = $renderer->render_create_licence_headers();
|
|
|
226 |
}
|
|
|
227 |
$return .= $form->render();
|
|
|
228 |
$return .= $renderer->footer();
|
|
|
229 |
|
|
|
230 |
echo $return;
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
/**
|
|
|
234 |
* View the license manager.
|
|
|
235 |
*/
|
|
|
236 |
private function view_license_manager(string $message = null): void {
|
|
|
237 |
global $PAGE, $OUTPUT;
|
|
|
238 |
|
|
|
239 |
$renderer = $PAGE->get_renderer('tool_licensemanager');
|
|
|
240 |
$html = $renderer->header();
|
|
|
241 |
$html .= $renderer->heading(get_string('licensemanager', 'tool_licensemanager'));
|
|
|
242 |
|
|
|
243 |
if (!empty($message)) {
|
|
|
244 |
$html .= $OUTPUT->notification($message);
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
$table = new \tool_licensemanager\output\table();
|
|
|
248 |
$html .= $renderer->render($table);
|
|
|
249 |
$html .= $renderer->footer();
|
|
|
250 |
|
|
|
251 |
echo $html;
|
|
|
252 |
}
|
|
|
253 |
}
|