wolnosciowiec/collective-voting-bundle

此包已被废弃,不再维护。未建议替代包。

Symfony 包,允许用户集体决定(直接民主)

安装: 22

依赖者: 0

建议者: 0

安全: 0

星标: 2

关注者: 2

分支: 0

类型:symfony-bundle

dev-master 2017-07-23 11:05 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:37:04 UTC


README

允许创建对某个行动的投票,以便每个允许的人可以对特定行动进行投票。

可以创建多种决策策略,基于已知数据,如投票数量和投票的某些最小/最大百分比。

创建投票主题

主题必须实现 CollectiveVotingSubjectInterface,因此它可以是任何类,或者实现此接口的您的实体。

除了主题类,您还需要创建以下内容

  1. 工厂(将在只有投票流程对象的情况下构建主题)
  2. 处理器(处理投票,如果通过则例如发布帖子)
  3. 描述者(描述如何在批准仪表板中显示对象)

第1步:工厂

您需要注册一个新的工厂类,用于构建您的主题对象。

示例服务名称:collectivevoting.factory.appbundle.entity.content.page 每个服务都必须以 collectivevoting.factory. 为前缀,名称是任意的。

如果您将使用实体作为主题,则有一个现成的实体基类可供使用,因此您不必自行实现它。

<?php

namespace YourApp\AppBundle;

class PageVotingProcessFactory extends AbstractBaseVotingProcessFactory
    implements VotingProcessEntityFactoryInterface { };

第2步:决策处理器

决策处理器将在投票结束后执行您想要执行的正确操作。

例如,您想在大多数人说应该发布时发布帖子。

服务命名空间应为:collectivevoting.processor.{{ your name here }}

处理器示例

<?php

namespace Wolnosciowiec\AppBundle\Service\Processor\CollectiveVoting;

use CollectiveVotingBundle\Model\Processor\DecisionProcessorInterface;
use CollectiveVotingBundle\Model\Entity\CollectiveVotingSubjectInterface;
use CollectiveVotingBundle\Entity\VotingProcess;
use Wolnosciowiec\AppBundle\Model\Entity\Content\Page;

/**
 * Page Decision Processor
 * =======================
 *   When voting will end and decision will be made
 *   then processDecision() will be ran
 *
 * @package Wolnosciowiec\AppBundle\Service\Processing\CollectiveVoting
 */
class PageDecisionProcessor implements DecisionProcessorInterface
{
    /**
     * @param VotingProcess $votingProcess
     * @param array $votesCount
     * @param CollectiveVotingSubjectInterface|Page $entity
     * @param array $originalEntityData
     * @param mixed $finalOption
     *
     * @return bool
     */
    public function processDecision(
        VotingProcess $votingProcess,
        $votesCount,
        CollectiveVotingSubjectInterface $entity,
        array $originalEntityData,
        $finalOption
    )
    {
        /** @var Page $entity */
        $entity->setPublished($finalOption === 'votes_for');

        return true;
    }

    /**
     * @param VotingProcess $votingProcess
     * @param array $votesCount
     * @param CollectiveVotingSubjectInterface|Page $entity
     * @param string $state
     * @param array $originalEntityData
     *
     * @return bool
     */
    public function processNotReadyState(
        VotingProcess $votingProcess,
        $votesCount,
        CollectiveVotingSubjectInterface $entity,
        $state,
        array $originalEntityData
    )
    {
        if ($entity->isPublished() && $this->isChanged($originalEntityData, $entity)) {
            $entity->setPublished(false);
            return false;
        }

        return true;
    }

    /**
     * @param array $originalEntityData
     * @param Page $entity
     * @return bool
     */
    private function isChanged($originalEntityData, Page $entity)
    {
        $dataSet = [
            'title'     => $entity->getTitle(),
            'content'   => $entity->getContent(),
            'published' => $entity->isPublished(),
            'urlName'  => $entity->getUrlName(),
        ];

        foreach ($dataSet as $field => $newValue) {
            if ($originalEntityData[$field] != $newValue) {
                return true;
            }
        }

        return false;
    }
}

权限

  • skipCollectiveVote:用户可以简单地结束投票并接受或拒绝提案。这仍然允许投票,但在保存主题对象时可以激活它(例如,如果批准是为了其激活)
  • collectiveVote:用户可以对任何行动进行投票的可能性

待办事项

  • 单元测试
  • 文档
  • 示例