public function get_context(event_interface $event) {
        global $USER;

        $courseid = $event->get_course() ? $event->get_course()->get('id') : null;
        $groupid = $event->get_group() ? $event->get_group()->get('id') : null;
        $userid = $event->get_user() ? $event->get_user()->get('id') : null;
        $moduleid = $event->get_course_module() ? $event->get_course_module()->get('id') : null;

        if (!empty($courseid)) {
            return \context_course::instance($event->get_course()->get('id'));
        } else if (!empty($groupid)) {
            $group = $this->get_group($event);
            return \context_course::instance($group->courseid);
        } else if (!empty($userid) && $userid == $USER->id) {
            return \context_user::instance($userid);
        } else if (!empty($userid) && $userid != $USER->id && $moduleid && $moduleid > 0) {
            $cm = $this->get_course_module($event);
            return \context_course::instance($cm->course);
        } else {
            return \context_user::instance($userid);
        }
    }