1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xoco70\KendoTournaments\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
7
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
8
|
|
|
use Illuminate\Foundation\Auth\User; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
|
11
|
|
|
class Championship extends Model |
12
|
|
|
{ |
13
|
|
|
use SoftDeletes; |
14
|
|
|
protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
15
|
|
|
protected $table = 'championship'; |
16
|
|
|
|
17
|
|
|
public $timestamps = true; |
18
|
|
|
protected $fillable = [ |
19
|
|
|
'tournament_id', |
20
|
|
|
'category_id', |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
protected static function boot() |
24
|
|
|
{ |
25
|
|
|
parent::boot(); |
26
|
|
|
|
27
|
|
|
static::deleting(function ($championship) { |
28
|
|
|
$championship->competitors()->delete(); |
29
|
|
|
$championship->settings()->delete(); |
30
|
|
|
}); |
31
|
|
|
static::restoring(function ($championship) { |
32
|
|
|
$championship->competitors()->restore(); |
33
|
|
|
$championship->settings()->restore(); |
34
|
|
|
}); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* A championship has many Competitors. |
39
|
|
|
* |
40
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
41
|
|
|
*/ |
42
|
|
|
public function competitors() |
43
|
|
|
{ |
44
|
|
|
return $this->hasMany(Competitor::class); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* A championship has many Competitors. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
51
|
|
|
*/ |
52
|
|
|
public function competitorsWithUser() |
53
|
|
|
{ |
54
|
|
|
return $this->hasMany(Competitor::class)->with('user'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* A championship belongs to a Category. |
59
|
|
|
* |
60
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
61
|
|
|
*/ |
62
|
|
|
public function category() |
63
|
|
|
{ |
64
|
|
|
return $this->belongsTo(Category::class); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* A championship belongs to a Tournament. |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
71
|
|
|
*/ |
72
|
|
|
public function tournament() |
73
|
|
|
{ |
74
|
|
|
return $this->belongsTo(Tournament::class); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get All competitors from a Championships. |
79
|
|
|
* |
80
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
81
|
|
|
*/ |
82
|
|
|
public function users() |
83
|
|
|
{ |
84
|
|
|
return $this->belongsToMany(User::class, 'competitor', 'championship_id') |
85
|
|
|
->withPivot('confirmed') |
86
|
|
|
->withTimestamps(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* A championship only has 1 Settings. |
91
|
|
|
* |
92
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
93
|
|
|
*/ |
94
|
|
|
public function settings() |
95
|
|
|
{ |
96
|
|
|
return $this->hasOne(ChampionshipSettings::class); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* A championship has Many Teams. |
101
|
|
|
* |
102
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
103
|
|
|
*/ |
104
|
|
|
public function teams() |
105
|
|
|
{ |
106
|
|
|
return $this->hasMany(Team::class); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if Championship has Preliminary Round Configured. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public function hasPreliminary() |
115
|
|
|
{ |
116
|
|
|
return $this->settings == null || $this->settings->hasPreliminary; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check if 2nd Round of Championship is Round Robin. |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
|
|
public function isPlayOffType() |
125
|
|
|
{ |
126
|
|
|
return $this->settings != null && $this->settings->treeType == ChampionshipSettings::PLAY_OFF; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check if 2nd Round of Championship is Direct Elimination. |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function isDirectEliminationType() |
135
|
|
|
{ |
136
|
|
|
return $this->settings != null && $this->settings->treeType == ChampionshipSettings::DIRECT_ELIMINATION; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* A championship has Many Groups of Fighters. |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
143
|
|
|
*/ |
144
|
|
|
public function fightersGroups() |
145
|
|
|
{ |
146
|
|
|
return $this->hasMany(FightersGroup::class); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* A championship has Many fights. |
151
|
|
|
* |
152
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
153
|
|
|
*/ |
154
|
|
|
public function fights() |
155
|
|
|
{ |
156
|
|
|
return $this->hasManyThrough(Fight::class, FightersGroup::class); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get the fights that happen to the first round |
161
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
162
|
|
|
*/ |
163
|
|
|
public function firstRoundFights() |
164
|
|
|
{ |
165
|
|
|
return $this->hasManyThrough(Fight::class, FightersGroup::class) |
166
|
|
|
->where('fighters_groups.round', 1) |
167
|
|
|
->orderby('fighters_groups.id', 'desc'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function hasNoCustomSettings() |
171
|
|
|
{ |
172
|
|
|
return |
173
|
|
|
$this->settings == null || |
174
|
|
|
($this->settings->ageCategory == null || $this->settings->ageCategory == 0) && |
175
|
|
|
$this->settings->ageMin == null && |
176
|
|
|
$this->settings->ageMax == null && |
177
|
|
|
$this->settings->gradeMin == null && |
178
|
|
|
$this->settings->gradeMax == null && |
179
|
|
|
($this->settings->gradeCategory == null || $this->settings->gradeCategory == 0); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function buildName() |
183
|
|
|
{ |
184
|
|
|
if ($this->settings != null && $this->settings->alias != null && $this->settings->alias != '') |
185
|
|
|
return $this->settings->alias; |
186
|
|
|
|
187
|
|
|
if ($this->hasNoCustomSettings()) { |
188
|
|
|
return $this->category->name; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$genders = [ |
192
|
|
|
'M' => trans('categories.male'), |
193
|
|
|
'F' => trans('categories.female'), |
194
|
|
|
'X' => trans('categories.mixt') |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
$teamText = $this->category->isTeam == 1 ? trans_choice('core.team', 1) : trans('categories.single'); |
198
|
|
|
$ageCategoryText = $this->category->getAgeString(); |
199
|
|
|
$gradeText = $this->category->getGradeString(); |
200
|
|
|
|
201
|
|
|
return $teamText . ' ' . $genders[$this->category->gender] . ' ' . $ageCategoryText . ' ' . $gradeText; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getSettings() |
205
|
|
|
{ |
206
|
|
|
return $setting = $this->settings ?? new ChampionshipSettings(ChampionshipSettings::DEFAULT_SETTINGS); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Return Groups that belongs to a round |
211
|
|
|
* @param $round |
212
|
|
|
* @return HasMany |
213
|
|
|
*/ |
214
|
|
|
public function groupsByRound($round) |
215
|
|
|
{ |
216
|
|
|
return $this->fightersGroups()->where('round',$round); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Return Fights that belongs to a round |
221
|
|
|
* |
222
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough |
223
|
|
|
*/ |
224
|
|
|
public function fightsByRound($round) |
225
|
|
|
{ |
226
|
|
|
return $this->hasManyThrough(Fight::class, FightersGroup::class)->where('round',$round); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.