Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • tmr/ld-simulator
1 result
Select Git revision
Show changes
Showing
with 913 additions and 0 deletions
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Fruitcake\Cors\HandleCors::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* @param \Illuminate\Http\Request $request
* @return string|null
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
}
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
class CheckForMaintenanceMode extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
*/
protected $except = [
//
];
}
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
//
];
}
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
return $next($request);
}
}
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array
*/
protected $except = [
'password',
'password_confirmation',
];
}
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Middleware\TrustHosts as Middleware;
class TrustHosts extends Middleware
{
/**
* Get the host patterns that should be trusted.
*
* @return array
*/
public function hosts()
{
return [
$this->allSubdomainsOfApplicationUrl(),
];
}
}
<?php
namespace App\Http\Middleware;
use Fideloper\Proxy\TrustProxies as Middleware;
use Illuminate\Http\Request;
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* @var array|string|null
*/
protected $proxies;
/**
* The headers that should be used to detect proxies.
*
* @var int
*/
protected $headers = Request::HEADER_X_FORWARDED_ALL;
}
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
//
];
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class MajorityVote extends Model
{
public $timestamps = false;
protected $fillable = [
'election_id',
'voter_id',
'vote'
];
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Population extends Model
{
protected $fillable =[
'name',
'parent_id',
'stage',
'election_type',
'follower_factor',
'forgetting_factor'
];
protected $appends = [
//'no_of_voters'
];
public function voters() {
return $this->hasMany(Voter::class, 'population_id', 'id');
}
public function childPopulations() {
return $this->hasMany(Population::class, 'parent_id', 'id');
}
public function getChildrenCountAttribute() {
return $this->childPopulations()->count();
}
public function getChildPopulationsStatsAttribute() {
$data = [
'm' => [
'info' => 'Majority',
'count' => 0,
'learning_stage_count' => 0,
'performance_stage_count' => 0
],
'd1' => [
'info' => 'Delegation Performance (d1)',
'count' => 0,
'learning_stage_count' => 0,
'performance_stage_count' => 0
],
'd2' => [
'info' => 'Delegation Learning (d2)',
'count' => 0,
'learning_stage_count' => 0,
'performance_stage_count' => 0
],
'd3' => [
'info' => 'Delegation Learning (d3)',
'count' => 0,
'learning_stage_count' => 0,
'performance_stage_count' => 0
]
];
foreach ($this->childPopulations as $childPopulation) {
if (isset($childPopulation->election_type) && isset($data[$childPopulation->election_type])) {
$data[$childPopulation->election_type]['count']++;
switch($childPopulation->stage) {
case 'l':
$data[$childPopulation->election_type]['learning_stage_count']++;
break;
case 'p':
$data[$childPopulation->election_type]['performance_stage_count']++;
break;
default:
break;
}
}
}
return $data;
}
public function getNoOfVotersAttribute() {
return $this->voters()->count();
}
public function getVotersStatsAttribute() {
$groupNames = $this->voters()->pluck('group')->unique();
$groups = array();
foreach ($groupNames as $name) {
$newGroup = new \stdClass();
$newGroup->name = $name;
$newGroup->no_of_voters = $this->voters()->where('group', '=', $name)->count();
$newGroup->expertise_average = $this->voters()->where('group', '=', $name)->average('expertise');
$newGroup->confidence_average = $this->voters()->where('group', '=', $name)->average('confidence');
$newGroup->following_average = $this->voters()->where('group', '=', $name)->average('following');
$newGroup->leadership_average = $this->voters()->where('group', '=', $name)->average('leadership');
$groups[] = $newGroup;
}
return [
'groups' => $groups,
'no_of_voters' => $this->voters()->count(),
'expertise_max' => $this->voters()->max('expertise'),
'expertise_average' => $this->voters()->average('expertise'),
'confidence_average' => $this->voters()->average('confidence'),
'following_average' => $this->voters()->average('following'),
'leadership_average' => $this->voters()->average('leadership')
];
}
public function elections() {
return $this->hasMany(Election::class, 'population_id', 'id');
}
public function getElectionsStatsAttribute() {
$noOfCorrectAvgM = $this->elections()->where('type', '=', 'm')->average('total_correct');
$noOfIncorrectAvgM = $this->elections()->where('type', '=', 'm')->average('total_incorrect');
$sumM = $noOfCorrectAvgM+ $noOfIncorrectAvgM;
if ($sumM > 0) {
$percentCorrectM = 100 * $noOfCorrectAvgM / $sumM;
} else {
$percentCorrectM = null;
}
$noOfCorrectAvgD1 = $this->elections()->where('type', '=', 'd1')->average('total_correct');
$noOfIncorrectAvgD1 = $this->elections()->where('type', '=', 'd1')->average('total_incorrect');
$sumD1 = $noOfCorrectAvgD1 + $noOfIncorrectAvgD1;
if ($sumD1 > 0) {
$percentCorrectD1 = 100 * $noOfCorrectAvgD1 / $sumD1;
} else {
$percentCorrectD1 = null;
}
$noOfCorrectAvgD2 = $this->elections()->where('type', '=', 'd2')->average('total_correct');
$noOfIncorrectAvgD2 = $this->elections()->where('type', '=', 'd2')->average('total_incorrect');
$sumD2 = $noOfCorrectAvgD2 + $noOfIncorrectAvgD2;
if ($sumD2 > 0) {
$percentCorrectD2 = 100 * $noOfCorrectAvgD2 / $sumD2;
} else {
$percentCorrectD2 = null;
}
$noOfCorrectAvgD3 = $this->elections()->where('type', '=', 'd3')->average('total_correct');
$noOfIncorrectAvgD3 = $this->elections()->where('type', '=', 'd3')->average('total_incorrect');
$sumD3 = $noOfCorrectAvgD3 + $noOfIncorrectAvgD3;
if ($sumD3 > 0) {
$percentCorrectD3 = 100 * $noOfCorrectAvgD3 / $sumD3;
} else {
$percentCorrectD3 = null;
}
return [
[
'type' => 'm',
'count' => $this->elections()->where('type', '=', 'm')->count(),
'no_of_correct_average' => $noOfCorrectAvgM,
'no_of_incorrect_average' => $noOfIncorrectAvgM,
'percent_correct' => $percentCorrectM
],
[
'type' => 'd1',
'count' => $this->elections()->where('type', '=', 'd1')->count(),
'no_of_correct_average' => $noOfCorrectAvgD1,
'no_of_incorrect_average' => $noOfIncorrectAvgD1,
'percent_correct' => $percentCorrectD1
],
[
'type' => 'd2',
'count' => $this->elections()->where('type', '=', 'd2')->count(),
'no_of_correct_average' => $noOfCorrectAvgD2,
'no_of_incorrect_average' => $noOfIncorrectAvgD2,
'percent_correct' => $percentCorrectD2
],
[
'type' => 'd3',
'count' => $this->elections()->where('type', '=', 'd3')->count(),
'no_of_correct_average' => $noOfCorrectAvgD3,
'no_of_incorrect_average' => $noOfIncorrectAvgD3,
'percent_correct' => $percentCorrectD3
]
];
}
}
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Broadcast;
use Illuminate\Support\ServiceProvider;
class BroadcastServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Broadcast::routes();
require base_path('routes/channels.php');
}
}
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
parent::boot();
//
}
}
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* @var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
// public routes - no auth
Route::prefix('api/external')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api_external.php'));
// protected routes
Route::prefix('api/internal')
->middleware('web')
->namespace($this->namespace)
->group(base_path('routes/api_internal.php'));
}
}
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Voter extends Model
{
public $timestamps = false;
protected $fillable = [
'population_id',
'expertise',
'confidence',
'following',
'leadership',
'reputation',
'group'
];
protected $appends = ['majority_votes_stats', 'delegation_one_votes_stats', 'delegation_two_votes_stats'];
public function majorityVotes() {
return $this->hasMany(MajorityVote::class, 'voter_id', 'id');
}
public function getMajorityVotesStatsAttribute() {
$correct = $this->majorityVotes()->where('vote', '=', true)->count();
$incorrect = $this->majorityVotes()->where('vote', '=', false)->count();
$all = $correct + $incorrect;
$percentCorrect = $all > 0 ? round(100*$correct/$all, 3) : null;
return [
'correct' => $correct,
'incorrect' => $incorrect,
'percent_correct' => $percentCorrect
];
}
public function delegationOneVotes() {
return $this->hasMany(DelegationOneVote::class, 'voter_id', 'id')
->whereHas('election', function($q) {$q->where('type', '=', 'd1');});
}
public function delegationTwoVotes() {
return $this->hasMany(DelegationOneVote::class, 'voter_id', 'id')
->whereHas('election', function($q) {$q->where('type', '=', 'd2');});
}
public function myDelegationOneVotingDelegate() {
return $this->hasOne(Voter::class, 'vote_delegation', 'id');
}
public function getDelegationOneVotesStatsAttribute() {
$noOfDelegationOneVotes = $this->delegationOneVotes()->count();
if ($noOfDelegationOneVotes < 1) {
return [
'as_independent' => null,
'as_follower' => null,
'as_delegate' => null,
'percent_finals_correct' => null,
'finals_correct' => null,
'finals_incorrect' => null,
'finals_correct_as_independent' => null,
'finals_correct_as_follower' => null,
'finals_correct_as_delegate' => null
];
}
$asIndependent = $this->delegationOneVotes()->where('voter_mark', '=', 'i')->count();
$asFollower = $this->delegationOneVotes()->where('voter_mark', '=', 'f')->count();
$asDelegate = $this->delegationOneVotes()->where('voter_mark', '=', 'd')->count();
$myDelegationOneVotesFinalsCorrect = $this->delegationOneVotes()->where('vote_final', '=', 1)->count();
$myDelegationOneVotesFinalsIncorrect = $noOfDelegationOneVotes - $myDelegationOneVotesFinalsCorrect;
$myDelegationOneVotesFinalsCorrectAsFollower = $this->delegationOneVotes()->where('vote_final', '=', 1)->where('voter_mark', '=', 'f')->count();
$myDelegationOneVotesFinalsCorrectAsIndependent = $this->delegationOneVotes()->where('vote_final', '=', 1)->where('voter_mark', '=', 'i')->count();
$myDelegationOneVotesFinalsCorrectAsDelegate = $myDelegationOneVotesFinalsCorrect
- $myDelegationOneVotesFinalsCorrectAsFollower
- $myDelegationOneVotesFinalsCorrectAsIndependent;
$myDelegationOneVotesPercentFinalsCorrect = 100 * $myDelegationOneVotesFinalsCorrect / $noOfDelegationOneVotes;
return [
'as_independent' => $asIndependent,
'as_follower' => $asFollower,
'as_delegate' => $asDelegate,
'percent_finals_correct' => $myDelegationOneVotesPercentFinalsCorrect,
'finals_correct' => $myDelegationOneVotesFinalsCorrect,
'finals_incorrect' => $myDelegationOneVotesFinalsIncorrect,
'finals_correct_as_independent' => $myDelegationOneVotesFinalsCorrectAsIndependent,
'finals_correct_as_follower' => $myDelegationOneVotesFinalsCorrectAsFollower,
'finals_correct_as_delegate' => $myDelegationOneVotesFinalsCorrectAsDelegate
];
}
public function getDelegationTwoVotesStatsAttribute() {
$noOfDelegationTwoVotes = $this->delegationTwoVotes()->count();
if ($noOfDelegationTwoVotes < 1) {
return [
'as_independent' => null,
'as_follower' => null,
'as_delegate' => null,
'percent_finals_correct' => null,
'finals_correct' => null,
'finals_incorrect' => null,
'finals_correct_as_independent' => null,
'finals_correct_as_follower' => null,
'finals_correct_as_delegate' => null
];
}
$asIndependent = $this->delegationTwoVotes()->where('voter_mark', '=', 'i')->count();
$asFollower = $this->delegationTwoVotes()->where('voter_mark', '=', 'f')->count();
$asDelegate = $this->delegationTwoVotes()->where('voter_mark', '=', 'd')->where('vote_weight', '>', 1)->count();
$myDelegationTwoVotesFinalsCorrect = $this->delegationTwoVotes()->where('vote_final', '=', 1)->count();
$myDelegationTwoVotesFinalsIncorrect = $noOfDelegationTwoVotes - $myDelegationTwoVotesFinalsCorrect;
$myDelegationTwoVotesFinalsCorrectAsFollower = $this->delegationTwoVotes()->where('vote_final', '=', 1)->where('voter_mark', '=', 'f')->count();
$myDelegationTwoVotesFinalsCorrectAsIndependent = $this->delegationTwoVotes()->where('vote_final', '=', 1)->where('voter_mark', '=', 'i')->count();
$myDelegationTwoVotesFinalsCorrectAsDelegate = $myDelegationTwoVotesFinalsCorrect
- $myDelegationTwoVotesFinalsCorrectAsFollower
- $myDelegationTwoVotesFinalsCorrectAsIndependent;
$myDelegationTwoVotesPercentFinalsCorrect = 100 * $myDelegationTwoVotesFinalsCorrect / $noOfDelegationTwoVotes;
return [
'as_independent' => $asIndependent,
'as_follower' => $asFollower,
'as_delegate' => $asDelegate,
'percent_finals_correct' => $myDelegationTwoVotesPercentFinalsCorrect,
'finals_correct' => $myDelegationTwoVotesFinalsCorrect,
'finals_incorrect' => $myDelegationTwoVotesFinalsIncorrect,
'finals_correct_as_independent' => $myDelegationTwoVotesFinalsCorrectAsIndependent,
'finals_correct_as_follower' => $myDelegationTwoVotesFinalsCorrectAsFollower,
'finals_correct_as_delegate' => $myDelegationTwoVotesFinalsCorrectAsDelegate
];
}
}
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require __DIR__.'/vendor/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);
<?php
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Illuminate\Foundation\Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
$app->singleton(
Illuminate\Contracts\Http\Kernel::class,
App\Http\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;