public static function get_session_messages($chatid, $sessionstart, $sessionend, $groupid = 0) {
        global $DB, $PAGE;

        $params = self::validate_parameters(self::get_session_messages_parameters(),
                                            array(
                                                'chatid' => $chatid,
                                                'sessionstart' => $sessionstart,
                                                'sessionend' => $sessionend,
                                                'groupid' => $groupid,
                                            ));
        $messages = $warnings = array();

        // Request and permission validation.
        $chat = $DB->get_record('chat', array('id' => $params['chatid']), '*', MUST_EXIST);
        list($course, $cm) = get_course_and_cm_from_instance($chat, 'chat');

        $context = context_module::instance($cm->id);
        self::validate_context($context);

        if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
            throw new moodle_exception('nopermissiontoseethechatlog', 'chat');
        }

        if (!empty($params['groupid'])) {
            $groupid = $params['groupid'];
            // Determine is the group is visible to user.
            if (!groups_group_visible($groupid, $course, $cm)) {
                throw new moodle_exception('notingroup');
            }
        } else {
            // Check to see if groups are being used here.
            if ($groupmode = groups_get_activity_groupmode($cm)) {
                $groupid = groups_get_activity_group($cm);
                // Determine is the group is visible to user (this is particullary for the group 0).
                if (!groups_group_visible($groupid, $course, $cm)) {
                    throw new moodle_exception('notingroup');
                }
            } else {
                $groupid = 0;
            }
        }

        $messages = chat_get_session_messages($chat->id, $groupid, $params['sessionstart'], $params['sessionend'],
            'timestamp ASC');
        if ($messages) {
            foreach ($messages as $message) {
                $exporter = new chat_message_exporter($message, array('context' => $context));
                $returneditems[] = $exporter->export($PAGE->get_renderer('core'));
            }
        }

        $result = array(
            'messages' => $messages,
            'warnings' => $warnings,
        );
        return $result;
    }