'SQL Drilldown' report type

'SQL Drilldown' report type

by Davo Smith -
Number of replies: 3
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I was in need of a few features that were not available in the 'standard' version of this excellent plugin, so I've created a new report, component and filter type to achieve what I was looking for.

A modified copy of your plugin, with these additions, can be found here:

https://github.com/davosmith/moodle-block_configurable_reports

 

Overview of the changes:

  • The 'SQL Drilldown' report type allows you to create an SQL query that shows statistics about all the top-level categories on your site, and then 'drill down' into each category (by clicking on the link inserted into each row of the table) to get further information about that category.
  • You can also include and unlimited number of independent SQL queries on a single page, by separating them with '###' - this I have used this to display a table with statistics about courses within the selected category, as well as a table of subcategories, on the same page.
  • There is a breadcrumb trail to easily keep track of where you have got to and to go back again.
  • There is also a new 'role' filter, that allows you to filter the results by role.
Adding all those together, I am able to write the following query (MySQL only - the CONCAT(tca.path, '/%') bit needs to change to (tca.path || '/%') for it to work with Postgres):

SELECT tca.name AS "Category name", r.name AS Role, count(distinct(ra.userid)) AS "User count"
FROM
( SELECT name, id, path FROM prefix_course_categories WHERE id = %%DRILLDOWN_PARENT_CAT%% ) tca,
prefix_role_assignments ra
JOIN prefix_context cx ON ra.contextid = cx.id
JOIN prefix_role r ON ra.roleid = r.id
JOIN prefix_course c ON cx.instanceid = c.id
JOIN prefix_course_categories ca ON c.category = ca.id
WHERE cx.contextlevel = 50
AND (ca.id = tca.id OR ca.path LIKE CONCAT(tca.path, '/%'))
AND ca.id %%DRILLDOWN_ALLOWED_CATS%%
%%FILTER_ROLES:ra.roleid%%
GROUP BY tca.name, r.name

###

SELECT tca.id AS "drilldownid", tca.name AS "Category name", r.name AS Role, count(distinct(ra.userid)) AS "User count"
FROM
( SELECT name, id, path FROM prefix_course_categories WHERE parent = %%DRILLDOWN_PARENT_CAT%% ) tca,
prefix_role_assignments ra
JOIN prefix_context cx ON ra.contextid = cx.id
JOIN prefix_role r ON ra.roleid = r.id
JOIN prefix_course c ON cx.instanceid = c.id
JOIN prefix_course_categories ca ON c.category = ca.id
WHERE cx.contextlevel = 50
AND (ca.id = tca.id OR ca.path LIKE CONCAT(tca.path, '/%'))
AND ca.id %%DRILLDOWN_ALLOWED_CATS%%
%%FILTER_ROLES:ra.roleid%%
GROUP BY tca.id, tca.name, r.name

###

SELECT c.shortname AS "Course name", r.name AS Role, count(distinct(ra.userid)) AS "User count"
FROM prefix_role_assignments ra
JOIN prefix_context cx ON ra.contextid = cx.id
JOIN prefix_role r ON ra.roleid = r.id
JOIN prefix_course c ON cx.instanceid = c.id
WHERE cx.contextlevel = 50
AND c.category = %%DRILLDOWN_PARENT_CAT%%
AND c.category %%DRILLDOWN_ALLOWED_CATS%%
%%FILTER_ROLES:ra.roleid%%
GROUP BY c.shortname, r.name

 

Which, basically shows a count of:

  • The total number of users assigned to each role (student/teacher/etc.) for the courses within the current category
  • The same details broken down by sub-category
  • The same details again, shown against any courses directly within the current category
(The DRILLDOWN_ALLOWED_CATS bit only allows users to see categories where they have been assigned the capability 'moodle/category:manage' - although, for simplicity and speed, it assumes that the permission is not overridden at any point, by 'prevent' or 'prohibit').
By adding the 'role' filter, you can select which role to display (without the filter, all roles are shown, one after the other).
By clicking on the links automatically generated from the 'drilldownid' field, you can then explore the same stats for each of the sub-categories.
I hope you might consider this a useful addition to the configurable_reports plugin and possibly include some of these changes in the official version (I will probably do a Moodle 2.x version of these changes, at some point, but I don't need it just yet, so it will be a while before I can do so).
Average of ratings: -
In reply to Davo Smith

Re: New plugin: Custom reports

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Thanks Davo for pointing out your new report plugin.

Unfortunatelly, the Moodle 1.9 version of this plugin is @frozen@. I mean, Im not going to add new features. I will only fix bugs.

I am not having much spare time so I can work only in fix bugs and add new features to  the HEAD version. In this case, Moodle 2

Thanks again

In reply to Juan Leyva

Re: New plugin: Custom reports

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
No problem, I'll let you know when I produce a Moodle 2 version of the changes I made.
In reply to Davo Smith

Re: New plugin: Custom reports

by Nacho Aguilar -

Hello Davo,

 

I know this is a very old thread but perhaps you can help us with this old add-on that you did.

 

I don't know if you have found a similar problem but when we use the drill down report we cannot export to ods or excel, files are empty.

 

We are going to move to Moodle 2.6 soon so if there is not a solution, forget this message. If you faced a similar problem and you had a solution we will very happy to hear good news from you.

 

Thank you for your addon and your help.