1

I manage a basketball league site with two main user roles, "commissioner" and "player". Every player on the site is assigned to both a Tuesday team (field_team) and a Friday team (field_team2) via a user field, which is editable by commissioners. Because of player absences and other variables, the teams are usually different by the end of the week, so there are also two additional fields, Tuesday default team (field_tuesday_default) and Friday default team (field_friday_default).

See below for an example of what my Tuesday games page looks like to commissioners:

tuesday games page

We start each week with a clean slate, i.e., every player back with their default team. Currently, at the end of each week our commissioners have to manually edit every player's user account to get their Tuesday/Friday team values back to the default.

See below for what a player's edit account page looks like to commissioners:

enter image description here

The problem is that it takes a long time and it is very tedious for commissioners to manually edit every player's user account to make their team values equal to their default team values.

Is there a way to programmatically change every user in the "player" role's Tuesday team and Friday team field values to equal their Tuesday default team and Friday default team values? All my research thus far has come up empty and I'm really at a loss at this point. Ideally, I would like to put a nice, big reset button on the Tuesday/Friday games pages that will perform the reset. Any help would be appreciated! Thanks.

1 Answer 1

0

Yes

Programmatically you have to create a custom form with submit button as the only field, now inside the submit handler it will be 2 step process something as below

following code I have written just to give an idea, and assuming field names in a way you can relate. You have to do the tweaks to make it work for you

/**
  * Form Builder
 */
function _my_module_reset_team_selection($form, &$form_state) {
  $form = array();
  $form['reset_options'] = array(
    '#type' => 'select',
    '#title' => 'Reset Options'
    '#options' => array(
      'both' => 'Reset Both Teams',
      'ft'   => 'Reset Friday Team',
      'tt'   => 'Reset Tuesday Team',
    )
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Reset',
  );
}

/**
 * Submit Handler for your custom form.
 * 2 step process 1. Find Users to Update. 2. Update :)
 */
function _my_module_reset_team_selection_submit($form, $form_state) {

  // Logic to decide if to reset both or any one
  // possible values as 'both'/'ft'/'tt'
  $reset = $form_state['input']['reset_options'];
  // Step1. Get All the Users who are part of any team.
  $users_to_reset = _my_module_get_users_to_reset($reset);
  if (is_array($users_to_reset) && count($reset)) {
    // Step2. Update to Reset.
    switch ($reset) {
      case 'both':
        foreach ($users_to_reset as $user_to_reset) {
          // For better performance you can use field_attach_api here**
          // I am using user_save() process
          $user = user_load($user_to_reset);
          $user->field_tuesday[$user->lang][0]['value'] = '';
          $user->field_friday[$user->lang][0]['value'] = '';
          user_save($user);
        }

        break;
      case 'ft':
        foreach ($users_to_reset as $user_to_reset) {
          // For better performance you can use field_attach_api here**
          // I am using user_save() process
          $user = user_load($user_to_reset);
          $user->field_friday[$user->lang][0]['value'] = '';
          user_save($user);
        }
        break;
      case 'tt':
        foreach ($users_to_reset as $user_to_reset) {
          // For better performance you can use field_attach_api here**
          // I am using user_save() process
          $user = user_load($user_to_reset);
          $user->field_tuesday[$user->lang][0]['value'] = '';
          user_save($user);
        }
        break;
    }
  }
}



/**
  *  Helper Function to get list of users to reset.
  *  Allows you to reset any of two Tuesday Team or Friday Team or both.
 */
function _my_module_get_users_to_reset($reset='both') {
  $get_tt_users = array();
  $get_ft_users = array();
  if($reset != 'ft') {
    $get_tt_users = db_select('field_tuesday_team','tt')
                 ->fields('tt',array(entity_id))
                 ->condition('tt.entity_type','user',"=")
                 ->execute()
  }               ->fetchCol();
  if($reset != 'tt') {
    $get_ft_users = db_select('field_friday_team','ft')
                 ->fields('tt',array(entity_id))
                 ->condition('tt.entity_type','user',"=")
                 ->execute()
                 ->fetchCol();
  }
  $users_to_reset = array_unique(array_merge($get_tt_users,$get_ft_users));
  return $users_to_reset;
}
4
  • Thanks for taking the time to write the code. Will this need to go into a custom module or is there another way?
    – Dan
    Commented Aug 25, 2015 at 21:02
  • this need to go to a custom module.
    – arpitr
    Commented Aug 25, 2015 at 21:40
  • I created my custom module and modified it with the proper field names. However, I don't see anything in the code that will create a block for the submit button to reside in. When I try to enable the module it won't enable. Do you have any tips on this?
    – Dan
    Commented Aug 26, 2015 at 16:11
  • Appreciate the help but found another method using VBO and Rules. Thanks anyways!
    – Dan
    Commented Aug 26, 2015 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.