You are here

public function ModerationDashboardActivity::build in Moderation Dashboard 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Block/ModerationDashboardActivity.php \Drupal\moderation_dashboard\Plugin\Block\ModerationDashboardActivity::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/ModerationDashboardActivity.php, line 84

Class

ModerationDashboardActivity
Provides the "Moderation Dashboard Activity" block.

Namespace

Drupal\moderation_dashboard\Plugin\Block

Code

public function build() {
  $request_time = $this->time
    ->getCurrentTime();
  $request_time_sub_month = strtotime('-1 month', $request_time);
  $results1 = $this->database
    ->query('select revision_uid as uid,count(*) as count from {node_revision} where revision_timestamp >= :request_time_sub_month group by revision_uid', [
    ':request_time_sub_month' => $request_time_sub_month,
  ])
    ->fetchAllAssoc('uid', \PDO::FETCH_ASSOC);
  $results2 = $this->database
    ->query('select n.uid,count(n.uid) as count from (select nid,uid from {node_field_data} where created >= :request_time_sub_month group by nid,uid) n group by n.uid', [
    ':request_time_sub_month' => $request_time_sub_month,
  ])
    ->fetchAllAssoc('uid', \PDO::FETCH_ASSOC);
  $uids = array_merge(array_keys($results1), array_keys($results2));
  if (!$uids) {
    return [
      '#markup' => '<p>' . $this
        ->t('There has been no editor activity within the last month.') . '</p>',
    ];
  }
  $users = $this->userStorage
    ->loadMultiple($uids);
  $data = [
    'labels' => [],
    'datasets' => [
      [
        'label' => $this
          ->t('Content edited'),
        'data' => [],
        'backgroundColor' => [],
      ],
      [
        'label' => $this
          ->t('Content authored'),
        'data' => [],
        'backgroundColor' => [],
      ],
    ],
  ];
  foreach ($users as $uid => $user) {
    $data['labels'][] = $user
      ->label();
    $data['datasets'][0]['data'][] = isset($results1[$uid]['count']) ? $results1[$uid]['count'] : 0;
    $data['datasets'][0]['backgroundColor'][] = 'rgba(11,56,223,.8)';
    $data['datasets'][1]['data'][] = isset($results2[$uid]['count']) ? $results2[$uid]['count'] : 0;
    $data['datasets'][1]['backgroundColor'][] = 'rgba(27,223,9,.8)';
  }
  $build = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'moderation-dashboard-activity',
      ],
    ],
    '#attached' => [
      'library' => [
        'moderation_dashboard/activity',
      ],
      'drupalSettings' => [
        'moderation_dashboard_activity' => $data,
      ],
    ],
  ];
  return $build;
}