Databases: Retrieve the email of a teacher from a given course

Databases: Retrieve the email of a teacher from a given course

by David Hoeffel -
Number of replies: 6

Hello Moodle community,

We are two French interns currently developping new functionnalities for a website based on Moodle (http://moodle.cifad.ca/)

On this website, users have the possibility to learn by themselves by having access to various courses. When they are ready, they can take a test which will determine wether they passed the course or not.

----------

When students are unenroled from a course, we would like an e-mail to be automatically sent to the student, and his teacher.

The problem is that we didn't manage to find the teacher associated to a course. Therefore, my question would be : Is there a way to get the email of a teacher from a given course ?


If you need more details, i'll be happy to give you the SQL Query we've already built and the tables we though we could find useful info in. We're kinda new to all this, so we're a little lost in the Moodle database wide eyes

 

Big Thanks for your help, and sorry if I'm asking a (stupid) question, which might also have already been asked...

Average of ratings: -
In reply to David Hoeffel

Re: Databases: Retrieve the email of a teacher from a given course

by David Hoeffel -

UP ! wink

In reply to David Hoeffel

Re: Databases: Retrieve the email of a teacher from a given course

by David Hoeffel -

Perhaps I should give some precisions to what we've come up to.

This is our SQL request :

SELECT DISTINCT
    u.username,
    c.fullname as "Course name",
    u.email,
    ra.id
   
From
    moodledev.mdl_user u,
    moodledev.mdl_course c,
    moodledev.mdl_enrol e,
    moodledev.mdl_role_assignments ra,
    moodledev.mdl_user_enrolments ue
Where
    u.id =ue.userid and
    ue.userid=ra.userid and
    ra.roleid= 3 and

    ue.enrolid= e.id and
    e.courseid= c.id
    group by ra.id

;

 

And we get a list of :

-teacher names

-course names related to the courses they have enrolled in

-their e-mail

-the unique ID (taken from the "mdl_role_assignments" table) that refers to a line that makes a recap of the users role each time it has been changed (if what I say makes sense to you)

--------------------------

The problem is that I have the same lines appearing when a user has been assigned several times to the same role. Considering I am selecting all the theachers from the database, I get :

username course name email id
teacher1 biology random@stuff.com

201

teacher1 biology random@stuff.com

212

Is there a way to select only the line with the highest ID ?

The probem we have is that some teachers are still listed as "teachers" in courses in which they arent enrolled anymore, because moodle keeps track of the role changes in the database. ("mdl_role_assignments" table)

In reply to David Hoeffel

Re: Databases: Retrieve the email of a teacher from a given course

by David Hoeffel -

Hello,

I am updating this topic to give more precisions on our progression.

The following diagram shows the links we figured out there was between he tables, that enable us to link a user to its courses :

Diagram that shows relations detween a user and its courses

Sorry if it looks like a class diagram, but I thought I'd be an easy solution to build it up quickly. Note that the class attributes actually coresspond to the names of the rows of the specified tables.

 

______________________________________

 

Somehow, by linking all of these table, we get the right amount of teachers, except that the course names aren't being updated.

 

Here's our new SQL request :

SELECT  DISTINCT ra.id as raid, u.username, u.email, r.name, c.fullname


FROM    moodledev.mdl_role_assignments ra,
        moodledev.mdl_user u,
        moodledev.mdl_role r,
        moodledev.mdl_course c,
        moodledev.mdl_enrol e,
        moodledev.mdl_user_enrolments ue
        
        
WHERE   u.id = ra.userid
AND     r.id = ra.roleid
AND     ra.roleid = 3
AND     ue.userid=ra.userid
AND     ue.enrolid= e.id
AND     e.courseid= c.id
     
GROUP BY raid

 

The only problem with this request is that when a teacher is part of more than 1 course, request returns the same source name twice instead of returning two lines with different course names. We get this :

259 Eric bla@gmail.com Teacher english
268 Eric bla@gmail.com Teacher english

instead of that :

259 Eric bla@gmail.com Teacher english
268 Eric bla@gmail.com Teacher biology

And the first column is the role_assignement id.

In reply to David Hoeffel

Re: Databases: Retrieve the email of a teacher from a given course

by Johnathan Au -
You are missing the relationship with the contexts table. That will solve your problem. Take some time to figure out the contexts table then report back. Contexts table is one of the main tables for everything if not the main one.
In reply to Johnathan Au

Re: Databases: Retrieve the email of a teacher from a given course

by David Hoeffel -

Thanks Johnathan. I did take a look at the contexts table and indeed, all the necessary data is here. I just had a lot of trouble understanding what all the columns stand for. Thanks to this particularily useful topic http://moodle.org/mod/forum/discuss.php?d=77316, I managed to solve my problem.

I settled on something like this and the query went on perfectly :

SELECT c.id, c.fullname,u.lastname,r.name
                    
FROM moodledev.mdl_course c
JOIN moodledev.mdl_context ct ON c.id = ct.instanceid
JOIN moodledev.mdl_role_assignments ra ON ra.contextid = ct.id
JOIN moodledev.mdl_user u ON u.id = ra.userid
JOIN moodledev.mdl_role r ON r.id = ra.roleid

WHERE c.id = $course AND r.id=3

 

Thanks again

In reply to David Hoeffel

Re: Databases: Retrieve the email of a teacher from a given course

by Johnathan Au -

Yeah I had trouble during my first couple of weeks with Moodle myself. I'm sort of interning myself as well so it's all this is very new to me. Good experience though smile But yeah, context table is usually the table you fall back to