Some Simple MySQL Queries for Moodle
The Moodle logs contain some great data on user activity. Awhile ago, I put a page detailing some statistics on WSD Online, our own Moodle server. You can find that page here: http://www.weber.k12.ut.us/index.php?page=moodle-statistics
Here’s a few MySQL queries you can use on your own Moodle server to get some statistics on the most popular activities, the most active users, and the most active courses. These have been tested with Moodle 1.9. I’m not a MySQL master, so they could probably use some further optimization, but overall they should work well enough:
Find the most popular activities:
SELECT COUNT(l.id) hits, module
FROM mdl_log l
WHERE module != 'login' AND module != 'course' AND module != 'role'
GROUP BY module
ORDER BY hits DESC
Find the most active users over the past 7 days (change the “604800” to the number of the appropriate number of seconds if you want to adjust this interval):
SELECT COUNT(l.id) hits, l.userid, u.username, u.firstname, u.lastname
FROM mdl_log l INNER JOIN mdl_user u ON l.userid = u.id
WHERE l.time > UNIX_TIMESTAMP(NOW()) - 604800
GROUP BY l.userid
ORDER BY hits DESC
Find the most active courses:
SELECT COUNT(l.id) hits, l.course courseId, c.fullname coursename
FROM mdl_log l INNER JOIN mdl_course c ON l.course = c.id
GROUP BY courseId
ORDER BY hits DESC
(You may need to change the second line to FROM mdl_log l INNER JOIN mdl_course c ON l.course = c.id AND c.id != ‘1’ to omit home page hits)
I know this is an older post, but I am wondering if you can help…. I need to get the grouping or group id/name for an if/else statement.
quisiera hacer una consulta por nombre donde aliste los movimientos de un usuario en todo moodle
Hi,
by chance i found your site and i am interested in queries according to our moodle-plattform as well.
According to my bachelor thesis i a m trying to design and develop a java based GUI for analytical queries.
your examples seem to be the right base for my work.
But i habe problems in understanding following circumstance:
you have: “FROM mdl_log l …” in your statements.
when i go into my mysql database i find the table mdl_log but no mdl_log l.
When i look into the table, mysql says that the table is empty.
when i copy your statement nothing happens.
can you give me a hint?
sincerly
thomas
Aditional information:
I am using the newest version of moodle (2.7) and have installed a test enviroement at localhost.
The moodle-db therefore is rather empty at the moment. I have included two test courses, some users and some content inside the courses.