protected static function get_users_sql_and_params($context, $search = '', $count = false) {

        // Fields we need from the user table.
        $extrafields = get_extra_user_fields($context);
        $params = array();
        if (!empty($search)) {
            list($filtersql, $params) = users_search_sql($search, 'u', true, $extrafields);
            $filtersql .= ' AND ';
        } else {
            $filtersql = '';
        }

        $ufields = \user_picture::fields('u', $extrafields).',u.username';
        if ($count) {
            $select = "SELECT COUNT(DISTINCT u.id) ";
            $orderby = "";
        } else {
            $select = "SELECT DISTINCT $ufields ";
            $orderby = " ORDER BY u.lastname ASC, u.firstname ASC";
        }
        $sql = "$select
                 FROM {user} u
                 JOIN {grade_grades_history} ggh ON u.id = ggh.userid
                 JOIN {grade_items} gi ON gi.id = ggh.itemid
                WHERE $filtersql gi.courseid = :courseid";
        $sql .= $orderby;
        $params['courseid'] = $context->instanceid;

        return array($sql, $params);
    }