Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
43 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 local_listcoursefiles;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
require_once($CFG->libdir . '/licenselib.php');
22
 
23
use coding_exception;
24
use dml_exception;
25
use html_writer;
26
use license_manager;
27
 
28
/**
29
 * Class licences
30
 * @package local_listcoursefiles
31
 * @copyright  2017 Martin Gauk (@innoCampus, TU Berlin)
32
 * @author Jeremy FitzPatrick
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class licences {
36
    /**
37
     * @var string[]|null
38
     */
39
    static protected ?array $licenses = null;
40
 
41
    /**
42
     * @var string[]|null
43
     */
44
    static protected ?array $licenscolors = null;
45
 
46
    /**
47
     * Returns an associative array of active licenses with short name keys and full name values.
48
     *
49
     * Caches the array after the first call.
50
     *
51
     * @return string[]
52
     * @throws coding_exception
53
     */
54
    public static function get_available_licenses(): array {
55
        if (is_null(self::$licenses)) {
56
            self::$licenses = license_manager::get_active_licenses();
57
            array_walk(
58
                self::$licenses,
59
                fn(object &$license, string $shortname) => $license = $license->fullname,
60
            );
61
        }
62
        return self::$licenses;
63
    }
64
 
65
    /**
66
     * Returns full license name wrapped in an HTML `span` with its configured background color.
67
     *
68
     * If no color was set for the specified license, the full license name is returned as is.
69
     * If no license with the specified `shortname` is available, an empty string is returned.
70
     *
71
     * Caches all configured license colors after the first call.
72
     *
73
     * @param string $licenseshort short name of a license
74
     * @return string full name of the license with HTML
75
     * @throws dml_exception|coding_exception
76
     */
77
    public static function get_license_name_color(string $licenseshort): string {
78
        if (is_null(self::$licenscolors)) {
79
            $colorscfg = get_config('local_listcoursefiles', 'licensecolors');
80
            preg_match_all('@\s*(\S+)\s*([a-fA-F0-9]{6})\s*@', $colorscfg, $matches, PREG_SET_ORDER);
81
            self::$licenscolors = array_combine(array_column($matches, 1), array_column($matches, 2));
82
        }
83
        $name = self::get_available_licenses()[$licenseshort] ?? '';
84
        if ($color = self::$licenscolors[$licenseshort] ?? null) {
85
            $name = html_writer::tag('span', $name, ['style' => "background-color: #$color"]);
86
        }
87
        return $name;
88
    }
89
}