MOODLE SQL仅返回用户在同一组学生中的结果

时间:2017-11-15 04:46:10

标签: moodle

SELECT ra.id,
    DATE_FORMAT(FROM_UNIXTIME(ra.timemodified),'%d-%m-%Y') AS Enrolment_Date,
    c.fullname,
    IFNULL(cmc.completed, 0) as 'Activities Completed',
    u.firstname,
    u.lastname,
    DATE_FORMAT(FROM_UNIXTIME(cc.timecompleted),'%d-%m-%Y') AS complete_date,
    CONCAT('<a target="_new" href="%%WWWROOT%%/user/profile.php?id=',ra.userid,'">', u.firstname, ' ', u.lastname, '</a>') AS Username
FROM prefix_role_assignments AS ra
    JOIN prefix_user u ON ra.userid = u.id
    JOIN prefix_context AS ctx ON ctx.id = ra.contextid
    JOIN prefix_course c ON c.id = ctx.instanceid
    JOIN prefix_enrol e ON e.courseid = c.id
    JOIN prefix_user_enrolments ue ON ue.enrolid = e.id AND ue.userid = u.id
    LEFT JOIN prefix_course_completions cc ON cc.course = c.id AND cc.userid = u.id
    LEFT JOIN prefix_grade_items gi ON gi.courseid = c.id AND gi.itemtype = 'course'
    LEFT JOIN prefix_grade_grades g ON g.itemid = gi.id AND g.userid =u.id
    LEFT JOIN (SELECT cmc.userid, cm.course, COUNT(cmc.id) as completed FROM prefix_course_modules_completion cmc, prefix_course_modules cm WHERE cm.visible = 1 AND cmc.coursemoduleid = cm.id  AND cmc.completionstate IN ('1','2') GROUP BY cm.course, cmc.userid) cmc ON cmc.course = c.id AND cmc.userid = u.id
WHERE u.id > 0 AND (c.id IN(
        SELECT DISTINCT(ctx.instanceid) as id 
        FROM prefix_role_assignments ra JOIN prefix_context ctx ON ra.contextid = ctx.id
        WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 50 AND ra.roleid IN ('1','9','2','3','4','10'))
    OR c.id IN(
        SELECT distinct(c.id) as id FROM prefix_role_assignments ra
            JOIN prefix_context ctx ON ra.contextid = ctx.id
            JOIN prefix_course c ON c.category = ctx.instanceid
            WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 40 AND ra.roleid IN ('1','9','2','3','4','10'))
        ) AND u.deleted = 0 AND u.suspended = 0 AND u.username <> 'guest' AND c.visible = 1 AND ue.status = 0 AND e.status = 0 AND ra.roleid = '5' 
GROUP BY ra.userid, ctx.instanceid HAVING u.firstname LIKE 'student'  ESCAPE '\\'

大家好, 我在这里挣扎,我似乎无法如何只返回上述查询的结果,其中运行查询的用户(%% USERID %%)与学生共享相同的组。这是一个由许多其他查询拼凑而成的查询,除了那件事之外,它的工作完美。

我试图以各种方式解决以下问题,但无法让它在所有方面发挥作用

SELECT g.name, gm.groupid, u.firstname
FROM mdl_groups as g 
    JOIN mdl_groups_members as gm ON g.id = gm.groupid
    JOIN mdl_user as u ON u.id = gm.userid AND u.id = 'student', %%USERID%%'

任何帮助甚至在正确的方向上都会很好,这不是我的工作角色,但我的任务是让这样的事情发生。

感谢您阅读

1 个答案:

答案 0 :(得分:0)

似乎子查询归结为一些userid,但这需要返回行。

        SELECT DISTINCT
             gms.userid
        FROM mdl_groups_members gms
        INNER JOIN mdl_groups_members AS gmu ON gms.groupid = gmu.groupid
        WHERE gmu.userid = %%USERID%%
        AND gms.userid LIKE 'student%'

如果确实如此,则需要链接到正确的位置。现在他的安静是一个飞跃,但它“可能”以这种方式使用:(不推荐选择*,只是缩写):

SELECT
      *
FROM prefix_role_assignments AS ra
INNER JOIN prefix_user u ON ra.userid = u.id
INNER JOIN (
        SELECT DISTINCT
             gms.userid
        FROM mdl_groups_members gms
        INNER JOIN mdl_groups_members AS gmu ON gms.groupid = gmu.groupid
        WHERE gmu.userid = %%USERID%%
        AND gms.userid LIKE 'student%'
           ) sg ON ra.userid = sg.userid
INNER JOIN prefix_context AS ctx ON ctx.id = ra.contextid
INNER JOIN prefix_course c ON c.id = ctx.instanceid
INNER JOIN prefix_enrol e ON e.courseid = c.id
INNER JOIN prefix_user_enrolments ue ON ue.enrolid = e.id AND ue.userid = u.id
LEFT JOIN prefix_course_completions cc ON cc.course = c.id AND cc.userid = u.id
LEFT JOIN prefix_grade_items gi ON gi.courseid = c.id AND gi.itemtype = 'course'
LEFT JOIN prefix_grade_grades g ON g.itemid = gi.id AND g.userid = u.id
LEFT JOIN (
      SELECT
            cmc.userid
          , cm.course
          , COUNT(cmc.id) AS completed
      FROM prefix_course_modules_completion cmc
         , prefix_course_modules cm
      WHERE cm.visible = 1
      AND cmc.coursemoduleid = cm.id
      AND cmc.completionstate IN ('1', '2')
      GROUP BY
            cm.course
          , cmc.userid
      ) cmc ON cmc.course = c.id AND cmc.userid = u.id
WHERE u.id > 0
AND (
c.id IN (
      SELECT DISTINCT
            (ctx.instanceid) AS id
      FROM prefix_role_assignments ra
      INNER JOIN prefix_context ctx ON ra.contextid = ctx.id
      WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 50
      AND ra.roleid IN ('1', '9', '2', '3', '4', '10')
)
OR c.id IN (
      SELECT DISTINCT
            (c.id) AS id
      FROM prefix_role_assignments ra
      INNER JOIN prefix_context ctx ON ra.contextid = ctx.id
      INNER JOIN prefix_course c ON c.category = ctx.instanceid
      WHERE ra.userid = %%USERID%% AND ctx.contextlevel = 40
      AND ra.roleid IN ('1', '9', '2', '3', '4', '10')
)
)
AND u.deleted = 0
AND u.suspended = 0
AND u.username <> 'guest'
AND c.visible = 1
AND ue.STATUS = 0
AND e.STATUS = 0
AND ra.roleid = '5'
GROUP BY
      ra.userid
    , ctx.instanceid
HAVING u.firstname LIKE 'student%' ESCAPE '\\'