2018-10-09 02:38:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2018-10-18 00:01:03 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
|
2018-10-09 02:38:16 +02:00
|
|
|
class Poll extends Model
|
|
|
|
{
|
2018-10-16 03:23:19 +02:00
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
public $keyType = "string";
|
|
|
|
|
2018-10-09 02:38:16 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2018-10-16 03:23:19 +02:00
|
|
|
$this->id = Poll::createId();
|
2018-10-09 02:38:16 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 03:23:19 +02:00
|
|
|
private static function createId()
|
2018-10-09 02:38:16 +02:00
|
|
|
{
|
|
|
|
//TODO: Check if id is unique
|
|
|
|
|
2018-10-16 03:23:19 +02:00
|
|
|
$characters = 'abcdefghijklmnopqrstuvwxyz';
|
|
|
|
$id = '';
|
|
|
|
for($i = 0; $i < 6; $i++) {
|
|
|
|
$id .= $characters[rand(0, strlen($characters) - 1)];
|
|
|
|
}
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function options()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\PollOption');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function votes()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\PollVote');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function voting_codes()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\PollVotingCode');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createVotingCodes($n)
|
|
|
|
{
|
|
|
|
$codes = [];
|
|
|
|
for($i = 0; $i < $n; $i++) {
|
|
|
|
$codes[] = new PollVotingCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->voting_codes()->saveMany($codes);
|
|
|
|
|
|
|
|
return $codes;
|
2018-10-09 02:38:16 +02:00
|
|
|
}
|
2018-10-17 15:12:20 +02:00
|
|
|
|
2018-10-19 20:16:58 +02:00
|
|
|
public function getClosedAttribute()
|
|
|
|
{
|
|
|
|
return ($this->closes_at != null && Carbon::parse($this->closes_at)->isPast()) ||
|
|
|
|
($this->duplicate_vote_checking == 'codes' && $this->voting_codes()->where('used', false)->count() == 0);
|
|
|
|
}
|
|
|
|
|
2018-10-17 15:12:20 +02:00
|
|
|
public function getResultsVisibleAttribute()
|
|
|
|
{
|
2018-10-19 20:16:58 +02:00
|
|
|
return !$this->hide_results_until_closed || $this->closed;
|
2018-10-17 15:12:20 +02:00
|
|
|
}
|
2018-10-09 02:38:16 +02:00
|
|
|
}
|