/** * Returns an array of joins, wheres and params that will limit the group of * users to only those enrolled and with given capability (if specified). * * @param context $context * @param string $prefix optional, a prefix to the user id column * @param string|array $capability optional, may include a capability name, or array of names. * If an array is provided then this is the equivalent of a logical 'OR', * i.e. the user needs to have one of these capabilities. * @param int $group optional, 0 indicates no current group, otherwise the group id * @param bool $onlyactive consider only active enrolments in enabled plugins and time restrictions * @param bool $onlysuspended inverse of onlyactive, consider only suspended enrolments * @return \core\dml\sql_join Contains joins, wheres, params */ function get_enrolled_with_capabilities_join(context $context, $prefix = '', $capability = '', $group = 0, $onlyactive = false, $onlysuspended = false) { $uid = $prefix . 'u.id'; $joins = array(); $wheres = array(); $enrolledjoin = get_enrolled_join($context, $uid, $onlyactive, $onlysuspended); $joins[] = $enrolledjoin->joins; $wheres[] = $enrolledjoin->wheres; $params = $enrolledjoin->params; if (!empty($capability)) { $capjoin = get_with_capability_join($context, $capability, $uid); $joins[] = $capjoin->joins; $wheres[] = $capjoin->wheres; $params = array_merge($params, $capjoin->params); } if ($group) { $groupjoin = groups_get_members_join($group, $uid); $joins[] = $groupjoin->joins; $params = array_merge($params, $groupjoin->params); } $joins = implode("\n", $joins); $wheres[] = "{$prefix}u.deleted = 0"; $wheres = implode(" AND ", $wheres); return new \core\dml\sql_join($joins, $wheres, $params); }
/** * Gets sql for finding users with a capability in the given context * * @param context $context * @param string $capability * @return array($sql, $params) */ function get_with_capability_sql(context $context, $capability) { static $i = 0; $i++; $prefix = 'cu' . $i . '_'; $capjoin = get_with_capability_join($context, $capability, $prefix . 'u.id'); $sql = "SELECT DISTINCT {$prefix}u.id\n FROM {user} {$prefix}u\n {$capjoin->joins}\n WHERE {$prefix}u.deleted = 0 AND {$capjoin->wheres}"; return array($sql, $capjoin->params); }