Refactor element_helper::get_grade_items() to be nicer to read

This commit is contained in:
Mark Nelson 2020-05-08 20:39:42 +02:00
parent 684af9de0f
commit 476f292d73
3 changed files with 49 additions and 101 deletions

View file

@ -462,93 +462,40 @@ class element_helper {
* @return array the array of gradeable items in the course * @return array the array of gradeable items in the course
*/ */
public static function get_grade_items($course) { public static function get_grade_items($course) {
global $DB;
// Array to store the grade items. // Array to store the grade items.
$modules = array(); $arrgradeitems = array();
// Collect course modules data.
if ($modinfo = get_fast_modinfo($course)) {
$mods = $modinfo->get_cms();
$sections = $modinfo->get_section_info_all();
// Create the section label depending on course format.
$sectionlabel = get_string('section');
if ($course->format == 'topics') {
$sectionlabel = get_string('topic');
} else if ($course->format == 'weeks') {
$sectionlabel = get_string('week');
}
// Loop through each course section.
for ($i = 0; $i <= count($sections) - 1; $i++) {
// Confirm the index exists, should always be true.
if (isset($sections[$i])) {
// Get the individual section.
$section = $sections[$i];
// Get the mods for this section.
$sectionmods = explode(",", $section->sequence);
// Loop through the section mods.
foreach ($sectionmods as $sectionmod) {
// Should never happen unless DB is borked.
if (empty($mods[$sectionmod])) {
continue;
}
$mod = $mods[$sectionmod];
$instance = $DB->get_record($mod->modname, array('id' => $mod->instance));
// Get the grade items for this activity.
if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
$moditem = grade_get_grades($course->id, 'mod', $mod->modname, $mod->instance);
$gradeitem = reset($moditem->items);
if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
}
}
}
}
}
}
// Get other non-module related grade items. // Get other non-module related grade items.
if ($gradeitems = \grade_item::fetch_all(['courseid' => $course->id])) { if ($gradeitems = \grade_item::fetch_all(['courseid' => $course->id])) {
$arrgradeitems = [];
foreach ($gradeitems as $gi) { foreach ($gradeitems as $gi) {
// Skip the course and mod items since we already have them. if ($gi->is_course_item()) {
if ($gi->itemtype == 'mod' || $gi->itemtype == 'course') { continue; // Skipping for legacy reasons - this was added to individual elements.
continue; }
if ($gi->is_external_item()) {
$cm = get_coursemodule_from_instance($gi->itemmodule, $gi->iteminstance, $course->id);
$modcontext = \context_module::instance($cm->id);
$modname = format_string($cm->name, true, array('context' => $modcontext));
}
if ($gi->is_external_item() && !$gi->is_outcome_item()) {
// Due to legacy reasons we are storing the course module ID here rather than the grade item id.
// If we were to change we would need to provide upgrade steps to convert cm->id to gi->id.
$arrgradeitems[$cm->id] = get_string('activity', 'mod_customcert') . ' : ' . $gi->get_name();
} else if ($gi->is_external_item() && $gi->is_outcome_item()) {
// Get the name of the activity.
$optionname = get_string('gradeoutcome', 'mod_customcert') . ' : ' . $modname . " - " . $gi->get_name();
$arrgradeitems['gradeitem:' . $gi->id] = $optionname;
} else {
$arrgradeitems['gradeitem:' . $gi->id] = get_string('gradeitem', 'grades') . ' : ' . $gi->get_name(true);
} }
$arrgradeitems['gradeitem:' . $gi->id] = get_string('gradeitem', 'grades') . ' : ' . $gi->get_name(true);
} }
// Alphabetise this. // Alphabetise this.
asort($arrgradeitems); asort($arrgradeitems);
// Merge results.
$modules = $modules + $arrgradeitems;
} }
// Get outcomes being used by activities. return $arrgradeitems;
if ($gradeitems = \grade_item::fetch_all(['courseid' => $course->id, 'gradetype' => GRADE_TYPE_SCALE,
'itemtype' => 'mod'])) {
$selectoutcomes = [];
foreach ($gradeitems as $gradeitem) {
// Get the name of the activity.
$cm = get_coursemodule_from_instance($gradeitem->itemmodule, $gradeitem->iteminstance, $course->id);
$modcontext = \context_module::instance($cm->id);
$modname = format_string($cm->name, true, array('context' => $modcontext));
$optionname = get_string('gradeoutcome', 'mod_customcert') . ' : ' .$modname . " - " . $gradeitem->get_name();
$selectoutcomes['gradeitem:' . $gradeitem->id] = $optionname;
}
// Alphabetise this.
asort($selectoutcomes);
// Merge results.
$modules = $modules + $selectoutcomes;
}
return $modules;
} }
/** /**

View file

@ -22,6 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/ */
$string['activity'] = 'Activity';
$string['addcertpage'] = 'Add page'; $string['addcertpage'] = 'Add page';
$string['addelement'] = 'Add element'; $string['addelement'] = 'Add element';
$string['awardedto'] = 'Awarded to'; $string['awardedto'] = 'Awarded to';

