Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
// This file is part of Moodle - http://moodle.org/
4
//
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.
9
//
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.
14
//
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/>.
17
 
18
 
19
/**
20
 * A namespace contains license specific functions
21
 *
22
 * @since      Moodle 2.0
23
 * @package    core
24
 * @subpackage lib
25
 * @copyright  2010 Dongsheng Cai <dongsheng@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
 
29
defined('MOODLE_INTERNAL') || die();
30
 
31
class license_manager {
32
 
33
    /**
34
     * License is a core license and can not be updated or deleted.
35
     */
36
    const CORE_LICENSE = 0;
37
 
38
    /**
39
     * License is a custom license and can be updated and/or deleted.
40
     */
41
    const CUSTOM_LICENSE = 1;
42
 
43
    /**
44
     * Integer representation of boolean for a license that is enabled.
45
     */
46
    const LICENSE_ENABLED = 1;
47
 
48
    /**
49
     * Integer representation of boolean for a license that is disabled.
50
     */
51
    const LICENSE_DISABLED = 0;
52
 
53
    /**
54
     * Integer for moving a license up order.
55
     */
56
    const LICENSE_MOVE_UP = -1;
57
 
58
    /**
59
     * Integer for moving a license down order.
60
     */
61
    const LICENSE_MOVE_DOWN = 1;
62
 
63
    /**
64
     * Save a license record.
65
     *
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]
72
     * }
73
     */
74
    public static function save($license) {
75
 
76
        $existinglicense = self::get_license_by_shortname($license->shortname);
77
 
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;
85
            }
86
            $license->id = $id;
87
            self::update($license);
88
        } else {
89
            self::create($license);
90
        }
91
 
92
        return true;
93
    }
94
 
95
    /**
96
     * Create a license record.
97
     *
98
     * @param object $license the license to create record for.
99
     */
100
    protected static function create($license) {
101
        global $DB;
102
 
103
        $licensecount = count(self::get_licenses());
104
        $license->sortorder = $licensecount + 1;
105
        // Enable all created license by default.
106
        $license->enabled = self::LICENSE_ENABLED;
107
        // API can only create custom licenses, core licenses
108
        // are directly created at install or upgrade.
109
        $license->custom = self::CUSTOM_LICENSE;
110
 
111
        $DB->insert_record('license', $license);
112
        self::reset_license_cache();
113
        // Update the config setting of active licenses.
114
        self::set_active_licenses();
115
    }
116
 
117
    /**
118
     * Read licens record(s) from database.
119
     *
120
     * @param array $params license parameters to return licenses for.
121
     *
122
     * @return array $filteredlicenses object[] of licenses.
123
     */
124
    public static function read(array $params = []) {
125
        $licenses = self::get_licenses();
126
 
127
        $filteredlicenses = [];
128
 
129
        foreach ($licenses as $shortname => $license) {
130
            $filtermatch = true;
131
            foreach ($params as $key => $value) {
132
                if ($license->$key != $value) {
133
                    $filtermatch = false;
134
                }
135
            }
136
            if ($filtermatch) {
137
                $filteredlicenses[$shortname] = $license;
138
            }
139
        }
140
        return $filteredlicenses;
141
 
142
    }
143
 
144
    /**
145
     * Update a license record.
146
     *
147
     * @param object $license the license to update record for.
148
     *
149
     * @throws \moodle_exception if attempting to update a core license.
150
     */
151
    protected static function update($license) {
152
        global $DB;
153
 
154
        $DB->update_record('license', $license);
155
        self::reset_license_cache();
156
    }
157
 
158
    /**
159
     * Delete a custom license.
160
     *
161
     * @param string $licenseshortname the shortname of license.
162
     *
163
     * @throws \moodle_exception when attempting to delete a license you are not allowed to.
164
     */
165
    public static function delete($licenseshortname) {
166
        global $DB;
167
 
168
        $licensetodelete = self::get_license_by_shortname($licenseshortname);
169
 
170
        if (!empty($licensetodelete)) {
171
            if ($licensetodelete->custom == self::CUSTOM_LICENSE) {
172
                // Check that the license is not in use by any non-draft files, if so it cannot be deleted.
173
                $countfilesselect = 'license = :license AND NOT (component = \'user\' AND filearea = \'draft\')';
174
                $countfilesusinglicense = $DB->count_records_select('files', $countfilesselect, ['license' => $licenseshortname]);
175
                if ($countfilesusinglicense > 0) {
176
                    throw new moodle_exception('cannotdeletelicenseinuse', 'license');
177
                }
178
                $deletedsortorder = $licensetodelete->sortorder;
179
                $DB->delete_records('license', ['id' => $licensetodelete->id]);
180
 
181
                // We've deleted a license, so update our list of licenses so we don't save the deleted license again.
182
                self::reset_license_cache();
183
                $licenses = self::get_licenses();
184
 
185
                foreach ($licenses as $license) {
186
                    if ($license->sortorder > $deletedsortorder) {
187
                        $license->sortorder = $license->sortorder - 1;
188
                        self::save($license);
189
                    }
190
                }
191
 
192
                // Update the config setting of active licenses as well.
193
                self::set_active_licenses();
194
 
195
            } else {
196
                throw new moodle_exception('cannotdeletecore', 'license');
197
            }
198
        } else {
199
            throw new moodle_exception('licensenotfoundshortname', 'license', $licenseshortname);
200
        }
201
    }
202
 
203
    /**
204
     * Get license records.
205
     *
206
     * @return array|false object[] of license records of false if none.
207
     */
