private function get_condition_sql($field, $field2 = null, $istext = false) {
        global $DB;
        if (is_null($field2)) {
            $field2 = $field;
        }

        $params = array();
        switch($this->operator) {
            case self::OP_CONTAINS:
                $sql = $DB->sql_like($field, self::unique_sql_parameter(
                        $params, '%' . $this->value . '%'));
                break;
            case self::OP_DOES_NOT_CONTAIN:
                if (empty($this->value)) {
                    // The 'does not contain nothing' expression matches everyone.
                    return null;
                }
                $sql = $DB->sql_like($field, self::unique_sql_parameter(
                        $params, '%' . $this->value . '%'), true, true, true);
                break;
            case self::OP_IS_EQUAL_TO:
                if ($istext) {
                    $sql = $DB->sql_compare_text($field) . ' = ' . $DB->sql_compare_text(
                            self::unique_sql_parameter($params, $this->value));
                } else {
                    $sql = $field . ' = ' . self::unique_sql_parameter(
                            $params, $this->value);
                }
                break;
            case self::OP_STARTS_WITH:
                $sql = $DB->sql_like($field, self::unique_sql_parameter(
                        $params, $this->value . '%'));
                break;
            case self::OP_ENDS_WITH:
                $sql = $DB->sql_like($field, self::unique_sql_parameter(
                        $params, '%' . $this->value));
                break;
            case self::OP_IS_EMPTY:
                // Mimic PHP empty() behaviour for strings, '0' or ''.
                $emptystring = self::unique_sql_parameter($params, '');
                if ($istext) {
                    $sql = '(' . $DB->sql_compare_text($field) . " IN ('0', $emptystring) OR $field2 IS NULL)";
                } else {
                    $sql = '(' . $field . " IN ('0', $emptystring) OR $field2 IS NULL)";
                }
                break;
            case self::OP_IS_NOT_EMPTY:
                $emptystring = self::unique_sql_parameter($params, '');
                if ($istext) {
                    $sql = '(' . $DB->sql_compare_text($field) . " NOT IN ('0', $emptystring) AND $field2 IS NOT NULL)";
                } else {
                    $sql = '(' . $field . " NOT IN ('0', $emptystring) AND $field2 IS NOT NULL)";
                }
                break;
        }
        return array($sql, $params);
    }