mariadb Moodle课程自定义SQL报告:选择学生,他们所属的组和特定课程的最终评估分数

rvpgvaaj  于 2023-11-19  发布在  其他
关注(0)|答案(2)|浏览(141)

👋
我对编程有一定的了解,我想尝试构建一个Moodle自定义SQL查询,但到目前为止还没有成功。
我想从用户表中选择几个字段,以及用户所属的组名和特定的测验最终成绩,所有这些都在特定课程的上下文中。
这就是我目前为止所取得的成就:

SELECT
    u.firstname AS "First Name",
    u.lastname AS "Last Name",
    u.username AS "Username",
    u.email AS "User Email",
    g.name AS "Group Name",
    CASE
        WHEN gi.grade IS NULL THEN 'Ungraded'
        ELSE gi.grade
    END AS "Quiz Grade"
FROM
    prefix_user AS u
INNER JOIN
    prefix_user_enrolments AS ue ON u.id = ue.userid
INNER JOIN
    prefix_enrol AS e ON ue.enrolid = e.id
INNER JOIN
    prefix_course AS c ON e.courseid = c.id
LEFT JOIN
    prefix_groups_members AS gm ON u.id = gm.userid
LEFT JOIN
    prefix_groups AS g ON gm.groupid = g.id
LEFT JOIN
    prefix_quiz AS q ON c.id = q.course
LEFT JOIN
    prefix_quiz_grades AS gi ON q.id = gi.id AND u.id = gi.userid
INNER JOIN
    prefix_role_assignments AS ra ON ra.userid = u.id
INNER JOIN
    prefix_context AS ctx ON ra.contextid = ctx.id
INNER JOIN
    prefix_role AS r ON ra.roleid = r.id
WHERE
    c.id = %%COURSEID%%
    AND q.name = 'Evaluación final'
    AND r.shortname = 'student'
ORDER BY
    u.lastname, u.firstname

字符串
问题是,我得到了很多重复的值(quiz设置为只有一次尝试),其中一些具有不同的组名称值(课程不重用)。
我真的很感激你能提供的任何纠正/帮助。
谢谢你提前!🙌

snz8szmq

snz8szmq1#

你会得到很多重复的几个原因
用户可以在同一课程中注册多次-可以手动注册,也可以通过其他注册方法注册
您可能希望通过使用EXISTS运算符而不是JOIN来缩小结果范围
例如

WHERE EXISTS (
    SELECT ue.id
    FROM prefix_enrol AS e ON e.courseid = c.id
    JOIN prefix_user_enrolments AS ue ON ue.enrolid = e.id AND ue.userid = u.id
)

字符串
如果用户已注册课程,则无论注册方法如何,此操作都返回true
他们也可以有一个以上的角色,他们可以是一个教师和学生,例如
而且也可以在一个以上的组在一个课程
因此,对于这两个,使用一个组concat函数
如果你使用的是PHP,那么有一个现有的Moodle函数可以返回正确的兼容SQL

$groupconcatsql = $DB->sql_group_concat('tablename.columnname');


或者,如果您在外部使用SQL,请检查数据库中的组联系功能
例如MySQL
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
使用类似于

LEFT JOIN (
    SELECT gm.userid, g.courseid, GROUP_CONCAT(g.name)
    FROM prefix_groups_members AS gm
    JOIN prefix_groups AS g ON g.id = gm.groupid
    GROUP BY gm.userid, g.courseid
) usergroups ON usergroups.userid = u.id AND usergroups.courseid = c.id


然后使用SELECT usergroups.name
请注意,您还需要将组加入到课程中--这是出现大量重复内容的另一个原因
Moodle网站上有很多SQL查询的例子
https://docs.moodle.org/403/en/ad-hoc_contributed_reports#COURSE_COMPLETION_and_GRADE_REPORTS

wtlkbnrh

wtlkbnrh2#

您的方向是正确的,但由于多个角色分配、组成员身份等原因,它可能会返回重复的值。您应该使用DISTINCT关键字来选择唯一的行,并确保正确地连接表。
你可以试试这个

SELECT DISTINCT
    u.firstname AS "First Name",
    u.lastname AS "Last Name",
    u.username AS "Username",
    u.email AS "User Email",
    g.name AS "Group Name",
    CASE
        WHEN gi.grade IS NULL THEN 'Ungraded'
        ELSE CAST(gi.grade AS CHAR)
    END AS "Quiz Grade"
FROM
    prefix_user AS u
INNER JOIN
    prefix_user_enrolments AS ue ON u.id = ue.userid
INNER JOIN
    prefix_enrol AS e ON ue.enrolid = e.id AND e.courseid = %%COURSEID%%
LEFT JOIN
    prefix_groups_members AS gm ON u.id = gm.userid
LEFT JOIN
    prefix_groups AS g ON gm.groupid = g.id
LEFT JOIN
    prefix_quiz AS q ON e.courseid = q.course AND q.name = 'Evaluación final'
LEFT JOIN
    prefix_quiz_grades AS gi ON q.id = gi.quiz AND u.id = gi.userid
INNER JOIN
    prefix_role_assignments AS ra ON ra.userid = u.id
INNER JOIN
    prefix_context AS ctx ON ra.contextid = ctx.id AND ctx.instanceid = %%COURSEID%%
INNER JOIN
    prefix_role AS r ON ra.roleid = r.id AND r.shortname = 'student'
ORDER BY
    u.lastname, u.firstname

字符串
在这里,您还将等级转换为CASE语句中的字符类型,以便在结果中具有相同的数据类型。

相关问题