punksolid/laravel-quadratic-voting
Composer 安装命令:
composer require punksolid/laravel-quadratic-voting
包简介
Voting system implementation for laravel. Quadratic voting approach.
README 文档
README
Quadratic Voting Implementation library to Laravel.
What is Quadratic Voting? It is a voting system designed to measure not only preference but also the intensity of that preference. https://www.youtube.com/watch?v=pjbakxIvGFA
Installation
composer require punksolid/laravel-quadratic-voting
<?php //User.php //Add the Voter trait use LaravelQuadraticVoting\Traits\VoterTrait; class User extends Authenticatable implements \LaravelQuadraticVoting\Interfaces\VoterInterface { use VoterTrait;
Currently you need to add the isVotable trait to your model and specify the voter model on the laravel_quadratic.php config file
<?php use LaravelQuadraticVoting\Traits\isVotable; //for example class Idea extends Model { use isVotable;
Publish the laravel_quadratic.php config file
php artisan vendor:publish --provider="LaravelQuadraticVoting\LaravelQuadraticVotingServiceProvider"
Set the models on the config file. models.voter and models.is_votable may be the only models that you need to change.
return [ 'models' => [ 'voter' => Illuminate\Foundation\Auth\User::class, //App\Models\User::class, 'is_votable' => \LaravelQuadraticVoting\Models\Idea::class, 'vote_credit' => LaravelQuadraticVoting\Models\VoteCredit::class, ], 'table_names' => [ 'votes' => 'votes', 'vote_credits' => 'vote_bag', ], 'column_names' => [ 'voter_key' => 'voter_id', ] ];
Basic Usage
To vote on something you just need to
//get an isVotable Model $idea = Idea::factory()->create(); $user->giveVoteCredits(14); //give credits to the voter //to the voter model, add an isVotable model, and in the second argument //the number of the credits, it will process the credits to votes. $user->voteOn($idea, $vote_credits = 14); // This will set 3 votes to the idea 1 + 4 + 9 = 14 $user->downVote($idea); // This will set -1 vote to the idea and give you the credits back
Methods available on voter
//ask if it has enough credits to spend $voter->hasCredits($wanna_spend) //boolean //adds 100 credits to a voter $voter->giveVoteCredits(100); //Return vote credits available $voter->getVoteCredits(); //Give voters and assign equally/massively credits VoterModel::massiveVoteCredits($voter_collection, $credits); VoterModel::massiveVoteReset($voter_collection); // All in 0 credits //You should not spend credits without voting, but in case you need //decrease the available credits to the user $voter->spendCredits($credits); //int // Get Next Vote Cost will return the credits to score 1 vote to the idea considering the previous votes of that user $voter->getNextVoteCost( $idea); // Get the real votes registered of a user in an specific idea $voter->getVotesAlreadyEmittedOnIdea($idea); // Get all the votes emitted by a voter in all the ideas $voter->getVotesAlreadyEmittedOverall();
On the Votable model object is
//gets all the votes $idea->getCountVotes() //Return a collection of all the voters $idea->getVoters();
punksolid/laravel-quadratic-voting 适用场景与选型建议
punksolid/laravel-quadratic-voting 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 122 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 punksolid/laravel-quadratic-voting 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 punksolid/laravel-quadratic-voting 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 122
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2018-10-27