I’m working on a Drupal project that’s connected to a Moodle database. I need to pull all the courses from Moodle along with the usernames of the people who made them. This info is crucial for my next steps in development.
I’ve already set up the connection between Drupal and the Moodle database. Now I’m stuck on how to actually get this data out. Can anyone point me in the right direction for writing a query or using any specific Drupal functions to fetch this info?
I’m not super familiar with Moodle’s database structure, so any tips on which tables to look at would be really helpful too. Thanks in advance for any advice!
Hey there ExploringForest! 
Sounds like you’ve got an interesting project on your hands! I’m actually really curious about how you’re integrating Moodle with Drupal. That’s a pretty cool setup!
For getting the course list with creator usernames, have you looked into Moodle’s ‘mdl_course’ and ‘mdl_user’ tables? I think those might be what you’re after. You’d probably need to join them somehow to get the creator info.
But here’s a thought - have you considered using Moodle’s Web Services API instead of directly querying the database? It might be a cleaner approach and could save you some headaches down the road.
What made you choose to connect Drupal to Moodle’s database directly? I’d love to hear more about your project and the challenges you’re facing. Maybe we can brainstorm some solutions together!
Keep us posted on how it goes, yeah? Good luck with your development! 
yo ExploringForest, have u checked out the mdl_course and mdl_user tables? thats prolly where the good stuff is. u might wanna join em like this:
SELECT c.id, c.fullname, u.username
FROM mdl_course c
JOIN mdl_user u ON c.userid = u.id
just a guess tho. let us kno how it goes!
To retrieve the Moodle course list with creator usernames, you’ll need to query the ‘mdl_course’ and ‘mdl_user’ tables in the Moodle database. Here’s a basic SQL query that should work:
SELECT c.id AS course_id, c.fullname AS course_name, u.username AS creator_username
FROM mdl_course c
JOIN mdl_user u ON c.id = u.id
This query joins the course and user tables on the ID field, which should give you the course details along with the creator’s username. You might need to adjust the join condition depending on how Moodle stores the creator information.
In Drupal, you can execute this query using the Database API. Something like:
$query = Database::getConnection(‘default’, ‘moodle_database’)
->select(‘mdl_course’, ‘c’)
->fields(‘c’, [‘id’, ‘fullname’])
->join(‘mdl_user’, ‘u’, ‘c.id = u.id’)
->fields(‘u’, [‘username’]);
$results = $query->execute()->fetchAll();
Remember to replace ‘moodle_database’ with your actual Moodle database connection name in Drupal’s configuration.