public function define_save($data) {
        global $DB;

        $data = $this->define_save_preprocess($data); // Hook for child classes.

        $old = false;
        if (!empty($data->id)) {
            $old = $DB->get_record('user_info_field', array('id' => (int)$data->id));
        }

        // Check to see if the category has changed.
        if (!$old or $old->categoryid != $data->categoryid) {
            $data->sortorder = $DB->count_records('user_info_field', array('categoryid' => $data->categoryid)) + 1;
        }

        if (empty($data->id)) {
            unset($data->id);
            $data->id = $DB->insert_record('user_info_field', $data);
        } else {
            $DB->update_record('user_info_field', $data);
        }

        $field = $DB->get_record('user_info_field', array('id' => $data->id));
        if ($old) {
            \core\event\user_info_field_updated::create_from_field($field)->trigger();
        } else {
            \core\event\user_info_field_created::create_from_field($field)->trigger();
        }
    }