3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * A namespace contains license specific functions
25 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') ||
die();
31 class license_manager
{
34 * License is a core license and can not be updated or deleted.
36 const CORE_LICENSE
= 0;
39 * License is a custom license and can be updated and/or deleted.
41 const CUSTOM_LICENSE
= 1;
44 * Integer representation of boolean for a license that is enabled.
46 const LICENSE_ENABLED
= 1;
49 * Integer representation of boolean for a license that is disabled.
51 const LICENSE_DISABLED
= 0;
54 * Integer for moving a license up order.
56 const LICENSE_MOVE_UP
= -1;
59 * Integer for moving a license down order.
61 const LICENSE_MOVE_DOWN
= 1;
64 * Save a license record.
66 * @param object $license {
67 * shortname => string a shortname of license, will be refered by files table[required]
68 * fullname => string the fullname of the license [required]
69 * source => string the homepage of the license type[required]
70 * enabled => int is it enabled?
71 * version => int a version number used by moodle [required]
74 static public function save($license) {
76 $existinglicense = self
::get_license_by_shortname($license->shortname
);
78 if (!empty($existinglicense)) {
79 $id = $existinglicense->id
;
80 if ($existinglicense->custom
== self
::CORE_LICENSE
) {
81 // Can only update the enabled status and sortorder for core licenses.
82 $existinglicense->enabled
= $license->enabled
;
83 $existinglicense->sortorder
= $license->sortorder
;
84 $license = $existinglicense;
87 self
::update($license);
89 self
::create($license);
96 * Adding a new license type
98 * @deprecated Since Moodle 3.9, MDL-45184.
99 * @todo MDL-67344 This will be deleted in Moodle 4.3.
100 * @see license_manager::save()
102 * @param object $license the license record to add.
104 * @return bool true on success.
106 public function add($license) : bool {
107 debugging('add() is deprecated. Please use license_manager::save() instead.', DEBUG_DEVELOPER
);
109 return self
::save($license);
113 * Create a license record.
115 * @param object $license the license to create record for.
117 static protected function create($license) {
120 $licensecount = count(self
::get_licenses());
121 $license->sortorder
= $licensecount +
1;
122 // Enable all created license by default.
123 $license->enabled
= self
::LICENSE_ENABLED
;
124 // API can only create custom licenses, core licenses
125 // are directly created at install or upgrade.
126 $license->custom
= self
::CUSTOM_LICENSE
;
128 $DB->insert_record('license', $license);
129 self
::reset_license_cache();
130 // Update the config setting of active licenses.
131 self
::set_active_licenses();
135 * Read licens record(s) from database.
137 * @param array $params license parameters to return licenses for.
139 * @return array $filteredlicenses object[] of licenses.
141 static public function read(array $params = []) {
142 $licenses = self
::get_licenses();
144 $filteredlicenses = [];
146 foreach ($licenses as $shortname => $license) {
148 foreach ($params as $key => $value) {
149 if ($license->$key != $value) {
150 $filtermatch = false;
154 $filteredlicenses[$shortname] = $license;
157 return $filteredlicenses;
162 * Update a license record.
164 * @param object $license the license to update record for.
166 * @throws \moodle_exception if attempting to update a core license.
168 static protected function update($license) {
171 $DB->update_record('license', $license);
172 self
::reset_license_cache();
176 * Delete a custom license.
178 * @param string $licenseshortname the shortname of license.
180 * @throws \moodle_exception when attempting to delete a license you are not allowed to.
182 static public function delete($licenseshortname) {
185 $licensetodelete = self
::get_license_by_shortname($licenseshortname);
187 if (!empty($licensetodelete)) {
188 if ($licensetodelete->custom
== self
::CUSTOM_LICENSE
) {
189 // Check that the license is not in use by any files, if so it cannot be deleted.
190 $countfilesusinglicense = $DB->count_records('files', ['license' => $licenseshortname]);
191 if ($countfilesusinglicense > 0) {
192 throw new moodle_exception('cannotdeletelicenseinuse', 'license');
194 $deletedsortorder = $licensetodelete->sortorder
;
195 $DB->delete_records('license', ['id' => $licensetodelete->id
]);
197 // We've deleted a license, so update our list of licenses so we don't save the deleted license again.
198 self
::reset_license_cache();
199 $licenses = self
::get_licenses();
201 foreach ($licenses as $license) {
202 if ($license->sortorder
> $deletedsortorder) {
203 $license->sortorder
= $license->sortorder
- 1;
204 self
::save($license);
208 // Update the config setting of active licenses as well.
209 self
::set_active_licenses();
212 throw new moodle_exception('cannotdeletecore', 'license');
215 throw new moodle_exception('licensenotfoundshortname', 'license', $licenseshortname);
220 * Get license records.
222 * @return array|false object[] of license records of false if none.
224 static public function get_licenses() {
227 $cache = \cache
::make('core', 'license');
228 $licenses = $cache->get('licenses');
230 if (empty($licenses)) {
232 $records = $DB->get_records_select('license', null, null, 'sortorder ASC');
233 foreach ($records as $license) {
234 // Interpret core license strings for internationalisation.
235 if ($license->custom
== self
::CORE_LICENSE
) {
236 $license->fullname
= get_string($license->shortname
, 'license');
238 $license->fullname
= format_string($license->fullname
);
240 $licenses[$license->shortname
] = $license;
242 $cache->set('licenses', $licenses);
249 * Change the sort order of a license (and it's sibling license as a result).
251 * @param int $direction value to change sortorder of license by.
252 * @param string $licenseshortname the shortname of license to changes sortorder for.
254 * @throws \moodle_exception if attempting to use invalid direction value.
256 static public function change_license_sortorder(int $direction, string $licenseshortname) : void
{
258 if ($direction != self
::LICENSE_MOVE_UP
&& $direction != self
::LICENSE_MOVE_DOWN
) {
259 throw new coding_exception(
260 'Must use a valid licence API move direction constant (LICENSE_MOVE_UP or LICENSE_MOVE_DOWN)');
263 $licenses = self
::get_licenses();
264 $licensetoupdate = $licenses[$licenseshortname];
266 $currentsortorder = $licensetoupdate->sortorder
;
267 $targetsortorder = $currentsortorder +
$direction;
269 if ($targetsortorder > 0 && $targetsortorder <= count($licenses) ) {
270 foreach ($licenses as $license) {
271 if ($license->sortorder
== $targetsortorder) {
272 $license->sortorder
= $license->sortorder
- $direction;
273 self
::update($license);
276 $licensetoupdate->sortorder
= $targetsortorder;
277 self
::update($licensetoupdate);
282 * Get license record by shortname
284 * @param string $name the shortname of license
285 * @return object|null the license or null if no license found.
287 static public function get_license_by_shortname(string $name) {
288 $licenses = self
::read(['shortname' => $name]);
290 if (!empty($licenses)) {
291 $license = reset($licenses);
301 * @param string $license the shortname of license
304 static public function enable($license) {
305 if ($license = self
::get_license_by_shortname($license)) {
306 $license->enabled
= self
::LICENSE_ENABLED
;
307 self
::update($license);
309 self
::set_active_licenses();
316 * @param string $license the shortname of license
319 static public function disable($license) {
321 // Site default license cannot be disabled!
322 if ($license == $CFG->sitedefaultlicense
) {
323 print_error('error');
325 if ($license = self
::get_license_by_shortname($license)) {
326 $license->enabled
= self
::LICENSE_DISABLED
;
327 self
::update($license);
329 self
::set_active_licenses();
335 * Store active licenses in global config.
337 static protected function set_active_licenses() {
338 $licenses = self
::read(['enabled' => self
::LICENSE_ENABLED
]);
340 foreach ($licenses as $l) {
341 $result[] = $l->shortname
;
343 set_config('licenses', implode(',', $result));
347 * Get the globally configured active licenses.
349 * @return array of license objects.
350 * @throws \coding_exception
352 static public function get_active_licenses() {
357 if (!empty($CFG->licenses
)) {
358 $activelicenses = explode(',', $CFG->licenses
);
359 $licenses = self
::get_licenses();
360 foreach ($licenses as $license) {
361 if (in_array($license->shortname
, $activelicenses)) {
362 $result[$license->shortname
] = $license;
371 * Get the globally configured active licenses as an array.
373 * @return array $licenses an associative array of licenses shaped as ['shortname' => 'fullname']
375 static public function get_active_licenses_as_array() {
376 $activelicenses = self
::get_active_licenses();
379 foreach ($activelicenses as $license) {
380 $licenses[$license->shortname
] = $license->fullname
;
387 * Install moodle built-in licenses.
389 static public function install_licenses() {
392 require_once($CFG->libdir
. '/db/upgradelib.php');
394 upgrade_core_licenses();
398 * Reset the license cache so it rebuilds next time licenses are fetched.
400 static public function reset_license_cache() {
401 $cache = \cache
::make('core', 'license');
402 $cache->delete('licenses');