MDL-75513 core_grades: New method that returns the grade item string

Introduces a new method get_element_type_string() in grade/lib.php
which returns the appropriate string that describes the type of the
grade item element.
This commit is contained in:
Mihail Geshoski 2022-10-31 12:17:21 +08:00
parent 3d1bb8e863
commit 458c1c771b

View file

@ -1494,6 +1494,48 @@ class grade_structure {
return $outputstr; return $outputstr;
} }
/**
* Returns the string that describes the type of the element.
*
* @param array $element An array representing an element in the grade_tree
* @return string The string that describes the type of the grade element
*/
public function get_element_type_string(array $element): string {
// If the element is a grade category.
if ($element['type'] == 'category') {
return get_string('category', 'grades');
}
// If the element is a grade item.
if (in_array($element['type'], ['item', 'courseitem', 'categoryitem'])) {
// If calculated grade item.
if ($element['object']->is_calculated()) {
return get_string('calculatedgrade', 'grades');
}
// If aggregated type grade item.
if ($element['object']->is_aggregate_item()) {
return get_string('aggregation', 'core_grades');
}
// If external grade item (module, plugin, etc.).
if ($element['object']->is_external_item()) {
// If outcome grade item.
if ($element['object']->is_outcome_item()) {
return get_string('outcome', 'grades');
}
return get_string('modulename', $element['object']->itemmodule);
}
// If manual grade item.
if ($element['object']->itemtype == 'manual') {
// If outcome grade item.
if ($element['object']->is_outcome_item()) {
return get_string('outcome', 'grades');
}
return get_string('manualitem', 'grades');
}
}
return '';
}
/** /**
* Returns name of element optionally with icon and link * Returns name of element optionally with icon and link
* *