work_time-1.0.x-dev/src/WorkTimeReport.php

src/WorkTimeReport.php
<?php

namespace Drupal\work_time;

use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;

/**
 * Class Work Time Monthly.
 *
 * @package Drupal\work_time\Services
 */
class WorkTimeReport {

  /**
   * Account user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Connection service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Work time report constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   Entity type manager service.
   * @param \Drupal\Core\Database\Connection $connection
   *   Database service.
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   Current user service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, Connection $connection, AccountInterface $currentUser) {
    $this->entityTypeManager = $entity_type_manager;
    $this->connection = $connection;
    $this->currentUser = $currentUser;
  }

  /**
   * {@inheritdoc}
   */
  public function getDataMonthly($year, $month) {
    $uid = $this->currentUser->id();
    return $this->getUserMonth($year, $month, $uid);
  }

  /**
   * {@inheritdoc}
   */
  public function getUserMonth($year, $month, $uid) {
    $sql = 'SELECT id, DATE(FROM_UNIXTIME(created)) AS date, reference_id, time_total/3600 AS total_time
FROM work_time
WHERE uid = :uid AND YEAR(FROM_UNIXTIME(created)) = :year AND
    MONTH(FROM_UNIXTIME(created)) = :month';
    $records = $this->connection->query($sql, [
      ':uid' => $uid,
      ':year' => $year,
      ':month' => $month,
    ])->fetchAll(\PDO::FETCH_ASSOC);
    return $records;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc