2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Helper class to fetch information about component grade items.
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
25 declare(strict_types = 1);
27 namespace core_grades;
29 use code_grades\local\gradeitem\itemnumber_mapping;
30 use code_grades\local\gradeitem\advancedgrading_mapping;
33 * Helper class to fetch information about component grade items.
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
39 class component_gradeitems {
42 * Get the gradeitems classname for the specific component.
44 * @param string $component The component to fetch the classname for
45 * @return string The composed classname
47 protected static function get_component_classname(string $component): string {
48 return "{$component}\\grades\gradeitems";
52 * Get the grade itemnumber mapping for a component.
54 * @param string $component The component that the grade item belongs to
57 public static function get_itemname_mapping_for_component(string $component): array {
58 $classname = "{$component}\\grades\gradeitems";
60 if (!class_exists($classname)) {
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);
70 return $classname::get_itemname_mapping_for_component();
74 * Whether the named grading item exists.
76 * @param string $component
77 * @param string $itemname
80 public static function is_valid_itemname(string $component, string $itemname): bool {
81 $items = self::get_itemname_mapping_for_component($component);
83 return array_search($itemname, $items) !== false;
87 * Check whether the component class defines the advanced grading items.
89 * @param string $component The component to check
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');
97 * Get the list of advanced grading item names for the named component.
99 * @param string $component
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);
108 return $classname::get_advancedgrading_itemnames();
112 * Whether the named grading item name supports advanced grading.
114 * @param string $component
115 * @param string $itemname
118 public static function is_advancedgrading_itemname(string $component, string $itemname): bool {
119 $gradingareas = self::get_advancedgrading_itemnames_for_component($component);
121 return array_search($itemname, $gradingareas) !== false;
125 * Get the suffixed field name for an activity field mapped from its itemnumber.
127 * For legacy reasons, the first itemnumber has no suffix on field names.
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
134 public static function get_field_name_for_itemnumber(string $component, int $itemnumber, string $fieldname): string {
135 $itemname = static::get_itemname_from_itemnumber($component, $itemnumber);
138 return "{$fieldname}_{$itemname}";
145 * Get the suffixed field name for an activity field mapped from its itemnumber.
147 * For legacy reasons, the first itemnumber has no suffix on field names.
149 * @param string $component The component that the grade item belongs to
150 * @param string $itemname The grade itemname
151 * @param string $fieldname The name of the field to be rewritten
152 * @return string The translated field name
154 public static function get_field_name_for_itemname(string $component, string $itemname, string $fieldname): string {
155 if (empty($itemname)) {
159 $itemnumber = static::get_itemnumber_from_itemname($component, $itemname);
161 if ($itemnumber > 0) {
162 return "{$fieldname}_{$itemname}";
169 * Get the itemname for an itemnumber.
171 * For legacy compatability when the itemnumber is 0, the itemname will always be empty.
173 * @param string $component The component that the grade item belongs to
174 * @param int $itemnumber The grade itemnumber
175 * @return int The grade itemnumber of the itemname
177 public static function get_itemname_from_itemnumber(string $component, int $itemnumber): string {
178 if ($itemnumber === 0) {
182 $mappings = self::get_itemname_mapping_for_component($component);
184 if (isset($mappings[$itemnumber])) {
185 return $mappings[$itemnumber];
188 if ($itemnumber >= 1000) {
189 // An itemnumber >= 1000 belongs to an outcome.
193 throw new \coding_exception("Unknown itemnumber mapping for {$itemnumber} in {$component}");
197 * Get the itemnumber for a item name.
199 * For legacy compatability when the itemname is empty, the itemnumber will always be 0.
201 * @param string $component The component that the grade item belongs to
202 * @param string $itemname The grade itemname
203 * @return int The grade itemname of the itemnumber
205 public static function get_itemnumber_from_itemname(string $component, string $itemname): int {
206 if (empty($itemname)) {
210 $mappings = self::get_itemname_mapping_for_component($component);
212 $flipped = array_flip($mappings);
213 if (isset($flipped[$itemname])) {
214 return $flipped[$itemname];
217 throw new \coding_exception("Unknown itemnumber mapping for {$itemname} in {$component}");