Add the ability to select Outcomes in the Grade element (#329)

This commit is contained in:
Mark Nelson 2020-05-08 15:40:32 +02:00
parent a5d51f2478
commit c992d7a376
2 changed files with 56 additions and 32 deletions

View file

@ -468,46 +468,48 @@ class element_helper {
$modules = array(); $modules = array();
// Collect course modules data. // Collect course modules data.
$modinfo = get_fast_modinfo($course); if ($modinfo = get_fast_modinfo($course)) {
$mods = $modinfo->get_cms(); $mods = $modinfo->get_cms();
$sections = $modinfo->get_section_info_all(); $sections = $modinfo->get_section_info_all();
// Create the section label depending on course format. // Create the section label depending on course format.
$sectionlabel = get_string('section'); $sectionlabel = get_string('section');
if ($course->format == 'topics') { if ($course->format == 'topics') {
$sectionlabel = get_string('topic'); $sectionlabel = get_string('topic');
} else if ($course->format == 'weeks') { } else if ($course->format == 'weeks') {
$sectionlabel = get_string('week'); $sectionlabel = get_string('week');
} }
// Loop through each course section. // Loop through each course section.
for ($i = 0; $i <= count($sections) - 1; $i++) { for ($i = 0; $i <= count($sections) - 1; $i++) {
// Confirm the index exists, should always be true. // Confirm the index exists, should always be true.
if (isset($sections[$i])) { if (isset($sections[$i])) {
// Get the individual section. // Get the individual section.
$section = $sections[$i]; $section = $sections[$i];
// Get the mods for this section. // Get the mods for this section.
$sectionmods = explode(",", $section->sequence); $sectionmods = explode(",", $section->sequence);
// Loop through the section mods. // Loop through the section mods.
foreach ($sectionmods as $sectionmod) { foreach ($sectionmods as $sectionmod) {
// Should never happen unless DB is borked. // Should never happen unless DB is borked.
if (empty($mods[$sectionmod])) { if (empty($mods[$sectionmod])) {
continue; continue;
} }
$mod = $mods[$sectionmod]; $mod = $mods[$sectionmod];
$instance = $DB->get_record($mod->modname, array('id' => $mod->instance)); $instance = $DB->get_record($mod->modname, array('id' => $mod->instance));
// Get the grade items for this activity. // Get the grade items for this activity.
if ($gradeitems = grade_get_grade_items_for_activity($mod)) { if ($gradeitems = grade_get_grade_items_for_activity($mod)) {
$moditem = grade_get_grades($course->id, 'mod', $mod->modname, $mod->instance); $moditem = grade_get_grades($course->id, 'mod', $mod->modname, $mod->instance);
$gradeitem = reset($moditem->items); $gradeitem = reset($moditem->items);
if (isset($gradeitem->grademax)) { if (isset($gradeitem->grademax)) {
$modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name; $modules[$mod->id] = $sectionlabel . ' ' . $section->section . ' : ' . $instance->name;
}
} }
} }
} }
} }
} }
// 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 = []; $arrgradeitems = [];
foreach ($gradeitems as $gi) { foreach ($gradeitems as $gi) {
@ -525,6 +527,27 @@ class element_helper {
$modules = $modules + $arrgradeitems; $modules = $modules + $arrgradeitems;
} }
// Get outcomes being used by activities.
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; return $modules;
} }

View file

@ -96,6 +96,7 @@ $string['fontcolour_help'] = 'The colour of the font.';
$string['fontsize'] = 'Size'; $string['fontsize'] = 'Size';
$string['fontsize_help'] = 'The size of the font in points.'; $string['fontsize_help'] = 'The size of the font in points.';
$string['getcustomcert'] = 'View certificate'; $string['getcustomcert'] = 'View certificate';
$string['gradeoutcome'] = 'Outcome';
$string['height'] = 'Height'; $string['height'] = 'Height';
$string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.'; $string['height_help'] = 'This is the height of the certificate PDF in mm. For reference an A4 piece of paper is 297mm high and a letter is 279mm high.';
$string['invalidcode'] = 'Invalid code supplied.'; $string['invalidcode'] = 'Invalid code supplied.';