original_string: 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();
          }
      }
code: 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();
          }
      }
code_tokens: 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 ( ) ; } }
docstring: Add a new profile field or save changes to current field
  @param array|stdClass $data from the add/edit profile field form
docstring_tokens: Add a new profile field or save changes to current field