Hello,
As I had to do the same thing for another activity that I am working on, here is the full code to only print the name of teachers in the same group..
In the mod/certificate/type/yourtype/certificate.php file where yourtype is the name of certificate you are using,
After
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from view.php
}
Add the following function:
function get_teachers_in_same_group($userid, $cm, $course) {
$context = context_module::instance($cm->id);
$potentialteachers = get_users_by_capability($context, 'mod/certificate:printteacher', '', $sort = 'u.lastname ASC', '', '', '', '', false);
$teachers = array();
if (groups_get_activity_groupmode($cm) == SEPARATEGROUPS) {
if ($groups = groups_get_all_groups($course->id, $userid, $cm->groupingid)) {
foreach ($groups as $group) {
foreach ($potentialteachers as $potentialteacher) {
if (groups_is_member($group->id, $potentialteacher->id)) {
$teachers[$potentialteacher->id] = $potentialteacher;
}
}
}
} else {
// User not in any group, try to find teachers without group.
foreach ($potentialteachers as $potentialteacher) {
if (!groups_has_membership($cm, $potentialteacher->id)) {
$teachers[$potentialteacher->id] = $potentialteacher;
}
}
}
return $teachers;
} else {
return $potentialteachers;
}
}
Then later in the same file when you want to print the name of the teachers, do:
if ($certificate->printteacher) {
if ($teachers = get_teachers_in_same_group($USER->id, $cm, $course)) {
foreach ($teachers as $teacher) {
$i++;
certificate_print_text($pdf, $sigx, $sigy + ($i * 4), 'L', 'freeserif', '', 12, fullname($teacher));
}
}
}
Last thing, for the code to work, you need to choose "separate groups" in the certificate settings.
Hope it will suit your needs.