pollthingy/app/Poll.php

64 lines
1.2 KiB
PHP
Raw Normal View History

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Poll extends Model
{
2018-10-16 03:23:19 +02:00
public $timestamps = false;
public $keyType = "string";
public function __construct()
{
parent::__construct();
2018-10-16 03:23:19 +02:00
$this->id = Poll::createId();
}
2018-10-16 03:23:19 +02:00
private static function createId()
{
//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-17 15:12:20 +02:00
public function getResultsVisibleAttribute()
{
return !$this->hide_results_until_closed || ($this->closes_at != null && $this->closes_at->isPast());
}
}