-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicket.php
96 lines (86 loc) · 1.75 KB
/
Ticket.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ticket extends Model
{
/**
* Massive assign
*
* @var array
*/
protected $fillable = [
'vote_group_id', 'string','active'
];
/**
* popular search for ticket
*
* @param $query
* @param $string
* @return mixed
*/
public function scopeTicket($query, $string)
{
return $query->where('string', $string)->firstorFail();
}
/**
* Return the vote group to which the ticket belongs.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function voteGroup()
{
return $this->belongsTo('App\VoteGroup');
}
/**
* All answers belong to specific ticket
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function answers() //@TODO fuck this does not work for unknown reasons
{
return $this->morphMany('App\Answer', 'source');
}
/**
* Ticket IPAddress
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function ipAddress()
{
return $this->morphMany('App\IPAddress', 'source');
}
/**
* Check if ticket used
*
* @param $voteId
* @return mixed
*/
public function isTicketUsed($voteId)
{
if ($this->answers->map(function ($answer) {
return $answer->option->question->vote->id;
})->flatten()->search($voteId) === false
) {
return false;
}
return true;
}
/**
* Return a list of votes that this ticket has been used for
*
* @return array
*/
public function usedForVote(){
return $this->answers->map(function ($answer) {
return $answer->option->question->vote->id;
})->flatten()->unique();
}
/**
* clear vote record for a given ticket id.
*/
public function clearAnswers(){
foreach($this->answers as $answer){
$answer->delete();
}
}
}