Show teacher for group only

Show teacher for group only

by Jason Lane -
Number of replies: 4

Hi all, is there a way to show just the name of the teacher/s for the GROUP that the certificate recipient is in?

I've been looking at the code in the certificate.php of my preferred layout (A4 embedded) and I'm wondering if I can replace something around the following section - just to show the coach for the group the student is enrolled in, not every teacher in that course.

In 'type/a4_embedded/certificate.php':

 if ($certificate->printteacher) {
    $context = context_module::instance($cm->id);
    if ($teachers = get_users_by_capability($context, 'mod/certificate:printteacher', '', $sort = 'u.lastname ASC', '', '', '', '', false)) {
        foreach ($teachers as $teacher) { ... }

Thanks to everyone for your contributions!

Average of ratings: -
In reply to Jason Lane

Re: Show teacher for group only

by Jean-Michel Védrine -

Hello Jason,

Don't forget that in Moodle a given user can be member of 0, 1 or several group(s) so there is no way you can find "the GROUP that the certificate recipient is in"

I never really used groups but looking at the docs : http://docs.moodle.org/dev/Groups_API, you could use the groups_get_user_groups($course->id, $USER->id) function to find all the groups for the student and loop over all these groups to see if $teacher is a member of this group using groups_is_member($groupid, $teacher->id) function and if yes use certificate_print_text to print teacher's name.

Please note that this is just ideas about the code you could write, if you have some php experience, you can use these ideas to write the code, but surely some work will be needed to have a working code.


In reply to Jean-Michel Védrine

Re: Show teacher for group only

by Jason Lane -

Thanks for your reply - sorry about my text formatting in the original post. I'm looking into lots of other customisations to the form/database side of this plugin too.

In reply to Jason Lane

Re: Show teacher for group only

by Jean-Michel Védrine -

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.

Average of ratings: Useful (1)
In reply to Jean-Michel Védrine

Re: Show teacher for group only

by Cath Trew -

Hello!  This question isn't really related to this subject but ... I'm new to Moodle and have little or no knowledge of programming and techie stuff!  I have been looking at a number of forums which talk about editing the code for a certificate, however I don't know where to find the code.  For example the following location is mentioned in this forum -  mod/certificate/type/yourtype/certificate.php file - could someone tell me where I would find this?  I've managed to create a certificate but want to move the text down a line or two - so if you could give me the code for that as well, that would be great!!!