You are here

protected function ActivityFactory::buildAggregatedActivites in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  2. 8 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  3. 8.2 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  4. 8.3 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  5. 8.4 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  6. 8.5 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  7. 8.6 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  8. 8.7 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  9. 8.8 modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  10. 10.0.x modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  11. 10.1.x modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()
  12. 10.2.x modules/custom/activity_creator/src/ActivityFactory.php \Drupal\activity_creator\ActivityFactory::buildAggregatedActivites()

Build the aggregated activities based on a data array.

1 call to ActivityFactory::buildAggregatedActivites()
ActivityFactory::buildActivities in modules/custom/activity_creator/src/ActivityFactory.php
Build the activities based on a data array.

File

modules/custom/activity_creator/src/ActivityFactory.php, line 252

Class

ActivityFactory
Class ActivityFactory to create Activity items based on ActivityLogs.

Namespace

Drupal\activity_creator

Code

protected function buildAggregatedActivites($data, $activity_fields) {
  $activities = [];
  $common_destinations = $this->activityDestinationManager
    ->getListByProperties('isCommon', TRUE);
  $personal_destinations = $this->activityDestinationManager
    ->getListByProperties('isCommon', FALSE);

  // Get related activities.
  $related_activities = $this
    ->getAggregationRelatedActivities($data);
  if (!empty($related_activities)) {

    // Update related activities.
    foreach ($related_activities as $related_activity) {
      $destination = $related_activity->field_activity_destinations->value;

      // If user already have related activity we remove it and create new.
      // And we also remove related activities from common streams.
      if ($related_activity
        ->getOwnerId() == $this
        ->getActor($data) || in_array($destination, $common_destinations)) {

        // @todo Consider if need to delete or unpublish old activites.
        $related_activity
          ->delete();
      }
      else {

        // For other users we leave activity only on their profile stream.
        $related_activity
          ->set('field_activity_destinations', [
          'stream_profile',
        ]);
        $related_activity
          ->save();
      }
    }

    // Clone activity fields for separate profile stream activity.
    $profile_activity_fields = $activity_fields;

    // Update output text for activity on not user related streams.
    $arguments = [];
    $message = Message::load($data['mid']);
    $count = $this
      ->getAggregationAuthorsCount($data);
    if (is_numeric($count) && $count > 1) {
      $arguments = [
        '@count' => $count - 1,
      ];
    }
    $activity_fields['field_activity_output_text'] = $this
      ->getFieldOutputText($message, $arguments);
    $activity_fields['field_activity_destinations'] = $this
      ->getFieldDestinations($data, $common_destinations);

    // Create separate activity for activity on user related streams.
    $profile_activity_fields['field_activity_destinations'] = $this
      ->getFieldDestinations($data, $personal_destinations);
    $activity = Activity::create($profile_activity_fields);
    $activity
      ->save();
    $activities[] = $activity;
  }
  $activity = Activity::create($activity_fields);
  $activity
    ->save();
  $activities[] = $activity;
  return $activities;
}