Moodle plugins directory: Ad-hoc database queries | Moodle.org
Ad-hoc database queries
Reports ::: report_customsql
Maintained by Tim Hunt, Mahmoud Kassaei, Anupama Sarjoshi
This report plugin allows Administrators to set up arbitrary database queries
to act as ad-hoc reports. Reports can be of two types, either run on demand,
or scheduled to run automatically. Other users with the right capability can
go in and see a list of queries that they have access to. Results can be viewed
on-screen or downloaded as CSV.
Latest release:
4486 sites
1k downloads
167 fans
Current versions available: 10
This report, created by The Open University, lets Administrators set up arbitrary SQL select queries that anyone with the appropriate permissions can then run. Reports can be set to be runnable on-demand, or automatically run weekly or monthly.
The results are displayed as a fairly plain HTML table, and can also be downloaded as CSV.
The idea is that this lets you quickly set up ad-hoc reports, without having to create a whole new admin report plugin.
Contributors
Tim Hunt (Lead maintainer)
Mahmoud Kassaei: Developer
Anupama Sarjoshi: Tester & Developer
Please login to view contributors details and/or to contact them
You will need to make a new role "View reports" which give that capability, and which you assign at site-level.
(Alternatively, you could call the role something more meaningful like "Teaching staff" in case you need to use it for other purposes in the future.)
I will add my thanks to everyone else's. This is a fantastic plugin.
I have one of suggestion: create an option to hide the "back to the list of available queries" link for those not allowed to manage/edit queries. Links to individual queries could then be placed in a URL resource, HTML block, custom menu, etc., without exposing the whole list of queries.
Thanks again.
A better option might be a way to generate a token for a particular query, then if someone went to the URL ...?id=123&token=... then they would be allowed to see just that query.
However, at the moment that is just an idea. I don't have time to implement that.
Continue to receive Error when attempting to include link in query results. Moodle Version 2.4.5 and lastest version of custom sql. Query tested directly through mysql workbench. Any insight is greatly appreciated
Below is sample.
Query SQLRequired field
Error when executing the query: ERROR: Incorrect number of query parameters. Expected 1, got 0.
SELECT
u.id,
u.firstname,
concat('',u.firstname,'') AS User
FROM prefix_cohort AS h
JOIN prefix_cohort_members AS hm ON h.id = hm.cohortid
JOIN prefix_user AS u ON hm.userid = u.id
ORDER BY cohortid
? is a special character in SQL in Moodle, and so you are not allowed it anywhere, even in string literals.
The help text in the latest version makes this clear, and given the work-around. You can sort-of read it here: https://github.com/moodleou/moodle-report_customsql/blob/master/lang/en/report_customsql.php#L114
concat('
Color
')Complete query
Select
c.id As cid,
Date_Format(From_UnixTime(c.startdate), '%m/%d/%Y') As Start,
c.shortname As Shortname,
c.fullname As `Course Name`,
Count(Distinct u.id) Enrolled,
c.maxstudents As Max,
c.maxstudents - Count(Distinct u.id) As Seats_available,
Format(Count(Distinct u.id) / (c.maxstudents) * 100, 2) As Percentagefull,
if(Format(Count(Distinct u.id) / (c.maxstudents) * 100, 2) <80 ,'Red', 'Green') Go,
concat('
Color
')From
prefix_role_assignments ra,
prefix_user u,
prefix_course c Inner Join
prefix_course_categories On c.category = prefix_course_categories.id,
prefix_context cxt
Where
ra.userid = u.id And
ra.contextid = cxt.id And
cxt.instanceid = c.id And
c.fullname Not Like '%cancel%' And
From_UnixTime(c.startdate) >= CurDate() And
DateDiff(Date_Add(CurDate(), Interval 30 Day), From_UnixTime(c.startdate)) >=
4
Group By
c.id, prefix_course_categories.name
Having
Format(Count(Distinct u.id) / (c.maxstudents) * 100, 2) <= 99
Order By
c.startdate,
Percentagefull
Color
which of course does not work.
(Note, these comments seem to be unreadable here. The HTML tags were readable in the notification email.)
Any ideas why this would happen, and how I can see these archived reports? I'm baffled.
Thanks,
Blair
I noticed that path to the archived query is in the format /report/customsql/view.php?id=__×tamp=________.
I could probably create another report to list the archives, but I'm not sure where to look for them. I checked the report_customsql_queries table and cannot find anything.
Any tips?
@tim: thanks for that great plugin it helps me a lot in the daily routine.
I am new to SQL and have got a very basic question. Is it possible to retrieve the email of the trainer (Role A) and the email of the manager (role B) for a course in one single record (row) listed behind the course fullname?
What is working is the following:
SELECT c.id, c.fullname, u.email AS Role_A
FROM prefix_course AS c
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
WHERE ra.roleid =15
AND ctx.instanceid = c.id
AND u.deleted=0
I would like to have another column called Role_B which is looking up u.email WHERE ra.roleid =14.
Is it generally possible to retrieve 'u.email' twice in the same record? And if yes, how? I would appreciate getting a link where this is explained, I could not even find a hint. Thanks a lot!!
SELECT c.fullname, traineruser.lastname AS trainer_name, manageruser.lastname AS manager_name
FROM prefix_course AS c
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS trainerra ON trainerra.contextid = ctx.id
JOIN prefix_user AS traineruser ON traineruser.id = trainerra.userid
JOIN prefix_role_assignments AS managerra ON managerra.contextid = ctx.id
JOIN prefix_user AS manageruser ON manageruser.id = managerra.userid
WHERE trainerra.roleid = 15
AND managerra.roleid = 17
AND ctx.contextlevel = 50
AND u.deleted=0
I am able now to retrieve the data required.