2020-05-29 23:20:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Application;
|
|
|
|
use App\Events\ApplicationApprovedEvent;
|
|
|
|
use App\Events\ApplicationDeniedEvent;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class CountVotes extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'votes:evaluate {--d|dryrun : Controls whether passing applicants should be promoted (e.g. only show results)}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Iterates through eligible applications and determines if they should be approved based on the number of votes';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$eligibleApps = Application::where('applicationStatus', 'STAGE_PEERAPPROVAL')->get();
|
|
|
|
$pbar = $this->output->createProgressBar($eligibleApps->count());
|
|
|
|
|
2020-10-21 00:29:50 +00:00
|
|
|
if($eligibleApps->isEmpty())
|
|
|
|
{
|
2020-05-29 23:20:39 +00:00
|
|
|
$this->error('𐄂 There are no applications that need to be processed.');
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-21 00:29:50 +00:00
|
|
|
foreach ($eligibleApps as $application)
|
|
|
|
{
|
2020-05-29 23:20:39 +00:00
|
|
|
$votes = $application->votes;
|
|
|
|
$voteCount = $application->votes->count();
|
|
|
|
|
|
|
|
$positiveVotes = 0;
|
|
|
|
$negativeVotes = 0;
|
|
|
|
|
2020-10-21 00:29:50 +00:00
|
|
|
if ($voteCount > 5)
|
|
|
|
{
|
|
|
|
$this->info('Counting votes for application ID ' . $application->id);
|
|
|
|
foreach ($votes as $vote)
|
|
|
|
{
|
|
|
|
switch ($vote->allowedVoteType)
|
|
|
|
{
|
2020-05-29 23:20:39 +00:00
|
|
|
case 'VOTE_APPROVE':
|
|
|
|
$positiveVotes++;
|
|
|
|
break;
|
|
|
|
case 'VOTE_DENY':
|
|
|
|
$negativeVotes++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 00:29:50 +00:00
|
|
|
$this->info('Total votes for application ID ' . $application->id . ': ' . $voteCount);
|
2020-05-29 23:20:39 +00:00
|
|
|
$this->info('Calculating criteria...');
|
|
|
|
$negativeVotePercent = floor(($negativeVotes / $voteCount) * 100);
|
|
|
|
$positiveVotePercent = floor(($positiveVotes / $voteCount) * 100);
|
|
|
|
|
|
|
|
$pollResult = $positiveVotePercent > $negativeVotePercent;
|
|
|
|
|
|
|
|
$this->table([
|
|
|
|
'% of approval votes',
|
2020-10-21 00:29:50 +00:00
|
|
|
'% of denial votes'
|
2020-05-29 23:20:39 +00:00
|
|
|
], [ // array of arrays, e.g. rows
|
|
|
|
[
|
2020-10-21 00:29:50 +00:00
|
|
|
$positiveVotePercent . "%",
|
|
|
|
$negativeVotePercent . "%"
|
|
|
|
]
|
2020-05-29 23:20:39 +00:00
|
|
|
]);
|
|
|
|
|
2020-10-21 00:29:50 +00:00
|
|
|
if ($pollResult)
|
|
|
|
{
|
|
|
|
$this->info('✓ Dispatched promotion event for applicant ' . $application->user->name);
|
|
|
|
if (!$this->option('dryrun'))
|
|
|
|
{
|
2020-06-26 23:32:33 +00:00
|
|
|
$application->response->vacancy->vacancyCount -= 1;
|
|
|
|
$application->response->vacancy->save();
|
|
|
|
|
2020-05-29 23:20:39 +00:00
|
|
|
event(new ApplicationApprovedEvent(Application::find($application->id)));
|
2020-10-21 00:29:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-29 23:20:39 +00:00
|
|
|
$this->warn('Dry run: Event won\'t be dispatched');
|
|
|
|
}
|
|
|
|
|
|
|
|
$pbar->advance();
|
2020-10-21 00:29:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
if (!$this->option('dryrun'))
|
|
|
|
{
|
2020-05-29 23:20:39 +00:00
|
|
|
event(new ApplicationDeniedEvent(Application::find($application->id)));
|
2020-10-21 00:29:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2020-05-29 23:20:39 +00:00
|
|
|
$this->warn('Dry run: Event won\'t be dispatched');
|
|
|
|
}
|
|
|
|
|
|
|
|
$pbar->advance();
|
2020-10-21 00:29:50 +00:00
|
|
|
$this->error('𐄂 Applicant ' . $application->user->name . ' does not meet vote criteria (Majority)');
|
2020-05-29 23:20:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 00:29:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->warn("Application ID" . $application->id . " did not have enough votes for processing (min 5)");
|
|
|
|
}
|
|
|
|
|
2020-05-29 23:20:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$pbar->finish();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|