LMSouq
moodle-core Open

MOODLE SQL only return results where user is in same group student

RI
Rivvered
1 month ago
3 views
Problem Description
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 '\\' Hi all, Im struggling here, I cant seem to get how to only return results from the above query where the user running the query (%%USERID%%) shares the same groups as the student. This is a query cobbled together from a number of other queries and works perfectly except for that one thing. Ive tried to tack on the following in various ways into this but cant get it to work at all 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%%&#...

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base