Example #1
0
/**
 * Get sql and parameters that will return user ids for a group
 *
 * @param int $groupid
 * @return array($sql, $params)
 */
function groups_get_members_ids_sql($groupid)
{
    $groupjoin = groups_get_members_join($groupid, 'u.id');
    $sql = "SELECT DISTINCT u.id\n              FROM {user} u\n            {$groupjoin->joins}\n             WHERE u.deleted = 0";
    return array($sql, $groupjoin->params);
}
Example #2
0
/**
 * 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);
}