public static function create_conversation(int $type, array $userids, string $name = null,
            int $enabled = self::MESSAGE_CONVERSATION_ENABLED, string $component = null,
            string $itemtype = null, int $itemid = null, int $contextid = null) {

        global $DB;

        $validtypes = [
            self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
            self::MESSAGE_CONVERSATION_TYPE_GROUP,
            self::MESSAGE_CONVERSATION_TYPE_SELF
        ];

        if (!in_array($type, $validtypes)) {
            throw new \moodle_exception('An invalid conversation type was specified.');
        }

        // Sanity check.
        if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL) {
            if (count($userids) > 2) {
                throw new \moodle_exception('An individual conversation can not have more than two users.');
            }
            if ($userids[0] == $userids[1]) {
                throw new \moodle_exception('Trying to create an individual conversation instead of a self conversation.');
            }
        } else if ($type == self::MESSAGE_CONVERSATION_TYPE_SELF) {
            if (count($userids) != 1) {
                throw new \moodle_exception('A self conversation can not have more than one user.');
            }
        }

        $conversation = new \stdClass();
        $conversation->type = $type;
        $conversation->name = $name;
        $conversation->convhash = null;
        if ($type == self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL || $type == self::MESSAGE_CONVERSATION_TYPE_SELF) {
            $conversation->convhash = helper::get_conversation_hash($userids);
        }
        $conversation->component = $component;
        $conversation->itemtype = $itemtype;
        $conversation->itemid = $itemid;
        $conversation->contextid = $contextid;
        $conversation->enabled = $enabled;
        $conversation->timecreated = time();
        $conversation->timemodified = $conversation->timecreated;
        $conversation->id = $DB->insert_record('message_conversations', $conversation);

        // Add users to this conversation.
        $arrmembers = [];
        foreach ($userids as $userid) {
            $member = new \stdClass();
            $member->conversationid = $conversation->id;
            $member->userid = $userid;
            $member->timecreated = time();
            $member->id = $DB->insert_record('message_conversation_members', $member);

            $arrmembers[] = $member;
        }

        $conversation->members = $arrmembers;

        return $conversation;
    }