1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scool\EnrollmentMobile\Models; |
4
|
|
|
|
5
|
|
|
use Acacha\Names\Traits\Nameable; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
//use Scool\Curriculum\Traits\HasManyFamilies; |
|
|
|
|
8
|
|
|
//use Scool\Curriculum\Traits\HasStudies; |
9
|
|
|
use Scool\EnrollmentMobile\Traits\HasManyFamilies; |
10
|
|
|
use Scool\EnrollmentMobile\Traits\HasStudies; |
11
|
|
|
use Scool\Foundation\User; |
12
|
|
|
|
13
|
|
|
class Department extends Model |
14
|
|
|
{ |
15
|
|
|
use HasManyFamilies, Nameable, HasStudies , Nameable; |
16
|
|
|
/** |
17
|
|
|
* The attributes that are mass assignable. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $fillable = ['name']; |
22
|
|
|
/** |
23
|
|
|
* Get the subdepartments for the department. |
24
|
|
|
*/ |
25
|
|
|
public function subdepartments() |
26
|
|
|
{ |
27
|
|
|
return $this->hasMany(Department::class, 'parent'); |
28
|
|
|
} |
29
|
|
|
/** |
30
|
|
|
* Get the parent department. |
31
|
|
|
*/ |
32
|
|
|
public function parent() |
33
|
|
|
{ |
34
|
|
|
return $this->belongsTo(Department::class, 'parent'); |
35
|
|
|
} |
36
|
|
|
/** |
37
|
|
|
* The heads that belong to the department. |
38
|
|
|
*/ |
39
|
|
|
public function heads() |
40
|
|
|
{ |
41
|
|
|
return $this->belongsToMany(User::class, 'department_head', 'department_id', 'user_id') |
42
|
|
|
->withPivot('main'); |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* The principal head that belong to the department. |
46
|
|
|
*/ |
47
|
|
|
public function head() |
48
|
|
|
{ |
49
|
|
|
return $this->heads()->wherePivot('main', 1)->first(); |
50
|
|
|
} |
51
|
|
|
/** |
52
|
|
|
* Set the principal head. |
53
|
|
|
* |
54
|
|
|
* @param User $user |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function setHeadAttribute(User $user) |
58
|
|
|
{ |
59
|
|
|
foreach ($this->heads as $head) { |
|
|
|
|
60
|
|
|
$this->heads()->updateExistingPivot($head->id, ['main' => false]); |
61
|
|
|
} |
62
|
|
|
if (! $this->heads->contains($user)) { |
|
|
|
|
63
|
|
|
$this->heads()->save($user, ['main' => true]); |
64
|
|
|
} else { |
65
|
|
|
$this->heads()->updateExistingPivot($user->id, ['main' => true]); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.