-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUser.php
512 lines (432 loc) · 14.1 KB
/
User.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
namespace App;
use App\Achievements\Achievement;
use App\Helpers\EventHelper;
use App\Helpers\TagsHelper;
use Attribute;
use Cache;
use Carbon\Carbon;
use DB;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Spatie\Permission\Traits\HasRoles;
use App\Filters\UserFilters;
/**
* App\User
*
* @property int $id
* @property string $firstname
* @property string $lastname
* @property string $username
* @property string $password
* @property string|null $email
* @property string|null $country_iso
* @property string|null $twitter
* @property string|null $website
* @property string|null $bio
* @property string $avatar_path
* @property string|null $provider
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $privacy
* @property string|null $email_display
* @property int $receive_emails
* @property-read \App\Country|null $country
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Event[] $events
* @property-read int|null $events_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Excellence[] $excellences
* @property-read int|null $excellences_count
* @property mixed $ambassador
* @property-read string $avatar
* @property-read mixed $full_name
* @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
* @property-read int|null $notifications_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Participation[] $participations
* @property-read int|null $participations_count
* @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Permission[] $permissions
* @property-read int|null $permissions_count
* @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Permission\Models\Role[] $roles
* @property-read int|null $roles_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\School[] $schools
* @property-read int|null $schools_count
* @method static \Illuminate\Database\Eloquent\Builder|User filter(\App\Filters\UserFilters $filters)
* @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
* @method static \Illuminate\Database\Query\Builder|User onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|User permission($permissions)
* @method static \Illuminate\Database\Eloquent\Builder|User query()
* @method static \Illuminate\Database\Eloquent\Builder|User role($roles, $guard = null)
* @method static \Illuminate\Database\Eloquent\Builder|User whereAvatarPath($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereBio($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereCountryIso($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereEmail($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereEmailDisplay($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereFirstname($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereLastname($value)
* @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
* @method static \Illuminate\Database\Eloquent\Builder|User wherePrivacy($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereProvider($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereReceiveEmails($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereTwitter($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereUsername($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereWebsite($value)
* @method static \Illuminate\Database\Query\Builder|User withTrashed()
* @method static \Illuminate\Database\Query\Builder|User withoutTrashed()
* @mixin \Eloquent
*/
class User extends Authenticatable
{
use Notifiable;
use HasRoles;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
'magic_key'
];
protected $appends = ['fullName'];
protected $dates = ['consent_given_at', 'future_consent_given_at'];
public function getName()
{
if (!empty($this->username)) {
return $this->username;
}
if (!empty($this->firstname) && !empty($this->lastname)) {
return $this->firstname . " " . $this->lastname;
}
if (!empty($this->firstname) && empty($this->lastname)) {
return $this->firstname;
}
return $this->email;
}
public function getAmbassadorAttribute()
{
return $this->isAmbassador();
}
public function setAmbassadorAttribute($value)
{
// Log::info($value);
if ($value) {
$this->assignRole('ambassador');
} else {
$this->removeRole('ambassador');
}
}
public function achievements()
{
return $this->belongsToMany(Achievement::class, 'user_achievements')->withTimestamps();
}
public function getLeadingTeacherAttribute()
{
return $this->isLeadingTeacher();
}
public function setLeadingTeacherAttribute($value)
{
if ($value) {
$this->assignRole('leading teacher');
} else {
$this->removeRole('leading teacher');
}
}
public function isAdmin()
{
return $this->hasRole("super admin");
}
public function isAmbassador()
{
return $this->hasRole("ambassador");
}
public function isLeadingTeacher()
{
return $this->hasRole("leading teacher");
}
public function isLeadingTeacherAdmin()
{
return $this->hasRole("leading teacher admin");
}
public function events()
{
return $this->hasMany('App\Event', 'creator_id');
}
public function schools()
{
return $this->belongsToMany('App\School');
}
public function country()
{
return $this->belongsTo('App\Country', 'country_iso', 'iso');
}
public function excellences()
{
return $this->hasMany('App\Excellence')->where('type', "Excellence");
}
public function superOrganisers()
{
return $this->hasMany('App\Excellence')->where('type', "SuperOrganiser");
}
public function participations()
{
return $this->hasMany('App\Participation');
}
public function expertises()
{
return $this->belongsToMany(LeadingTeacherExpertise::class, 'leading_teacher_expertise_user', 'user_id',
'lte_id');
}
public function levels()
{
return $this->belongsToMany(ResourceLevel::class);
}
public function subjects()
{
return $this->belongsToMany(ResourceSubject::class);
}
public function city()
{
return $this->belongsTo(City::class);
}
public function locations()
{
return $this->hasMany(Location::class);
}
public function scopeFilter($query, UserFilters $filters)
{
return $filters->apply($query);
}
public function experience()
{
return $this->hasOne(Experience::class, 'user_id', 'id');
}
public function actions()
{
return $this->hasMany(LeadingTeacherAction::class);
}
public function resetExperience($year = null)
{
if (is_null($year)) {
$year = Carbon::now()->year;
}
$this->getExperience($year)->update(
["points" => 0]
);
}
public function getPoints($year = null)
{
if (is_null($year)) {
$year = Carbon::now()->year;
}
return $this->getExperience($year)->points;
}
public function getExperience($year = null)
{
if (is_null($year)) {
$year = Carbon::now()->year;
}
$experience = Experience::firstOrCreate(
[
'user_id' => $this->id,
'year' => $year
],
[
'points' => 0
]
);
return $experience;
}
public function awardExperience($points, $year = null)
{
if (is_null($year)) {
$year = Carbon::now()->year;
}
$this->getExperience($year)->awardExperience($points);
}
public function stripExperience($points, $year = null)
{
if (is_null($year)) {
$year = Carbon::now()->year;
}
$this->getExperience($year)->stripExperience($points);
}
/**
* Get the path to the user's avatar.
*
* @param string $avatar
* @return string
*/
public function getAvatarPathAttribute($avatar)
{
if (is_null($avatar)) {
$avatar = 'avatars/default_avatar.png';
}
return Storage::disk('s3')->url($avatar);
}
/**
* Get the path to the user's avatar.
*
* @param string $avatar
* @return string
*/
public function getAvatarAttribute()
{
$arr = explode("/", $this->avatar_path);
$filename = array_pop($arr);
array_push($arr, $filename);
$glued = implode("/", $arr);
return $glued;
}
/**
* Get a string path for the thread.
*
* @return string
*/
public function fullName()
{
return $this->firstname . " " . $this->lastname;
}
/**
* Fetch the path to the thread as a property.
*/
public function getFullNameAttribute()
{
return $this->fullName();
}
public static function getGeoIPData()
{
return geoip(geoip()->getClientIP());
}
public function activities($edition)
{
return DB::table('events')
->where('creator_id', '=', $this->id)
->where('status', "=", "APPROVED")
->whereNull('deleted_at')
->whereYear('end_date', '=', $edition)
->count();
}
public function reported($edition = null)
{
$query = DB::table('events')
->where('creator_id', '=', $this->id)
->where('status', "=", "APPROVED")
->whereNotNull('reported_at')
->whereNull('deleted_at');
if (!is_null($edition)) {
$query->whereYear('created_at', '=', $edition);
}
return $query->count();
}
public function influence($edition = null)
{
Log::info("Influence for $this->email for edition $edition");
if (is_null($this->tag)) {
return 0;
}
// $nameInTag = TagsHelper::getNameInTag($this->tag);
// $key = $nameInTag . '-' . $edition;
//
// $cache_timeout = 0;
// if (app()->runningInConsole() && !app()->runningUnitTests()) {
// $cache_timeout = 3000;
// }
// $result = Cache::remember($key, $cache_timeout, function () use ($nameInTag, $edition) {
// Log::info("$nameInTag - $edition not in cache");
$taggedActivities = $this->taggedActivities()
->where('status', '=', 'APPROVED');
if (!is_null($edition)) {
$taggedActivities->whereYear('events.created_at', '=', $edition);
}
$result = $taggedActivities->count() * 2;
// });
// Log::info("Name in Tag: $nameInTag - $result");
return $result;
}
public function generateMagicKey()
{
$this->update([
'magic_key' => random_int(1000000, 2000000) * random_int(1000, 2000)
]);
}
public function unsubscribe()
{
$this->update([
'receive_emails' => false
]);
}
public function getEventsToReviewCount()
{
if (auth()->user()->isAmbassador()) {
return EventHelper::getPendingEventsCount($this->country_iso);
}
if (auth()->user()->isAdmin()) {
if (!is_null($this->current_country)) {
return EventHelper::getPendingEventsCount($this->current_country);
}
return EventHelper::getPendingEventsCount();
}
return null;
}
public function getNextPendingEvent(Event $event)
{
if (auth()->user()->isAmbassador()) {
return EventHelper::getNextPendingEvent($event, $this->country_iso);
}
if (auth()->user()->isAdmin()) {
if (!is_null($this->current_country)) {
return EventHelper::getNextPendingEvent($event, $this->current_country);
}
return EventHelper::getNextPendingEvent($event);
}
return null;
}
public function setCurrentCountry($country)
{
$this->update(['current_country' => $country]);
}
public function taggedActivities()
{
return $this->hasMany('App\Event', 'leading_teacher_tag', 'tag');
}
public function hasGivenConsent()
{
return $this->consent_given_at !== null;
}
public function hasGivenFutureConsent()
{
return $this->future_consent_given_at !== null;
}
public function giveConsent()
{
if (!$this->hasGivenConsent()) {
$this->consent_given_at = now();
$this->save();
}
}
public function giveFutureConsent()
{
if (!$this->hasGivenFutureConsent()) {
$this->future_consent_given_at = now();
$this->save();
}
}
}