Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
/**
18
 * Helper class to fetch information about component grade items.
19
 *
20
 * @package   core_grades
21
 * @copyright Andrew Nicols <andrew@nicols.co.uk>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
declare(strict_types = 1);
26
 
27
namespace core_grades;
28
 
29
use code_grades\local\gradeitem\itemnumber_mapping;
30
use code_grades\local\gradeitem\advancedgrading_mapping;
31
 
32
/**
33
 * Helper class to fetch information about component grade items.
34
 *
35
 * @package   core_grades
36
 * @copyright Andrew Nicols <andrew@nicols.co.uk>
37
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class component_gradeitems {
40
 
41
    /**
42
     * Get the gradeitems classname for the specific component.
43
     *
44
     * @param string $component The component to fetch the classname for
45
     * @return string The composed classname
46
     */
47
    protected static function get_component_classname(string $component): string {
48
        return "{$component}\\grades\gradeitems";
49
    }
50
 
51
    /**
52
     * Get the grade itemnumber mapping for a component.
53
     *
54
     * @param string $component The component that the grade item belongs to
55
     * @return array
56
     */
57
    public static function get_itemname_mapping_for_component(string $component): array {
58
        $classname = "{$component}\\grades\gradeitems";
59
 
60
        if (!class_exists($classname)) {
61
            return [
62
 
63
            ];
64
        }
65
 
66
        if (!is_subclass_of($classname, 'core_grades\local\gradeitem\itemnumber_mapping')) {
67
            throw new \coding_exception("The {$classname} class does not implement " . itemnumber_mapping::class);
68
        }
69
 
70
        return $classname::get_itemname_mapping_for_component();
71
    }
72
 
73
    /**
74
     * Whether the named grading item exists.
75
     *
76
     * @param string $component
77
     * @param string $itemname
78
     * @return bool
79
     */
80
    public static function is_valid_itemname(string $component, string $itemname): bool {
81
        $items = self::get_itemname_mapping_for_component($component);
82
 
83
        return array_search($itemname, $items) !== false;
84
    }
85
 
86
    /**
87
     * Check whether the component class defines the advanced grading items.
88
     *
89
     * @param string $component The component to check
90
     * @return bool
91
     */
92
    public static function defines_advancedgrading_itemnames_for_component(string $component): bool {
93
        return is_subclass_of(self::get_component_classname($component), 'core_grades\local\gradeitem\advancedgrading_mapping');
94
    }
95
 
96
    /**
97
     * Get the list of advanced grading item names for the named component.
98
     *
99
     * @param string $component
100
     * @return array
101
     */
102
    public static function get_advancedgrading_itemnames_for_component(string $component): array {
103
        $classname = self::get_component_classname($component);
104
        if (!self::defines_advancedgrading_itemnames_for_component($component)) {
105
            throw new \coding_exception("The {$classname} class does not implement " . advancedgrading_mapping::class);
106
        }
107
 
108
        return $classname::get_advancedgrading_itemnames();
109
    }
110
 
111
    /**
112
     * Whether the named grading item name supports advanced grading.
113
     *
114
     * @param string $component
115
     * @param string $itemname
116
     * @return bool
117
     */
118
    public static function is_advancedgrading_itemname(string $component, string $itemname): bool {
119
        $gradingareas = self::get_advancedgrading_itemnames_for_component($component);
120
 
121
        return array_search($itemname, $gradingareas) !== false;
122
    }
123
 
124
    /**
125
     * Get the suffixed field name for an activity field mapped from its itemnumber.
126
     *
127
     * For legacy reasons, the first itemnumber has no suffix on field names.
128
     *
129
     * @param string $component The component that the grade item belongs to
130
     * @param int $itemnumber The grade itemnumber
131
     * @param string $fieldname The name of the field to be rewritten
132
     * @return string The translated field name
133
     */
134
    public static function get_field_name_for_itemnumber(string $component, int $itemnumber, string $fieldname): string {
135
 
136
        $classname = "{$component}\grades\gradeitems";
137
 
138
        if (class_exists($classname) && is_subclass_of($classname, 'core_grades\local\gradeitem\fieldname_mapping')) {
139
            $fieldname = $classname::get_field_name_for_itemnumber($component, $itemnumber, $fieldname);
140
        } else {
141
            $itemname = static::get_itemname_from_itemnumber($component, $itemnumber);
142
 
143
            if ($itemname) {
144
                $fieldname .= '_' . $itemname;
145
            }
146
        }
147
        return $fieldname;
148
 
149
    }
150
 
151
    /**
152
     * Get the suffixed field name for an activity field mapped from its itemnumber.
153
     *
154
     * For legacy reasons, the first itemnumber has no suffix on field names.
155
     *
156
     * @param string $component The component that the grade item belongs to
157
     * @param string $itemname The grade itemname
158
     * @param string $fieldname The name of the field to be rewritten
159
     * @return string The translated field name
160
     */
161
    public static function get_field_name_for_itemname(string $component, string $itemname, string $fieldname): string {
162
        if (empty($itemname)) {
163
            return $fieldname;
164
        }
165
 
166
        $itemnumber = static::get_itemnumber_from_itemname($component, $itemname);
167
 
168
        if ($itemnumber > 0) {
169
            return "{$fieldname}_{$itemname}";
170
        }
171
 
172
        return $fieldname;
173
    }
174
 
175
    /**
176
     * Get the itemname for an itemnumber.
177
     *
178
     * For legacy compatability when the itemnumber is 0, the itemname will always be empty.
179
     *
180
     * @param string $component The component that the grade item belongs to
181
     * @param int $itemnumber The grade itemnumber
182
     * @return int The grade itemnumber of the itemname
183
     */
184
    public static function get_itemname_from_itemnumber(string $component, int $itemnumber): string {
185
        if ($itemnumber === 0) {
186
            return '';
187
        }
188
 
189
        $mappings = self::get_itemname_mapping_for_component($component);
190
 
191
        if (isset($mappings[$itemnumber])) {
192
            return $mappings[$itemnumber];
193
        }
194
 
195
        if ($itemnumber >= 1000) {
196
            // An itemnumber >= 1000 belongs to an outcome.
197
            return '';
198
        }
199
 
200
        throw new \coding_exception("Unknown itemnumber mapping for {$itemnumber} in {$component}");
201
    }
202
 
203
    /**
204
     * Get the itemnumber for a item name.
205
     *
206
     * For legacy compatability when the itemname is empty, the itemnumber will always be 0.
207
     *
208
     * @param string $component The component that the grade item belongs to
209
     * @param string $itemname The grade itemname
210
     * @return int The grade itemname of the itemnumber
211
     */
212
    public static function get_itemnumber_from_itemname(string $component, string $itemname): int {
213
        if (empty($itemname)) {
214
            return 0;
215
        }
216
 
217
        $mappings = self::get_itemname_mapping_for_component($component);
218
 
219
        $flipped = array_flip($mappings);
220
        if (isset($flipped[$itemname])) {
221
            return $flipped[$itemname];
222
        }
223
 
224
        throw new \coding_exception("Unknown itemnumber mapping for {$itemname} in {$component}");
225
    }
226
}