Commit 2c34079c authored by Fernando Andrés Muñoz Bravo's avatar Fernando Andrés Muñoz Bravo Committed by Fernando Andrés Muñoz Bravo
Browse files

Issue #3254981 by waspper, KondratievaS: Views improvements

parent 45e0adeb
Loading
Loading
Loading
Loading
+53 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ function auctioneer_requirements($phase) {
/**
 * Implements hook_schema().
 *
 * We're using a small/dedicated table to save fulfillments. This is
 * We're using small/dedicated tables to store useful data. This is
 * because we want to keep this separated from main entities, but it's not
 * as big, or as flexible as content entities.
 */
@@ -85,6 +85,58 @@ function auctioneer_schema() {
      ],
    ],
  ];
  $schema['auctioneer_auction_statistics'] = [
    'description' => 'Stores auction statistics.',
    'fields' => [
      'auction_id' => [
        'description' => 'The auction id for these statistics.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'total_amount_bids' => [
        'description' => 'The total number of bids the auction has received.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ],
      'total_hammer_bids' => [
        'description' => 'The total number of hammer bids the auction has received.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ],
      'allowed_hammer_bids' => [
        'description' => 'The number of allowed hammer bids the auction is allowed to have.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'big',
      ],
      'timestamp' => [
        'description' => 'The most recent time the auction has received an interaction.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['auction_id'],
    'foreign keys' => [
      'auction_id' => [
        'table' => 'auction',
        'columns' => [
          'auction_id' => 'id',
        ],
      ],
    ],
  ];

  return $schema;
}
+33 −1
Original line number Diff line number Diff line
@@ -180,11 +180,43 @@ function auctioneer_cron() {
  \Drupal::service('auctioneer.auction_type.manager')->purgeOrphanBids();
}

/**
 * Implements hook_ENTITY_TYPE_insert() for entity type "auction".
 */
function auctioneer_auction_insert(EntityInterface $entity) {
  \Drupal::service('auctioneer.auction_type.manager')->setAuctionStatistics($entity);
}

/**
 * Implements hook_ENTITY_TYPE_delete() for entity type "auction".
 */
function auctioneer_auction_delete(EntityInterface $entity) {
  \Drupal::service('auctioneer.auction_type.manager')->purgeAuctionStatistics($entity);
}

/**
 * Implements hook_ENTITY_TYPE_insert() for entity type "bid".
 */
function auctioneer_bid_insert(EntityInterface $entity) {
  $auction_type_manager = \Drupal::service('auctioneer.auction_type.manager');
  /** @var \Drupal\auctioneer\Entity\AuctionInterface $auction */
  $auction = $entity->getAuction();
  if ($auction) {
    $auction_type_manager->setAuctionStatistics($auction);
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete() for entity type "bid".
 */
function auctioneer_bid_delete(EntityInterface $entity) {
  \Drupal::service('auctioneer.auction_type.manager')->deleteAuctionHammerBidData($entity);
  $auction_type_manager = \Drupal::service('auctioneer.auction_type.manager');
  $auction_type_manager->deleteAuctionHammerBidData($entity);
  /** @var \Drupal\auctioneer\Entity\AuctionInterface $auction */
  $auction = $entity->getAuction();
  if ($auction) {
    $auction_type_manager->setAuctionStatistics($auction);
  }
}

/**
+82 −9
Original line number Diff line number Diff line
@@ -272,7 +272,7 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
   * {@inheritdoc}
   */
  public function processBidForm(array &$form, FormStateInterface $form_state) {
    /* @var \Drupal\auctioneer\Entity\BidInterface $bid */
    /** @var \Drupal\auctioneer\Entity\BidInterface $bid */
    $bid = $form_state->getFormObject()->getEntity();
    // Plugin instance and validation.
    $auction_plugin_id = $bid->getBidType()->getPluginName();
@@ -310,32 +310,82 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setAuctionStatistics(AuctionInterface $auction) {
    /** @var \Drupal\auctioneer\Entity\BidTypeInterface $bid_type */
    $bid_type = $auction->getAuctionType()->getBidType();
    $plugin_definition = $this->getBidTypePluginDefinition($bid_type);
    $total_bids = $this->getAuctionBidsCount($auction);
    $hammer = $this->getAuctionHammerBids($auction, FALSE);
    // Now, let's insert/update our modified record.
    $this->database->merge('auctioneer_auction_statistics')
      ->key('auction_id', $auction->id())
      ->fields([
        'total_amount_bids' => $total_bids,
        'total_hammer_bids' => count($hammer),
        'allowed_hammer_bids' => $plugin_definition['hammer_count'],
        'timestamp' => time(),
      ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function getAuctionStatistics(AuctionInterface $auction) : array {
    $result = $this->database->select('auctioneer_auction_statistics', 'ac')
      ->fields('ac')
      ->condition('auction_id', $auction->id())
      ->execute()
      ->fetchAssoc();

    return is_array($result) ? $result : [];
  }

  /**
   * {@inheritdoc}
   */
  public function purgeAuctionStatistics(AuctionInterface $auction) {
    $this->database->delete('auctioneer_auction_statistics')
      ->condition('auction_id', $auction->id())
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function setAuctionHammerBids(array $bids, bool $close_auction = TRUE) {
    $auctions = [];
    // First: Hammer bids. Extract auctions to be closed.
    // First: Hammer bids. Extract auctions to be processed.
    $uid = $this->account->id();
    $merge = $this->database->merge('auctioneer_auction_hammer');
    $timestamp = time();
    $operation = $this->database->insert('auctioneer_auction_hammer')->fields([
      'bid',
      'uid',
      'timestamp',
    ]);
    foreach ($bids as $bid) {
      $merge->key('bid', $bid->id())->fields([
      $operation->values([
        'bid' => $bid->id(),
        'uid' => $uid,
        'timestamp' => time(),
        'timestamp' => $timestamp,
      ]);
      /** @var \Drupal\auctioneer\Entity\AuctionInterface $auction */
      $auction = $bid->getAuction();
      $auctions[$auction->id()] = $auction;
    }
    $merge->execute();
    // Second: Close auctions.
    if (!empty($auctions) && $close_auction) {
    $operation->execute();
    // Second: Process auctions.
    if (!empty($auctions)) {
      foreach ($auctions as $auction) {
        if ($auction->getAuctionState() != 'closed') {
        if ($auction->getAuctionState() != 'closed' && $close_auction) {
          $auction->set('auction_state', 'closed');
          $auction->save();
        }
        // Let's reset auction statistics.
        $this->setAuctionStatistics($auction);
      }
    }
  }
@@ -360,6 +410,11 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
    $this->database->delete('auctioneer_auction_hammer')
      ->condition('bid', $bid->id())
      ->execute();
    $auction = $bid->getAuction();
    if ($auction) {
      // Let's reset auction statistics.
      $this->setAuctionStatistics($auction);
    }
  }

  /**
@@ -371,6 +426,8 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
      $this->database->delete('auctioneer_auction_hammer')
        ->condition('bid', $hammer, 'IN')
        ->execute();
      // Let's reset auction statistics.
      $this->setAuctionStatistics($auction);
    }
  }

@@ -419,6 +476,22 @@ class AuctionTypeManager implements AuctionTypeManagerInterface {
    return is_array($plugin_definition) ? $plugin_definition : [];
  }

  /**
   * Helper function to get a number of placed bids per auction.
   *
   * @param \Drupal\auctioneer\Entity\AuctionInterface $auction
   *   Thye auction to get counted.
   */
  public function getAuctionBidsCount(AuctionInterface $auction) {
    $total = $this->database->select('bid_field_data', 'bfd')
      ->condition('auction_id', $auction->id())
      ->countQuery()
      ->execute()
      ->fetchField();

    return $total;
  }

  /**
   * Get an entity display for the given entity type and bundle.
   *
+24 −0
Original line number Diff line number Diff line
@@ -133,6 +133,30 @@ interface AuctionTypeManagerInterface {
   */
  public function submitBidForm(array &$form, FormStateInterface $form_state);

  /**
   * Set auction statistics for a given auction.
   *
   * @param \Drupal\auctioneer\Entity\AuctionInterface $auction
   *   The auction to react on.
   */
  public function setAuctionStatistics(AuctionInterface $auction);

  /**
   * Get auction statistics for a given auction.
   *
   * @param \Drupal\auctioneer\Entity\AuctionInterface $auction
   *   The auction to react on.
   */
  public function getAuctionStatistics(AuctionInterface $auction) : array;

  /**
   * Purge auction statistics.
   *
   * @param \Drupal\auctioneer\Entity\AuctionInterface $auction
   *   The auction to purge statistics on.
   */
  public function purgeAuctionStatistics(AuctionInterface $auction);

  /**
   * Set bids as winner. This will also close the auction.
   *
+74 −0
Original line number Diff line number Diff line
@@ -14,6 +14,80 @@ class AuctionViewsData extends EntityViewsData {
   */
  public function getViewsData() {
    $data = parent::getViewsData();
    $data['auctioneer_auction_statistics']['table']['group'] = $this->t('Auction statistics');
    $data['auctioneer_auction_statistics']['table']['join']['auction_field_data'] = [
      'type' => 'LEFT',
      'left_field' => 'id',
      'field' => 'auction_id',
    ];
    $data['auctioneer_auction_statistics']['total_amount_bids'] = [
      'title' => $this->t('Amount of bids'),
      'help' => $this->t('The total number of placed bids.'),
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];
    $data['auctioneer_auction_statistics']['total_hammer_bids'] = [
      'title' => $this->t('Amount of hammer bids'),
      'help' => $this->t('The total number of hammer bids.'),
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];
    $data['auctioneer_auction_statistics']['allowed_hammer_bids'] = [
      'title' => $this->t('Allowed hammer bids'),
      'help' => $this->t('The number of allowed hammer bids.'),
      'field' => [
        'id' => 'numeric',
      ],
      'filter' => [
        'id' => 'numeric',
      ],
      'sort' => [
        'id' => 'standard',
      ],
      'argument' => [
        'id' => 'standard',
      ],
    ];
    $data['auctioneer_auction_statistics']['timestamp'] = [
      'title' => $this->t('Last interaction timestamp'),
      'help' => $this->t('Date and time of when the last interaction was made.'),
      'field' => [
        'id' => 'date',
      ],
      'sort' => [
        'id' => 'date',
      ],
      'filter' => [
        'id' => 'date',
      ],
    ];
    $data['auctioneer_auction_statistics']['hammer_bids_reached'] = [
      'title' => $this->t('Hammer bids reached'),
      'help' => $this->t('Indicates if hammer bids amount is reached.'),
      'filter' => [
        'id' => 'auction_hammer_bids_reached',
      ],
    ];

    return $data;
  }
Loading