CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • CiviCRM Group Roles re-adding a contact to a group when deleted from the group
Pages: [1]

Author Topic: CiviCRM Group Roles re-adding a contact to a group when deleted from the group  (Read 631 times)

flug

  • I post frequently
  • ***
  • Posts: 126
  • Karma: 12
CiviCRM Group Roles re-adding a contact to a group when deleted from the group
January 23, 2015, 06:37:36 am
Running CiviCRM 4.2.X & Drupal 6.X I ran into an issue with CivicRM Group Roles.

If you have a Drupal role & CiviCRM group set up to sync in the CiviCRM Group Roles module, when you 'disable' a contact from a group, the contact is removed from the corresponding Drupal role as expected.

However, if you DELETE the contact from the group, the API immediately adds the contact back into the group and also then syncs the contact back into the corresponding Drupal role.

Naturally, this causes a bit of frustration!

This is fixed in later version of the CiviCRM Group Roles module.  Here is the solution backported, for anyone who may still be running the earlier version of CiviCRM Group Roles.  (I think you could also just upgrade the CiviCRM Group Roles module to the latest version; I'm just not exactly sure where those pieces could be downloaded from.)

Look in civicrm_group_roles.module - around line 182 you'll see some code that looks about like this:
Code: [Select]
      switch ($op) {
        case 'create':
        case 'edit':
          //Contact added or re-joined to group; add to corresponding role
          civicrm_group_roles_add_role($uid, $rid, $role_name);
          break;

        case 'delete':

Replace those lines with this:

Code: [Select]
      switch ($op) {
        case 'create':
          civicrm_group_roles_add_role($uid, $rid, $role_name);
          break;
       
        case 'edit':
          //'edit' may mean added OR it may mean deleted.  Take the appropriate action depending on which has happened.
          // Ideally would use GroupContact API with group_id as a parameter, currently broken (amend once bug fixed):
          // http://issues.civicrm.org/jira/browse/CRM-10331?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
          $contact_groups = civicrm_api('GroupContact', 'get', array(version => '3', 'contact_id' => $contact_id));
          $in_group = FALSE;
          foreach ($contact_groups['values'] as $group_row){
            if ($group_row['group_id'] == $group_id){
              $in_group = TRUE;
              break;
            }
          }

          if ($in_group){
            // Contact rejoined the group; add to corresponding role
            civicrm_group_roles_add_role($uid, $rid, $role_name);
          } else {
            // Contact no longer belongs to group, remove corresponding role,
            // but only if the contact is in no other groups that grant this role
            if (!civicrm_role_granted_by_other_group($contact_id, $rid, $group_id)) {
              civicrm_group_roles_del_role($uid, $rid, $role_name);
            }
          }
          break; 

        case 'delete':

Full patched civicrm_group_roles.module file attached (rename to civicrm_group_roles.module, backup the existing file with the same name, and then just copy this new file over the existing file with the same name).  Again it would probably be better to simply upgrade to the latest civicrm_group_roles module if possible.
« Last Edit: January 23, 2015, 06:44:14 am by flug »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • CiviCRM Group Roles re-adding a contact to a group when deleted from the group

This forum was archived on 2017-11-26.