Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Drupal\hello\Controller;
  4.  
  5. use Drupal\Core\Controller\ControllerBase;
  6. use Drupal\Core\Database\Connection;
  7. use Drupal\Core\Datetime\DateFormatter;
  8. use Drupal\user\UserInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10.  
  11. class UserStatisticsController extends ControllerBase {
  12.  
  13. /**
  14. * @var Connection
  15. */
  16. protected $database;
  17.  
  18. /**
  19. * @var DateFormatter
  20. */
  21. protected $dateFormatter;
  22.  
  23. /**
  24. * UserStatisticsController constructor.
  25. * @param Connection $database
  26. * @param DateFormatter $dateFormatter
  27. */
  28. public function __construct(Connection $database, DateFormatter $dateFormatter) {
  29. $this->database = $database;
  30. $this->dateFormatter = $dateFormatter;
  31. }
  32.  
  33. /**
  34. * {@inheritdoc}.
  35. */
  36. public static function create(ContainerInterface $container) {
  37. return new static(
  38. $container->get('database'),
  39. $container->get('date.formatter')
  40. );
  41. }
  42.  
  43. /**
  44. * @param \Drupal\user\UserInterface $user
  45. * @return array
  46. */
  47. public function content(UserInterface $user) {
  48. /** @var \Drupal\Core\Database\Query\SelectInterface $query */
  49. $query = $this->database->select('hello_user_statistics', 'hus')
  50. ->fields('hus', ['action', 'time'])
  51. ->condition('uid', $user->id());
  52. $result = $query->execute();
  53.  
  54. // Tableau des statistics.
  55. $rows = [];
  56. $connexions = 0;
  57. foreach ($result as $record) {
  58. $rows[] = [
  59. $record->action == '1' ? $this->t('Login') : $this->t('Logout'),
  60. $this->dateFormatter->format($record->time),
  61. ];
  62. $connexions = $connexions + $record->action;
  63. }
  64. $table = [
  65. '#type' => 'table',
  66. '#header' => [$this->t('Action'), $this->t('Time')],
  67. '#rows' => $rows,
  68. '#empty' => $this->t('No connections yet.'),
  69. ];
  70.  
  71. // Message en en-tΓͺte.
  72. $message = [
  73. '#theme' => 'hello_user_connexion',
  74. '#user' => $user,
  75. '#count' => $connexions,
  76. ];
  77.  
  78. // On renvoie les render arrays.
  79. return [
  80. 'message' => $message,
  81. 'table' => $table,
  82. '#cache' => [
  83. 'keys' => ['hello:user_statistics'],
  84. 'contexts' => ['user'],
  85. ],
  86. ];
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement