Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
// This file is part of Moodle - http://moodle.org/
2
//
3
// Moodle is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// Moodle is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU General Public License for more details.
12
//
13
// You should have received a copy of the GNU General Public License
14
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
 
16
import * as Components from '../../components.js';
17
 
18
const componentData = Components.fetchComponentData();
19
 
20
/**
21
 * The standard components shipped with core Moodle.
22
 *
23
 * @type {Object}
24
 */
25
export const standardComponents = componentData.standardComponents;
26
 
27
/**
28
 * All components of the current Moodle instance.
29
 *
30
 * @type {Object}
31
 */
32
export const allComponents = componentData.components;
33
 
34
/**
35
 * Get all components of the current Moodle instance.
36
 *
37
 * @returns {Array}
38
 */
39
export const getAllComponents = () => {
40
    let components = new Map(Object.entries(componentData.pluginTypes).map(([value, path]) => ([path,{
41
        path,
42
        value,
43
        name: `${value} (plugin type)`,
44
    }])));
45
 
46
    Object
47
        .entries(componentData.components)
48
        .filter(([path, value]) => Object.values(componentData.standardComponents).includes(value))
49
        .forEach(([path, value]) => {
50
            const entry = {
51
                path,
52
                value,
53
                name: value,
54
            };
55
            if (Object.values(componentData.subsystems).includes(value)) {
56
                if (components.has(path)) {
57
                    entry.name = `${value} (subsystem / plugintype)`;
58
                } else {
59
                    entry.name = `${value} (subsystem)`;
60
                }
61
            }
62
 
63
            components.set(path, entry);
64
        });
65
 
66
        return Array.from(components.values());
67
};
68
 
69
/**
70
 * Whether the specified component is a standard component shipped with core Moodle.
71
 *
72
 * @param {string} componentName
73
 * @returns {boolean}
74
 */
75
export const isStandardComponent = (componentName) => {
76
    if (Object.values(componentData.standardComponents).includes(componentName)) {
77
        return true;
78
    }
79
 
80
    if (Object.keys(componentData.pluginTypes).includes(componentName)) {
81
        return true;
82
    }
83
 
84
    return false;
85
};
86
 
87
export const rewritePlugintypeAsSubsystem = (componentName) => {
88
    if (Object.keys(componentData.pluginTypes).includes(componentName)) {
89
        const pluginTypePath = componentData.pluginTypes[componentName];
90
        if (Object.keys(componentData.subsystems).includes(pluginTypePath)) {
91
            return true;
92
        }
93
    }
94
 
95
    return false;
96
};
97
 
98
/**
99
 * Whether the specified component is a community component.
100
 *
101
 * @param {string} componentName
102
 * @returns {boolean}
103
 */
104
export const isCommunityComponent = (componentName) => {
105
    if (isStandardComponent(componentName)) {
106
        return false;
107
    }
108
 
109
    return Object.values(componentData.components).indexOf(componentName) !== -1;
110
};
111
 
112
/**
113
 * Sort method for components.
114
 *
115
 * This method sorts components putting `core` first, followed by core subsystems, then everything else.
116
 *
117
 * @param {String} a
118
 * @param {String} b
119
 * @returns {Number}
120
 */
121
export const sortComponents = (a, b) => {
122
    // Always put 'core' first.
123
    if (a === 'core') {
124
        return -1;
125
    } else if (b === 'core') {
126
        return 1;
127
    }
128
 
129
    // Put core subsystems next.
130
    if (a.startsWith('core_') && !b.startsWith('core_')) {
131
        return -1;
132
    } else if (b.startsWith('core_') && !a.startsWith('core_')) {
133
        return 1;
134
    }
135
 
136
    // Sort alphabetically for everything else.
137
    return a.localeCompare(b);
138
};