View file

@ -180,44 +180,44 @@ Feature: Being able to manage elements in a certificate template
# Grade. # Grade.
And I add the element "Grade" to page "1" of the "Custom certificate 1" certificate template And I add the element "Grade" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values: And I set the following fields to these values:
| Grade item | Topic 0 : Assignment 1 | | Grade item | Activity : Assignment 1 |
| Grade format | Percentage | | Grade format | Percentage |
| Font | Helvetica | | Font | Helvetica |
| Size | 20 | | Size | 20 |
| Colour | #045ECD | | Colour | #045ECD |
| Width | 20 | | Width | 20 |
| Reference point location | Top left | | Reference point location | Top left |
And I press "Save changes" And I press "Save changes"
And I should see "Grade" in the "elementstable" "table" And I should see "Grade" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Grade" "table_row" And I click on ".edit-icon" "css_element" in the "Grade" "table_row"
And the following fields match these values: And the following fields match these values:
| Grade item | Topic 0 : Assignment 1 | | Grade item | Activity : Assignment 1 |
| Grade format | Percentage | | Grade format | Percentage |
| Font | Helvetica | | Font | Helvetica |
| Size | 20 | | Size | 20 |
| Colour | #045ECD | | Colour | #045ECD |
| Width | 20 | | Width | 20 |
| Reference point location | Top left | | Reference point location | Top left |
And I press "Save changes" And I press "Save changes"
# Grade item name. # Grade item name.
And I add the element "Grade item name" to page "1" of the "Custom certificate 1" certificate template And I add the element "Grade item name" to page "1" of the "Custom certificate 1" certificate template
And I set the following fields to these values: And I set the following fields to these values:
| Grade item | Topic 0 : Assignment 2 | | Grade item | Activity : Assignment 2 |
| Font | Helvetica | | Font | Helvetica |
| Size | 20 | | Size | 20 |
| Colour | #045ECD | | Colour | #045ECD |
| Width | 20 | | Width | 20 |
| Reference point location | Top left | | Reference point location | Top left |
And I press "Save changes" And I press "Save changes"
And I should see "Grade item name" in the "elementstable" "table" And I should see "Grade item name" in the "elementstable" "table"
And I click on ".edit-icon" "css_element" in the "Grade item name" "table_row" And I click on ".edit-icon" "css_element" in the "Grade item name" "table_row"
And the following fields match these values: And the following fields match these values:
| Grade item | Topic 0 : Assignment 2 | | Grade item | Activity : Assignment 2 |
| Font | Helvetica | | Font | Helvetica |
| Size | 20 | | Size | 20 |
| Colour | #045ECD | | Colour | #045ECD |
| Width | 20 | | Width | 20 |
| Reference point location | Top left | | Reference point location | Top left |
And I press "Save changes" And I press "Save changes"
# Image. # Image.
And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template And I add the element "Image" to page "1" of the "Custom certificate 1" certificate template