protected function get_qubaids_condition() {
        if (is_null($this->rawdata)) {
            throw new coding_exception(
                    'Cannot call get_qubaids_condition until the main data has been loaded.');
        }

        if ($this->is_downloading()) {
            // We want usages for all attempts.
            return new qubaid_join("(
                SELECT DISTINCT quiza.uniqueid
                  FROM " . $this->sql->from . "
                 WHERE " . $this->sql->where . "
                    ) quizasubquery", 'quizasubquery.uniqueid',
                    "1 = 1", $this->sql->params);
        }

        $qubaids = array();
        foreach ($this->rawdata as $attempt) {
            if ($attempt->usageid > 0) {
                $qubaids[] = $attempt->usageid;
            }
        }

        return new qubaid_list($qubaids);
    }