Configurable SQL report access logs

Configurable SQL report access logs

by Dani Márquez BiG -
Number of replies: 2
Good morning, 

I've been trying to create a custom SQL query using the configurable reports plugin but im stuck.

I want this info:
  • Student name
  • Distinct days the student conected to the course
  • First access day to the course
  • Total time connected to course
  • Scorm packages completed
  • Course final score

I was able to get some of the info with chatgpt help:

WITH UserLogs AS (

    SELECT

        u.id AS userid,

        u.firstname AS first_name,

        u.lastname AS last_name,

        l1.timecreated AS log_time,

        COALESCE(l2.timecreated, 0) AS previous_log_time,

        CASE

            WHEN l2.timecreated IS NULL THEN 0

            ELSE TIME_TO_SEC(TIMEDIFF(l1.timecreated, l2.timecreated))

        END AS time_diff

    FROM

        mdl_user u

    LEFT JOIN

        mdl_logstore_standard_log l1 ON u.id = l1.userid

    LEFT JOIN

        mdl_logstore_standard_log l2 ON u.id = l2.userid

        AND l1.id > l2.id

    WHERE

        l1.courseid IS NOT NULL

),

SCORMActivities AS (

    SELECT

        c.userid AS userid,

        cm.instance AS scorm_id

    FROM

        mdl_course_modules_completion c

    JOIN

        mdl_course_modules cm ON c.coursemoduleid = cm.id

    JOIN

        mdl_modules m ON cm.module = m.id

    WHERE

        m.name = 'scorm'

)

SELECT

    U.userid,

    U.first_name,

    U.last_name,

    COUNT(DISTINCT DATE(FROM_UNIXTIME(U.log_time))) AS distinct_days_connected,

    SEC_TO_TIME(SUM(CASE WHEN U.time_diff <= 900 THEN U.time_diff ELSE 0 END)) AS total_time_connected,

    COALESCE(SUM(CASE WHEN SC.scorm_id IS NOT NULL THEN 1 ELSE 0 END), 0) AS scorm_activities_count,

    ROUND(AVG(Q.grade), 2) AS average_quiz_grade

FROM

    UserLogs U

LEFT JOIN

    SCORMActivities SC ON U.userid = SC.userid

LEFT JOIN

    mdl_course_modules CM ON U.log_time >= U.previous_log_time + 900

LEFT JOIN

    mdl_quiz Q ON CM.instance = Q.course

GROUP BY

    U.userid, U.first_name, U.last_name

ORDER BY

    U.last_name, U.first_name;


I added a timediff maximum of 15 mins as a threshold for inactivity but it doesn't work and I don't really know how to get logs info into the sql query.


Thanks in advance for any tips

Average of ratings: -
In reply to Dani Márquez BiG

Re: Configurable SQL report access logs

by Quintin Seegers -
Picture of Particularly helpful Moodlers
Hi Dani,

Have a look at the following links. They helped me a lot (we use Moodle 3.11):
https://docs.moodle.org/311/en/ad-hoc_contributed_reports
https://docs.moodle.org/311/en/Configurable_reports
https://www.examulator.com/er/3.11/index.html

Others could help with your specific query, but a couple of things I've noticed:
1. Table names should be prefixed with "prefix_" instead of "mdl_", e.g. mdl_quiz should be prefix_quiz.
2. Chatgpt has most likely given you the syntax for something like MySQL or MS SQL. Depending on the database used for your Moodle, the syntax may be slightly different, e.g. your Moodle could be running on MariaDB. (You can find this under Site Admin > Server > Environment.) Although you are doing a SQL report, the syntax is slightly different from standard SQL, so certain syntaxes/functions might not work.
Average of ratings:Useful (2)
In reply to Quintin Seegers

Re: Configurable SQL report access logs

by Dani Márquez BiG -
Hi Quintin,

Thanks for you quick response, i've been checking everything but my log table show 0 data.
Our moodle uses MariaDB, you were right and i was trying to do the same sql query from both directly the database and from the plugin but, i can't get data from the course logs, is it even possible?