Ad-hoc database queries

Reports ::: report_customsql
Maintained by Tim at Lone Pine Koala SanctuaryTim Hunt, at the OU (Perry building)Mahmoud Kassaei
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:
3793 sites
146 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 quicly set up ad-hoc reports, without having to create a whole new admin report plugin.


Screenshot #0


Tim at Lone Pine Koala Sanctuary
Tim Hunt (Lead maintainer)
at the OU (Perry building)
Mahmoud Kassaei: Developer
Please login to view contributors details and/or to contact them

Comments RSS


  • Tim at Lone Pine Koala Sanctuary
    Tue, Jul 30, 2019, 8:44 PM
    Yes, the are currently kept for ever. We ought to do something about that, and was created some time ago to track it.

    Even with a daily report, I don't think the archive could possibly fill up, so I don't konw why it has stopped working for you. Note that it only archives if the report includes some data.
  • Ray at his desk
    Sat, Aug 3, 2019, 12:20 AM
    Hi team,

    Our system is no longer sending out email notifications for our reports we have set to run daily. I think I tracked it down to an error about `message_send() must be an instance of core\message\message, instance of stdClass given`, which reminded me of a commit I saw on the GitHub repo. However, the code available on this page does not include that commit.

    I posted a longer explanation as an issue on your GitHub repo. I would deeply appreciate it if anyone could take a look and provide any insight. Thank you!

    - Ray
  • Tue, Aug 27, 2019, 10:43 AM

    I have the same problem with Email sending function.
    Even though CSV files were saved, sending email has not worked since upgrading 3.7.

    Moodle 3.7+ (Build: 20190628) / Plugin Ver: 2018112200

    I got the following message for the Failed tasks in the Cron Task logs page.

    !!! Coding error detected, it must be fixed by a programmer: A lock was created but not released at:
    [dirroot]/lib/cronlib.php on line 99
    Code should look like:
    $factory = \core\lock\lock_config::get_lock_factory('type');
    $lock = $factory->get_lock(Resource id #565);
    $lock->release(); // Locks must ALWAYS be released like this.

    Is it related to the issue?
  • Wed, Sep 25, 2019, 8:37 AM
    Is there a way to install on 3.7?
    How can I contribute to do it?
  • Tim at Lone Pine Koala Sanctuary
    Thu, Sep 26, 2019, 1:27 AM
    Thanks for the reminder. The report was basically alreayd compatible with 3.7, but I have just uploaded the latest version here.
  • Fri, Sep 27, 2019, 6:56 PM
    Thanks for this valuable information with us.
  • Mon, Oct 7, 2019, 10:10 PM
    Good morning. We are having an issue with duplicate records in the output of an ad hoc db query. When the ad hoc db query runs, the resulting file has duplicates - not all duplicates but duplicates.
    When I pull the query logic out of the ad hoc db query and run it in mySQL Workbench, I do not get duplicates.
    I even tried to pull the logic directly out of the mdl_report_customsql_queries table and running that in mySQL Workbench and I still can't recreate the duplicates.
    I've tried doing a Notepad++ file compare between the logic in the table and the logic on the front end - nothing.
    I re-read the fine print in the plugin on the front end and I saw mention of what it does when a column ends in the word "date". I did have a column that ended with "date" and I changed it to avoid any chance of issue there. Still duplicates.
    We've upgraded the plugin to the newest available version.

    Any thoughts on why this might be happening?
    Thank you in advance.
  • Tim at Lone Pine Koala Sanctuary
    Mon, Oct 7, 2019, 11:52 PM
    The report really just executes teh given SQL using Moodle's database API:

    Can you try executing the query directly in MySQL. I wonder if mySQL Workbench is hiding duplicate rows for some reason.
  • Tue, Oct 8, 2019, 4:35 AM
    Tim thank you for the prompt response! : )
    I neglected to mention one other detail - we have the exact same report built using the configurable reports plugin and that report does not produce the duplicates either.

    Does that change your recommendation at all?
    Thank you again!
  • Wed, Nov 13, 2019, 2:29 PM
    Hi I was trying to modify the plugin to make it run every N minutes instead of a 'given time daily'. Im not really sure on how to do this. It would be great if you can shed some light on this query.

    Great plugin by the way!

  • Wed, Nov 13, 2019, 11:46 PM
    Hello @Ivan Marquez: I'm not sure you can directly. AFAIK, what the plugin does is send the query to the cron task manager, so, depending on your Moodle version, you'd need to check there in order to find when is it being sent. Also, if it's being sent via an old cron job, the time is set by the system in which Moodle is running, not even Moodle itself.
  • Stefano Guglielmetti
    Thu, Nov 28, 2019, 10:40 PM
    Hi! Is there a way to change the 5000 rows limit?
  • Tim at Lone Pine Koala Sanctuary
    Fri, Nov 29, 2019, 2:24 AM
    Upgrade to the latest version.
  • Thu, Dec 26, 2019, 6:27 PM
    Hi. Is it possible to set the “Automatically email to” field in order to send an email with the result of the query to all users with Manager role assigned?
  • Panthers fan
    Fri, Jan 3, 2020, 6:59 AM
    Thanks for a great plugin. Does this plugin have a dedicated forum to ask for assistance with writing custom reports? If not, where would I post to ask for some guidance with a rather complicated course completion report (well, at least for me it is)? Thanks.
Please login to post comments