public function from_legacy_event_to_data(\calendar_event $legacyevent) {
        $legacyevent->count_repeats();
        $data = $legacyevent->properties();
        $data->timedurationuntil = $legacyevent->timestart + $legacyevent->timeduration;
        $data->duration = (empty($legacyevent->timeduration)) ? 0 : 1;

        if ($legacyevent->eventtype == 'group') {
            // Set up the correct value for the to display on the form.
            $data->groupid = $legacyevent->groupid;
            $data->groupcourseid = $legacyevent->courseid;
        }
        if ($legacyevent->eventtype == 'course') {
            // Set up the correct value for the to display on the form.
            $data->courseid = $legacyevent->courseid;
        }
        $data->description = [
            'text' => $data->description,
            'format' => $data->format
        ];

        // We don't want to return the context because it's not a
        // form value and breaks the validation.
        if (isset($data->context)) {
            unset($data->context);
        }

        return $data;
    }