努力对Moodle数据库表“ mdl_logstore_standard_log”进行正确的查询,以便从GUI中导出日志

时间:2018-10-14 17:17:40

标签: logging moodle moodle-api

我的问题是我试图从Moodle数据库“ mdl_logstore_standard_log”中导出csv文件,其确切顺序与“站点管理”>“报告”下的“日志”页面相同。

The Moodle GUI report fields order

实现这种目标的一种方法是通过如下查询:

SELECT 
FROM_UNIXTIME(l.timecreated,'%d %b, %H:%i') AS `Time`,
CONCAT(u.firstname, ' ', u.lastname) AS `User full name`,
CONCAT(au.firstname, ' ', au.lastname) AS `Affected user`,
l.origin AS `Origin`,
l.ip AS `IP address`
FROM mdl_logstore_standard_log AS l
LEFT JOIN mdl_user AS u ON u.id = l.userid
LEFT JOIN mdl_user AS au ON au.id = l.relateduserid

不幸的是,上面的查询没有达到我的预期。我需要来自moodle的GUI报告中的确切数据。有什么主意,如果此查询已经在穆德的php文件中实现,或者以前有人尝试过?

1 个答案:

答案 0 :(得分:1)

Web表单不依赖单个查询,而是依赖一系列查询和PHP代码来创建日志报告。如果您有兴趣,我在这里记录了两个这样的系列:Todays LogsAll Logs

日志中的大多数查询专用于查看权限等。主要部分可以在末尾找到,大致从此处开始:

SELECT COUNT('x') FROM mdl_course
SELECT id,shortname,fullname,category FROM mdl_course   ORDER BY fullname
SELECT u.id, u.firstnamephonetic,u.lastnamephonetic,u.middlename,u.alternatename,u.firstname,u.lastname
              FROM mdl_user u
              JOIN (SELECT DISTINCT eu1_u.id
              FROM mdl_user eu1_u                
             WHERE 1 = 1 AND eu1_u.deleted = 0) je ON je.id = u.id
             WHERE u.deleted = 0 ORDER BY u.lastname, u.firstname, u.id LIMIT 0, 1001
SELECT COUNT('x') FROM mdl_logstore_standard_log WHERE anonymous = 0
SELECT * FROM mdl_logstore_standard_log WHERE anonymous = 0 ORDER BY timecreated DESC, id DESC LIMIT 0, 100
SELECT id,firstnamephonetic,lastnamephonetic,middlename,alternatename,firstname,lastname FROM mdl_user WHERE id = '2'
SELECT * FROM mdl_logstore_standard_log WHERE anonymous = 0 ORDER BY timecreated DESC, id DESC LIMIT 0, 100
SELECT * FROM mdl_user WHERE id = '2' AND deleted = '0'

就是这样。 因此,基本上,您正在正确地进行操作。但是,您需要自己打印其余的列,即contextidcomponenteventnameother

SELECT FROM_UNIXTIME(l.timecreated, '%d %b, %H:%i') AS `Time`
    ,CONCAT (u.firstname, ' ', u.lastname) AS `User FULL name`
    ,CONCAT (au.firstname, ,' ', au.lastname) AS `Affected user`
    ,l.contextid AS EventContext
    ,l.component
    ,l.eventname
    ,other AS description
    ,l.origin AS `Origin`
    ,l.ip AS `IP address`
FROM mdl_logstore_standard_log AS l
LEFT JOIN mdl_user AS u ON u.id = l.userid
LEFT JOIN mdl_user AS au ON au.id = l.relateduserid
WHERE l.anonymous = 0
ORDER BY l.timecreated DESC
    ,l.id DESC

如果这与自动化有关,而不仅仅是报告,我将避免繁琐的翻译,而只处理此处选择的原始数据。