private static function getInfoByBranch($campaign, $branchId, $activities) {
    $connection = \Drupal::service('database');

    /** @var \Drupal\Core\Database\Query\Select $query */
    $query = $connection->select('openy_campaign_member', 'm');

    $query->join('openy_campaign_member_campaign', 'mc', 'm.id = mc.member');
    $query->join('openy_campaign_memb_camp_actv', 'mca', 'mc.id = mca.member_campaign');

    $query->addField('m', 'id', 'member');
    $query->addField('mca', 'activity');
    $query->addField('mca', 'member_campaign');

    $query->condition('m.branch', $branchId);
    $query->condition('m.is_employee', FALSE);
    $query->condition('mc.campaign', $campaign->id());

    $results = $query->execute()->fetchAll();

    $mainActivities = array_keys($activities);

    foreach ($results as $item) {
      $memberCampaignsInfo[$item->member_campaign]['member_campaign'] = $item->member_campaign;
      foreach ($mainActivities as $category) {
        $allItems = $activities[$category];
        if (in_array($item->activity, $allItems)) {
          if (empty($memberCampaignsInfo[$item->member_campaign]['activity_visits'][$category])) {
            $memberCampaignsInfo[$item->member_campaign]['activity_visits'][$category] = 1;
          }
          else {
            ++$memberCampaignsInfo[$item->member_campaign]['activity_visits'][$category];
          }
          break;
        }
      }
    }

    // Get main activity for each MemberCampaign and collect result array.
    $resultInfo = [];
    foreach ($memberCampaignsInfo as &$mcData) {
      // Get only members who tracked activities on site.
      if (!empty($mcData['activity_visits'])) {
        $mcData['main_activity'] = array_search(max($mcData['activity_visits']), $mcData['activity_visits']);
      }

      // Collect result array grouped by activity.
      if (!empty($mcData['activity_visits'])) {
        foreach ($mcData['activity_visits'] as $cat => $count) {
          if ($count > 0) {
            $resultInfo[$cat][] = $mcData;
          }
        }
      }
    }

    return $resultInfo;
  }
private static function getInfoByBranch($campaign, $branchId, $activities) {
    $connection = \Drupal::service('database');

    /** @var \Drupal\Core\Database\Query\Select $query */
    $query = $connection->select('openy_campaign_member', 'm');

    $query->join('openy_campaign_member_campaign', 'mc', 'm.id = mc.member');
    $query->join('openy_campaign_memb_camp_actv', 'mca', 'mc.id = mca.member_campaign');

    $query->addField('m', 'id', 'member');
    $query->addField('mca', 'activity');
    $query->addField('mca', 'member_campaign');

    $query->condition('m.branch', $branchId);
    $query->condition('m.is_employee', FALSE);
    $query->condition('mc.campaign', $campaign->id());

    $results = $query->execute()->fetchAll();

    $mainActivities = array_keys($activities);

    foreach ($results as $item) {
      $memberCampaignsInfo[$item->member_campaign]['member_campaign'] = $item->member_campaign;
      foreach ($mainActivities as $category) {
        $allItems = $activities[$category];
        if (in_array($item->activity, $allItems)) {
          if (empty($memberCampaignsInfo[$item->member_campaign]['activity_visits'][$category])) {
            $memberCampaignsInfo[$item->member_campaign]['activity_visits'][$category] = 1;
          }
          else {
            ++$memberCampaignsInfo[$item->member_campaign]['activity_visits'][$category];
          }
          break;
        }
      }
    }

    // Get main activity for each MemberCampaign and collect result array.
    $resultInfo = [];
    foreach ($memberCampaignsInfo as &$mcData) {
      // Get only members who tracked activities on site.
      if (!empty($mcData['activity_visits'])) {
        $mcData['main_activity'] = array_search(max($mcData['activity_visits']), $mcData['activity_visits']);
      }

      // Collect result array grouped by activity.
      if (!empty($mcData['activity_visits'])) {
        foreach ($mcData['activity_visits'] as $cat => $count) {
          if ($count > 0) {
            $resultInfo[$cat][] = $mcData;
          }
        }
      }
    }

    return $resultInfo;
  }
private static function getInfoByBranch ( $ campaign , $ branchId , $ activities ) { $ connection = \ Drupal :: service ( 'database' ) ; /** @var \Drupal\Core\Database\Query\Select $query */ $ query = $ connection -> select ( 'openy_campaign_member' , 'm' ) ; $ query -> join ( 'openy_campaign_member_campaign' , 'mc' , 'm.id = mc.member' ) ; $ query -> join ( 'openy_campaign_memb_camp_actv' , 'mca' , 'mc.id = mca.member_campaign' ) ; $ query -> addField ( 'm' , 'id' , 'member' ) ; $ query -> addField ( 'mca' , 'activity' ) ; $ query -> addField ( 'mca' , 'member_campaign' ) ; $ query -> condition ( 'm.branch' , $ branchId ) ; $ query -> condition ( 'm.is_employee' , FALSE ) ; $ query -> condition ( 'mc.campaign' , $ campaign -> id ( ) ) ; $ results = $ query -> execute ( ) -> fetchAll ( ) ; $ mainActivities = array_keys ( $ activities ) ; foreach ( $ results as $ item ) { $ memberCampaignsInfo [ $ item -> member_campaign ] [ 'member_campaign' ] = $ item -> member_campaign ; foreach ( $ mainActivities as $ category ) { $ allItems = $ activities [ $ category ] ; if ( in_array ( $ item -> activity , $ allItems ) ) { if ( empty ( $ memberCampaignsInfo [ $ item -> member_campaign ] [ 'activity_visits' ] [ $ category ] ) ) { $ memberCampaignsInfo [ $ item -> member_campaign ] [ 'activity_visits' ] [ $ category ] = 1 ; } else { ++ $ memberCampaignsInfo [ $ item -> member_campaign ] [ 'activity_visits' ] [ $ category ] ; } break ; } } } // Get main activity for each MemberCampaign and collect result array. $ resultInfo = [ ] ; foreach ( $ memberCampaignsInfo as & $ mcData ) { // Get only members who tracked activities on site. if ( ! empty ( $ mcData [ 'activity_visits' ] ) ) { $ mcData [ 'main_activity' ] = array_search ( max ( $ mcData [ 'activity_visits' ] ) , $ mcData [ 'activity_visits' ] ) ; } // Collect result array grouped by activity. if ( ! empty ( $ mcData [ 'activity_visits' ] ) ) { foreach ( $ mcData [ 'activity_visits' ] as $ cat => $ count ) { if ( $ count > 0 ) { $ resultInfo [ $ cat ] [ ] = $ mcData ; } } } } return $ resultInfo ; }
Get all needed data for current campaign.

@param \Drupal\node\Entity\Node $campaign
Campaign node entity.
@param int $branchId
Branch ID to calculate winners for.
@param array $activities
Array of arrays with all activities for current Campaign.
Key - parent activity, value - array of child ones.

@return array
Get all needed data for current campaign .