(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id = '1'
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '7'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )
UNION
(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id IN ('59410','59412','59456','59458','59618','59628','59660','59728','59850','60002','60286','60404','60406','60456','60558','60708','60880','60886','61012','61410','61542','61720','61770','62390','62396','62724','62726','62728','64148','67350','67352','67354','67356','67358','87484','113734','113778','128037','150832','150833','152470','152751','152866','153082','154891','154898','187297','199288','219260','219261','219262','219263','219264','219265','219354','219355','219356','219357','219358','219965','241301','246320','270866','270867','270923','270952','270953','270956','270974','270975','270990','271056','271101','271130','271172','271173','271256','271294','271486','272028','272741','272832','272835','279915','281490','286578','315523','329679','329684','329689','329694','330114','330123','332128','332519','332524','335087','335197','335222','335332','335342','335352','335888','378398','385575','385580','385585','385630','385635','385640','385715','385720','385725','385775','385780','385785','385790','385795','385800','385850','385855','385860','385925','385930','385935','385940','388195','388200','388205','388210','388215','389362','389367','389372','402865','402870','402875','402880','402885','402890','402895','402900','402905','405455','405460','405465','405485','405490','405495','405635','405640','405645','405730','405735','405740','405760','405765','405770','405885','405890','405895','405900','405905','446800','446805','446810','446815','446825','465606','466459','466464','469934','469939','469944','479682')
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '3'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )
UNION
(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id IN ('60034','128037')
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '38'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )
UNION
(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id IN ('60212','60248','113682','113858','152866','174658','188007','241301','271347','272606','272607','272717','272718','272737','272778','314947','327692','327908')
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '26'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )
UNION
(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id IN ('70318','79262','79628','79722','79774','79786','79798','87484','128037','246696','272833','272834','272835','272836','272837','309090','317207','375785')
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '5'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )
UNION
(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id IN ('152272','175927','287849')
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '28'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )
UNION
(SELECT ctx.path, rc.roleid, rc.capability, rc.permission
                     FROM mdl_role_capabilities rc
                     JOIN mdl_context ctx
                          ON (ctx.id = rc.contextid)
                     JOIN mdl_context pctx
                          ON (pctx.id IN ('322261','374313')
                              AND (ctx.id = pctx.id
                                   OR ctx.path LIKE CONCAT(pctx.path, '/%')
                                   OR pctx.path LIKE CONCAT(ctx.path, '/%')))
                LEFT JOIN mdl_block_instances bi
                          ON (ctx.contextlevel = 80 AND bi.id = ctx.instanceid)
                LEFT JOIN mdl_context bpctx
                          ON (bpctx.id = bi.parentcontextid)
                    WHERE rc.roleid = '68'
                          AND (ctx.contextlevel <= 50 OR bpctx.contextlevel < 50)
                   )ORDER BY capability;
 -- 2994 row(s) returned. Duration / Fetch: 7.073 sec / 0.0015 sec