208
    public static function get_licenses() {
209
        global $DB;
210
 
211
        $cache = \cache::make('core', 'license');
212
        $licenses = $cache->get('licenses');
213
 
214
        if ($licenses === false) {
215
            $licenses = [];
216
            $records = $DB->get_records_select('license', null, null, 'sortorder ASC');
217
            foreach ($records as $license) {
218
                $licenses[$license->shortname] = $license;
219
            }
220
            $cache->set('licenses', $licenses);
221
        }
222
 
223
        foreach ($licenses as $license) {
224
            // Localise the license names.
225
            if ($license->custom == self::CORE_LICENSE) {
226
                $license->fullname = get_string($license->shortname, 'core_license');
227
            } else {
228
                $license->fullname = format_string($license->fullname);
229
            }
230
        }
231
 
232
        return $licenses;
233
    }
234
 
235
    /**
236
     * Change the sort order of a license (and it's sibling license as a result).
237
     *
238
     * @param int $direction value to change sortorder of license by.
239
     * @param string $licenseshortname the shortname of license to changes sortorder for.
240
     *
241
     * @throws \moodle_exception if attempting to use invalid direction value.
242
     */
243
    public static function change_license_sortorder(int $direction, string $licenseshortname): void {
244
 
245
        if ($direction != self::LICENSE_MOVE_UP && $direction != self::LICENSE_MOVE_DOWN) {
246
            throw new coding_exception(
247
                'Must use a valid licence API move direction constant (LICENSE_MOVE_UP or LICENSE_MOVE_DOWN)');
248
        }
249
 
250
        $licenses = self::get_licenses();
251
        $licensetoupdate = $licenses[$licenseshortname];
252
 
253
        $currentsortorder = $licensetoupdate->sortorder;
254
        $targetsortorder = $currentsortorder + $direction;
255
 
256
        if ($targetsortorder > 0 && $targetsortorder <= count($licenses) ) {
257
            foreach ($licenses as $license) {
258
                if ($license->sortorder == $targetsortorder) {
259
                    $license->sortorder = $license->sortorder - $direction;
260
                    self::update($license);
261
                }
262
            }
263
            $licensetoupdate->sortorder = $targetsortorder;
264
            self::update($licensetoupdate);
265
        }
266
    }
267
 
268
    /**
269
     * Get license record by shortname
270
     *
271
     * @param string $name the shortname of license
272
     * @return object|null the license or null if no license found.
273
     */
274
    public static function get_license_by_shortname(string $name) {
275
        $licenses = self::read(['shortname' => $name]);
276
 
277
        if (!empty($licenses)) {
278
            $license = reset($licenses);
279
        } else {
280
            $license = null;
281
        }
282
 
283
        return $license;
284
    }
285
 
286
    /**
287
     * Enable a license
288
     * @param string $license the shortname of license
289
     * @return boolean
290
     */
291
    public static function enable($license) {
292
        if ($license = self::get_license_by_shortname($license)) {
293
            $license->enabled = self::LICENSE_ENABLED;
294
            self::update($license);
295
        }
296
        self::set_active_licenses();
297
 
298
        return true;
299
    }
300
 
301
    /**
302
     * Disable a license
303
     * @param string $license the shortname of license
304
     * @return boolean
305
     */
306
    public static function disable($license) {
307
        global $CFG;
308
        // Site default license cannot be disabled!
309
        if ($license == $CFG->sitedefaultlicense) {
310
            throw new \moodle_exception('error');
311
        }
312
        if ($license = self::get_license_by_shortname($license)) {
313
            $license->enabled = self::LICENSE_DISABLED;
314
            self::update($license);
315
        }
316
        self::set_active_licenses();
317
 
318
        return true;
319
    }
320
 
321
    /**
322
     * Store active licenses in global config.
323
     */
324
    protected static function set_active_licenses() {
325
        $licenses = self::read(['enabled' => self::LICENSE_ENABLED]);
326
        $result = array();
327
        foreach ($licenses as $l) {
328
            $result[] = $l->shortname;
329
        }
330
        set_config('licenses', implode(',', $result));
331
    }
332
 
333
    /**
334
     * Get the globally configured active licenses.
335
     *
336
     * @return array of license objects.
337
     * @throws \coding_exception
338
     */
339
    public static function get_active_licenses() {
340
        global $CFG;
341
 
342
        $result = [];
343
 
344
        if (!empty($CFG->licenses)) {
345
            $activelicenses = explode(',', $CFG->licenses);
346
            $licenses = self::get_licenses();
347
            foreach ($licenses as $license) {
348
                if (in_array($license->shortname, $activelicenses)) {
349
                    $result[$license->shortname] = $license;
350
                }
351
            }
352
        }
353
 
354
        return $result;
355
    }
356
 
357
    /**
358
     * Get the globally configured active licenses as an array.
359
     *
360
     * @return array $licenses an associative array of licenses shaped as ['shortname' => 'fullname']
361
     */
362
    public static function get_active_licenses_as_array() {
363
        $activelicenses = self::get_active_licenses();
364
 
365
        $licenses = [];
366
        foreach ($activelicenses as $license) {
367
            $licenses[$license->shortname] = $license->fullname;
368
        }
369
 
370
        return $licenses;
371
    }
372
 
373
    /**
374
     * Install moodle built-in licenses.
375
     */
376
    public static function install_licenses() {
377
        global $CFG;
378
 
379
        require_once($CFG->libdir . '/db/upgradelib.php');
380
 
381
        upgrade_core_licenses();
382
    }
383
 
384
    /**
385
     * Reset the license cache so it rebuilds next time licenses are fetched.
386
     */
387
    public static function reset_license_cache() {
388
        $cache = \cache::make('core', 'license');
389
        $cache->delete('licenses');
390
    }
391
}