openquestions-1.0.x-dev/src/Model/VoteDataBasic.php

src/Model/VoteDataBasic.php
<?php

namespace Drupal\openquestions\Model;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\openquestions\Entity\Term;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\openquestions\Model\VoteDataInterface;

/**
 * Represents the votes on a specific item.
 *
 * @see VoteDataByQuestionBasic, that hold the votes for each of the questions organized by author.
 */
class VoteDataBasic implements VoteDataInterface {

  use StringTranslationTrait;

  /**
   * The item ID.
   *
   * @var int
   */
  protected $itemID;

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

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

  /**
   * Our logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * The data.
   *
   * @var array
   */
  protected $votesByQuestions;

  /**
   * Constructs a new VoteDataBasic.
   *
   * @param int $itemID
   *   The ID of the item.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Config factory.
   * @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
   *   Entity type manager.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   Our logger.
   */
  public function __construct($itemID, ConfigFactoryInterface $configFactory, EntityTypeManager $entityTypeManager, LoggerChannelInterface $logger) {
    $this->itemID = $itemID;
    $this->configFactory = $configFactory;
    $this->entityTypeManager = $entityTypeManager;
    $this->logger = $logger;

    $votes = $this->entityTypeManager->getStorage('oq_vote')->loadByProperties([
      'item' => $this->itemID,
    ]);

    $this->votesByQuestions = [];
    foreach ($votes as $vote) {
      $questionID = $this->getReferencedEntityId($vote->question->referencedEntities());
      if (!$questionID) {
        continue;
      }

      if (empty($this->votesByQuestions[$questionID])) {
        $this->votesByQuestions[$questionID] = new VoteDataByQuestionBasic();
      }

      $this->votesByQuestions[$questionID]->add($vote);
    }

    // $this->logger->info('summary=' . $this->summarize($votes));
  }

  /**
   * {@inheritdoc}
   *
   * @TODO: loadByProperties doesn't do access checks, resolve that if necessary
   */
  public function getAllAccountsThatAreAllowedToVote() : array {
    $list = $this->entityTypeManager
      ->getStorage('user')
      ->loadByProperties([
        'roles' => 'oq_voter',
      ]);

    return $list;
  }

  /**
   * {@inheritdoc}
   */
  public function getAccountsThatAreAllowedToVoteOnThisItem() : array {
    return $this->getAllAccountsThatAreAllowedToVote();
  }

  /**
   * {@inheritdoc}
   *
   * @todo implement
   */
  public function getAccountsThatHaveVoted() : array {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getVotesForQuestionId($questionID) {
    return !empty($this->votesByQuestions[$questionID]) ? $this->votesByQuestions[$questionID] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getVotesForQuestionIdAndAccountId($questionID, $uid) {
    if (empty($this->votesByQuestions[$questionID])) {
      return NULL;
    }

    return $this->votesByQuestions[$questionID]->getVotesForAccountId($uid);
  }

  /**
   * Get the first ID from an array of entities.
   */
  protected function getReferencedEntityId($entities) {
    foreach ($entities as $entity) {
      $entityID = $entity->id();
      if ($entityID) {
        return $entityID;
      }
    }

    return 0;
  }

  /**
   * Debug function.
   *
   * @todo remove
   */
  protected function summarize($list) {
    if (!$list) {
      return '[NULL]';
    }
    $ret = [];
    foreach ($list as $item) {
      $ret[] = $item->id();
    }

    return json_encode($ret);
  }

}

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

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