public static function processCampaignWinnersBatch($campaign, $branches, $activities, $places, $isVisitsGoal, &$context) {
    if (empty($context['sandbox'])) {
      $context['sandbox']['progress'] = 0;

      $context['sandbox']['campaign'] = $campaign;
      $context['sandbox']['activities'] = $activities;
      $context['sandbox']['branches'] = array_keys($branches);
      $context['sandbox']['places'] = $places;

      $context['sandbox']['max'] = count($branches);
    }

    $branchId = $context['sandbox']['branches'][$context['sandbox']['progress']];
    $campaign = $context['sandbox']['campaign'];
    $activities = $context['sandbox']['activities'];
    $places = $context['sandbox']['places'];

    // Get all member campaigns for this branch.
    $memberCampaignsInfo = self::getInfoByBranch($campaign, $branchId, $activities);

    $alreadyWinners = [];

    $activityWinners = [];
    // Assign places.
    foreach ($places as $place) {
      // Calculate winners per activity.
      foreach ($memberCampaignsInfo as $category => $data) {
        if (empty($data)) {
          break;
        }

        $memberCampaignItem = self::getRandomWinner($data, $alreadyWinners);

        // Create Winner entities.
        if (!empty($memberCampaignItem)) {
          $activityWinners[] = [
            'member_campaign' => $memberCampaignItem['member_campaign'],
            'activity' => $category,
            'place' => $place,
          ];
        }
      }
    }

    // Calculate winners by Visits goal.
    $goalWinners = [];
    if ($isVisitsGoal) {
      /** @var \Drupal\openy_campaign\CampaignMenuService $campaignMenuService */
      $campaignMenuService = \Drupal::service('openy_campaign.campaign_menu_handler');
      $goalWinnersIds = $campaignMenuService->getVisitsGoalWinners($campaign, $branchId, $alreadyWinners);
      // Create Winner entity. If winner defined by Visits goal without activity - set Activity = 0.
      foreach ($places as $place) {
        $goalWinnerId = array_shift($goalWinnersIds);
        $goalWinners[] = [
          'member_campaign' => $goalWinnerId,
          'activity' => 0,
          'place' => $place,
        ];
      }
    }

    $winners = array_merge($goalWinners, $activityWinners);

    foreach ($winners as $item) {
      $winner = Winner::create($item);
      $winner->save();

      $context['results'][] = $item['member_campaign'];
    }

    $context['sandbox']['progress']++;

    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
public static function processCampaignWinnersBatch($campaign, $branches, $activities, $places, $isVisitsGoal, &$context) {
    if (empty($context['sandbox'])) {
      $context['sandbox']['progress'] = 0;

      $context['sandbox']['campaign'] = $campaign;
      $context['sandbox']['activities'] = $activities;
      $context['sandbox']['branches'] = array_keys($branches);
      $context['sandbox']['places'] = $places;

      $context['sandbox']['max'] = count($branches);
    }

    $branchId = $context['sandbox']['branches'][$context['sandbox']['progress']];
    $campaign = $context['sandbox']['campaign'];
    $activities = $context['sandbox']['activities'];
    $places = $context['sandbox']['places'];

    // Get all member campaigns for this branch.
    $memberCampaignsInfo = self::getInfoByBranch($campaign, $branchId, $activities);

    $alreadyWinners = [];

    $activityWinners = [];
    // Assign places.
    foreach ($places as $place) {
      // Calculate winners per activity.
      foreach ($memberCampaignsInfo as $category => $data) {
        if (empty($data)) {
          break;
        }

        $memberCampaignItem = self::getRandomWinner($data, $alreadyWinners);

        // Create Winner entities.
        if (!empty($memberCampaignItem)) {
          $activityWinners[] = [
            'member_campaign' => $memberCampaignItem['member_campaign'],
            'activity' => $category,
            'place' => $place,
          ];
        }
      }
    }

    // Calculate winners by Visits goal.
    $goalWinners = [];
    if ($isVisitsGoal) {
      /** @var \Drupal\openy_campaign\CampaignMenuService $campaignMenuService */
      $campaignMenuService = \Drupal::service('openy_campaign.campaign_menu_handler');
      $goalWinnersIds = $campaignMenuService->getVisitsGoalWinners($campaign, $branchId, $alreadyWinners);
      // Create Winner entity. If winner defined by Visits goal without activity - set Activity = 0.
      foreach ($places as $place) {
        $goalWinnerId = array_shift($goalWinnersIds);
        $goalWinners[] = [
          'member_campaign' => $goalWinnerId,
          'activity' => 0,
          'place' => $place,
        ];
      }
    }

    $winners = array_merge($goalWinners, $activityWinners);

    foreach ($winners as $item) {
      $winner = Winner::create($item);
      $winner->save();

      $context['results'][] = $item['member_campaign'];
    }

    $context['sandbox']['progress']++;

    if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
public static function processCampaignWinnersBatch ( $ campaign , $ branches , $ activities , $ places , $ isVisitsGoal , & $ context ) { if ( empty ( $ context [ 'sandbox' ] ) ) { $ context [ 'sandbox' ] [ 'progress' ] = 0 ; $ context [ 'sandbox' ] [ 'campaign' ] = $ campaign ; $ context [ 'sandbox' ] [ 'activities' ] = $ activities ; $ context [ 'sandbox' ] [ 'branches' ] = array_keys ( $ branches ) ; $ context [ 'sandbox' ] [ 'places' ] = $ places ; $ context [ 'sandbox' ] [ 'max' ] = count ( $ branches ) ; } $ branchId = $ context [ 'sandbox' ] [ 'branches' ] [ $ context [ 'sandbox' ] [ 'progress' ] ] ; $ campaign = $ context [ 'sandbox' ] [ 'campaign' ] ; $ activities = $ context [ 'sandbox' ] [ 'activities' ] ; $ places = $ context [ 'sandbox' ] [ 'places' ] ; // Get all member campaigns for this branch. $ memberCampaignsInfo = self :: getInfoByBranch ( $ campaign , $ branchId , $ activities ) ; $ alreadyWinners = [ ] ; $ activityWinners = [ ] ; // Assign places. foreach ( $ places as $ place ) { // Calculate winners per activity. foreach ( $ memberCampaignsInfo as $ category => $ data ) { if ( empty ( $ data ) ) { break ; } $ memberCampaignItem = self :: getRandomWinner ( $ data , $ alreadyWinners ) ; // Create Winner entities. if ( ! empty ( $ memberCampaignItem ) ) { $ activityWinners [ ] = [ 'member_campaign' => $ memberCampaignItem [ 'member_campaign' ] , 'activity' => $ category , 'place' => $ place , ] ; } } } // Calculate winners by Visits goal. $ goalWinners = [ ] ; if ( $ isVisitsGoal ) { /** @var \Drupal\openy_campaign\CampaignMenuService $campaignMenuService */ $ campaignMenuService = \ Drupal :: service ( 'openy_campaign.campaign_menu_handler' ) ; $ goalWinnersIds = $ campaignMenuService -> getVisitsGoalWinners ( $ campaign , $ branchId , $ alreadyWinners ) ; // Create Winner entity. If winner defined by Visits goal without activity - set Activity = 0. foreach ( $ places as $ place ) { $ goalWinnerId = array_shift ( $ goalWinnersIds ) ; $ goalWinners [ ] = [ 'member_campaign' => $ goalWinnerId , 'activity' => 0 , 'place' => $ place , ] ; } } $ winners = array_merge ( $ goalWinners , $ activityWinners ) ; foreach ( $ winners as $ item ) { $ winner = Winner :: create ( $ item ) ; $ winner -> save ( ) ; $ context [ 'results' ] [ ] = $ item [ 'member_campaign' ] ; } $ context [ 'sandbox' ] [ 'progress' ] ++ ; if ( $ context [ 'sandbox' ] [ 'progress' ] != $ context [ 'sandbox' ] [ 'max' ] ) { $ context [ 'finished' ] = $ context [ 'sandbox' ] [ 'progress' ] / $ context [ 'sandbox' ] [ 'max' ] ; } }
Batch step to calculate winners for each branch.
Batch step to calculate winners for each branch .