Skip to content
Snippets Groups Projects
Commit 46b73e4f authored by Tim Rohaly's avatar Tim Rohaly
Browse files

Issue #3508990 by tr: Add some new VoteResultFunction plugins

parent 0032a1c8
Branches
Tags
2 merge requests!50Issue #3510555 return int in getCreatedTime(),!46Issue #3508990 by tr: Add some new VoteResultFunction plugins
Pipeline #434286 passed with warnings
<?php
declare(strict_types=1);
namespace Drupal\votingapi\Plugin\VoteResultFunction;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\votingapi\Attribute\VoteResultFunction;
use Drupal\votingapi\VoteResultFunctionBase;
/**
* The maximum of a set of votes.
*/
#[VoteResultFunction(
id: "vote_maximum",
label: new TranslatableMarkup("Maximum"),
description: new TranslatableMarkup("The maximum vote value.")
)]
class Maximum extends VoteResultFunctionBase {
/**
* {@inheritdoc}
*/
public function calculateResult(array $votes): float {
$max = 0;
/** @var \Drupal\votingapi\VoteInterface[] $votes */
foreach ($votes as $vote) {
$value = $vote->getValue();
if ($value > $max) {
$max = $value;
}
}
return $max;
}
}
<?php
declare(strict_types=1);
namespace Drupal\votingapi\Plugin\VoteResultFunction;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\votingapi\Attribute\VoteResultFunction;
use Drupal\votingapi\VoteResultFunctionBase;
/**
* The median of a set of votes.
*/
#[VoteResultFunction(
id: "vote_median",
label: new TranslatableMarkup("Median"),
description: new TranslatableMarkup("The median vote value.")
)]
class Median extends VoteResultFunctionBase {
/**
* {@inheritdoc}
*/
public function calculateResult(array $votes): float {
/** @var \Drupal\votingapi\VoteInterface[] $votes */
$count = count($votes);
if ($count === 0) {
return 0;
}
$halfway = intdiv($count, 2);
usort($votes, function ($a, $b) {
$av = $a->getValue();
$bv = $b->getValue();
if ($av == $bv) {
return 0;
}
return ($av < $bv) ? -1 : 1;
});
if ($count & 1) {
// Count is odd.
return $votes[$halfway]->getValue();
}
else {
// Count is even.
// Assumes numerically indexed array starting with 0, and that's exactly
// what we have here because the usort() call replaces the original keys.
return ($votes[$halfway - 1]->getValue() + $votes[$halfway]->getValue()) / 2;
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\votingapi\Plugin\VoteResultFunction;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\votingapi\Attribute\VoteResultFunction;
use Drupal\votingapi\VoteResultFunctionBase;
/**
* The minimum of a set of votes.
*/
#[VoteResultFunction(
id: "vote_minimum",
label: new TranslatableMarkup("Minimum"),
description: new TranslatableMarkup("The minimum vote value.")
)]
class Minimum extends VoteResultFunctionBase {
/**
* {@inheritdoc}
*/
public function calculateResult(array $votes): float {
/** @var \Drupal\votingapi\VoteInterface[] $votes */
$min = reset($votes)->getValue();
foreach ($votes as $vote) {
$value = $vote->getValue();
if ($value < $min) {
$min = $value;
}
}
return $min;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment