public function enable($timesplittingid = false) {
        global $DB, $USER;

        $now = time();

        if ($timesplittingid && $timesplittingid !== $this->model->timesplitting) {

            if (!\core_analytics\manager::is_valid($timesplittingid, '\core_analytics\local\time_splitting\base')) {
                throw new \moodle_exception('errorinvalidtimesplitting', 'analytics');
            }

            if (substr($timesplittingid, 0, 1) !== '\\') {
                throw new \moodle_exception('errorinvalidtimesplitting', 'analytics');
            }

            // Delete generated predictions before changing the model version.
            $this->clear();

            // It needs to be reset as the version changes.
            $this->uniqueid = null;

            $this->model->timesplitting = $timesplittingid;
            $this->model->version = $now;

            // Reset trained flag.
            if (!$this->is_static()) {
                $this->model->trained = 0;
            }
        } else if (empty($this->model->timesplitting)) {
            // A valid timesplitting method needs to be supplied before a model can be enabled.
            throw new \moodle_exception('invalidtimesplitting', 'analytics', '', $this->model->id);

        }

        // Purge pages with insights as this may change things.
        if ($this->model->enabled != 1) {
            $this->purge_insights_cache();
        }

        $this->model->enabled = 1;
        $this->model->timemodified = $now;
        $this->model->usermodified = $USER->id;

        // We don't always update timemodified intentionally as we reserve it for target, indicators or timesplitting updates.
        $DB->update_record('analytics_models', $this->model);
    }