protected function load_module_instances() {
        global $DB;

        $this->moduleinstances = [];
        $modulestoload = [];
        foreach ($this->events as $event) {
            if ($module = $event->get_course_module()) {
                $id = $module->get('instance');
                $name = $module->get('modname');

                $ids = isset($modulestoload[$name]) ? $modulestoload[$name] : [];
                $ids[$id] = true;
                $modulestoload[$name] = $ids;
            }
        }

        if (empty($modulestoload)) {
            return;
        }

        foreach ($modulestoload as $modulename => $ids) {
            list($idsql, $params) = $DB->get_in_or_equal(array_keys($ids));
            $sql = "SELECT * FROM {" . $modulename . "} WHERE id {$idsql}";
            $this->moduleinstances[$modulename] = $DB->get_records_sql($sql, $params);
        }
    }