punksolid/laravel-quadratic-voting
为Laravel实现的投票系统。采用二次投票方法。
v0.9.2
2023-03-16 04:05 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- illuminate/database: ^9.0
- illuminate/support: ^9.0
Requires (Dev)
- nunomaduro/collision: ^6.2
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.5
- rector/rector: ^0.15.0@dev
This package is auto-updated.
Last update: 2024-09-29 20:58:33 UTC
README
二次投票实现库,用于Laravel。
什么是二次投票?它是一种旨在衡量不仅偏好,还衡量偏好强度的投票系统。请参考视频教程
安装
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;
目前您需要将isVotable特质添加到您的模型中,并在laravel_quadratic.php配置文件中指定投票模型。
<?php use LaravelQuadraticVoting\Traits\isVotable; //for example class Idea extends Model { use isVotable;
发布laravel_quadratic.php
配置文件
php artisan vendor:publish --provider="LaravelQuadraticVoting\LaravelQuadraticVotingServiceProvider"
在配置文件中设置模型。可能只需要更改models.voter
和models.is_votable
这两个模型。
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', ] ];
基本用法
要对某事物进行投票,您只需
//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
投票者上的可用方法
//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();
在可投票模型对象上是
//gets all the votes $idea->getCountVotes() //Return a collection of all the voters $idea->getVoters();