From 784ca08fb136b0450781b51edfdf90062257efe2 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Fri, 8 May 2020 15:40:32 +0200 Subject: [PATCH] Add the ability to select Outcomes in the Grade element (#329) --- classes/element_helper.php | 87 ++++++++++++++++++++++++-------------- lang/en/customcert.php | 1 + 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/classes/element_helper.php b/classes/element_helper.php index 622fa11..42b2704 100644 --- a/classes/element_helper.php +++ b/classes/element_helper.php @@ -468,46 +468,48 @@ class element_helper { $modules = array(); // Collect course modules data. - $modinfo = get_fast_modinfo($course); - $mods = $modinfo->get_cms(); - $sections = $modinfo->get_section_info_all(); + 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'); - } + // 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; + // 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. if ($gradeitems = \grade_item::fetch_all(['courseid' => $course->id])) { $arrgradeitems = []; foreach ($gradeitems as $gi) { @@ -525,6 +527,27 @@ class element_helper { $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; } diff --git a/lang/en/customcert.php b/lang/en/customcert.php index c5f7a08..4d4b5c2 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -96,6 +96,7 @@ $string['fontcolour_help'] = 'The colour of the font.'; $string['fontsize'] = 'Size'; $string['fontsize_help'] = 'The size of the font in points.'; $string['getcustomcert'] = 'View certificate'; +$string['gradeoutcome'] = 'Outcome'; $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['invalidcode'] = 'Invalid code supplied.';