1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\AssignRoles\CompletionConditions; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Contracts\Models\Group; |
6
|
|
|
use BristolSU\ControlDB\Contracts\Models\Role; |
7
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Position; |
8
|
|
|
use BristolSU\ControlDB\Contracts\Repositories\Role as RoleRepository; |
9
|
|
|
use BristolSU\Module\AssignRoles\Fields\RequiredPositions; |
10
|
|
|
use BristolSU\Module\AssignRoles\Support\RequiredSettingRetrieval; |
11
|
|
|
use BristolSU\Module\AssignRoles\Support\SettingRetrievalException; |
12
|
|
|
use BristolSU\Support\ActivityInstance\ActivityInstance; |
13
|
|
|
use BristolSU\Support\Completion\Contracts\CompletionCondition; |
14
|
|
|
use BristolSU\Support\Logic\Contracts\LogicRepository; |
15
|
|
|
use BristolSU\Support\Logic\Facade\LogicTester; |
16
|
|
|
use BristolSU\Support\ModuleInstance\Contracts\ModuleInstance; |
17
|
|
|
use FormSchema\Generator\Field; |
18
|
|
|
use FormSchema\Schema\Form; |
19
|
|
|
|
20
|
|
|
class RequiredPositionsFilled extends CompletionCondition |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @var RoleRepository |
25
|
|
|
*/ |
26
|
|
|
private $roleRepository; |
|
|
|
|
27
|
|
|
|
28
|
2 |
|
public function __construct(string $moduleAlias, RoleRepository $roleRepository) |
|
|
|
|
29
|
|
|
{ |
30
|
2 |
|
parent::__construct($moduleAlias); |
31
|
2 |
|
$this->roleRepository = $roleRepository; |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
|
|
|
|
37
|
2 |
|
public function isComplete($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): bool |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
2 |
|
$remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance); |
41
|
|
|
} catch (SettingRetrievalException $e) { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
return count($remainingPositions) === 0; |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function percentage($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): int |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$group = $this->getGroup($activityInstance); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$requiredPositions = $this->getRequiredPositions($settings, $group, $moduleInstance); |
54
|
|
|
$remainingPositions = $this->positionsStillToFill($settings, $activityInstance, $moduleInstance); |
55
|
|
|
} catch (SettingRetrievalException $e) { |
56
|
|
|
return false; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if(count($requiredPositions) === 0) { |
|
|
|
|
60
|
|
|
return 100; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$filled = count($requiredPositions) - count($remainingPositions); |
64
|
|
|
|
65
|
|
|
$percentage = (int) round(($filled/count($requiredPositions)) * 100, 0); |
66
|
|
|
|
67
|
|
|
if($percentage > 100) { |
|
|
|
|
68
|
|
|
return 100; |
69
|
|
|
} |
70
|
|
|
return $percentage; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
protected function positionsStillToFill($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance) |
|
|
|
|
74
|
|
|
{ |
75
|
2 |
|
$group = $this->getGroup($activityInstance); |
76
|
|
|
$roles = $this->rolesThroughGroup($group, $moduleInstance)->filter(function(Role $role) { |
|
|
|
|
77
|
2 |
|
return $role->users()->count() > 0; |
78
|
2 |
|
}); |
|
|
|
|
79
|
|
|
|
80
|
|
|
return collect($this->getRequiredPositions($settings, $group, $moduleInstance))->filter(function(int $positionId) use ($roles) { |
|
|
|
|
81
|
|
|
return $roles->filter(function(Role $role) use ($positionId) { |
|
|
|
|
82
|
2 |
|
return $role->positionId() === $positionId; |
83
|
2 |
|
})->count() === 0; |
|
|
|
|
84
|
2 |
|
}); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
|
|
|
|
90
|
|
|
public function options(): Form |
91
|
|
|
{ |
92
|
|
|
return \FormSchema\Generator\Form::make()->withField( |
93
|
|
|
Field::make(RequiredPositions::class, 'required') |
94
|
|
|
->label('Required Positions')->featured(true)->required(true) |
95
|
|
|
->default([])->hint('Define which positions are required') |
96
|
|
|
->help('Define the positions that are required for any given logic group') |
97
|
|
|
->logic($this->getLogic()) |
98
|
|
|
->positions($this->getPositions()) |
99
|
|
|
)->getSchema(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
|
|
|
|
105
|
|
|
public function name(): string |
106
|
|
|
{ |
107
|
|
|
return 'Required Positions are filled'; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* @inheritDoc |
112
|
|
|
*/ |
|
|
|
|
113
|
|
|
public function description(): string |
114
|
|
|
{ |
115
|
|
|
return 'Positions that are marked as required for a group have been filled'; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
|
|
|
|
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
|
|
|
|
121
|
|
|
public function alias(): string |
122
|
|
|
{ |
123
|
|
|
return 'required_positions_are_filled'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function getLogic() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
return collect(app(LogicRepository::class)->all()); |
130
|
|
|
} catch (\Exception $e) { |
131
|
|
|
return collect(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
private function getPositions() |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
return collect(app(Position::class)->all()); |
139
|
|
|
} catch (\Exception $e) { |
140
|
|
|
return collect(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the group from an activity instance |
146
|
|
|
* |
147
|
|
|
* @param ActivityInstance $activityInstance |
|
|
|
|
148
|
|
|
* @return Group|\BristolSU\ControlDB\Models\Group |
|
|
|
|
149
|
|
|
*/ |
150
|
2 |
|
private function getGroup(ActivityInstance $activityInstance) |
|
|
|
|
151
|
|
|
{ |
152
|
2 |
|
if($activityInstance->resource_type === 'group') { |
|
|
|
|
153
|
2 |
|
return $activityInstance->participant(); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
return $activityInstance->participant()->group(); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
private function getRequiredPositions(array $settings, Group $group, ModuleInstance $moduleInstance) |
|
|
|
|
159
|
|
|
{ |
160
|
2 |
|
return app(RequiredSettingRetrieval::class)->getSettings($group, $settings, $moduleInstance); |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
|
private function rolesThroughGroup(Group $group, ModuleInstance $moduleInstance) |
|
|
|
|
164
|
|
|
{ |
165
|
2 |
|
$roles = $group->roles(); |
166
|
2 |
|
if($this->logicGroupId($moduleInstance) !== null) { |
|
|
|
|
167
|
|
|
$logicGroup = app(LogicRepository::class)->getById($this->logicGroupId($moduleInstance)); |
|
|
|
|
168
|
|
|
return $roles->filter(function(\BristolSU\ControlDB\Contracts\Models\Role $role) use ($moduleInstance, $logicGroup) { |
|
|
|
|
169
|
|
|
return LogicTester::evaluate($logicGroup, null, $role->group(), $role); |
170
|
|
|
})->values(); |
|
|
|
|
171
|
|
|
} |
172
|
2 |
|
return $roles; |
173
|
|
|
} |
174
|
|
|
|
175
|
2 |
|
private function logicGroupId(ModuleInstance $moduleInstance) |
|
|
|
|
176
|
|
|
{ |
177
|
2 |
|
$id = $moduleInstance->setting('logic_group', null); |
178
|
2 |
|
if($id === null) { |
|
|
|
|
179
|
2 |
|
return null; |
180
|
|
|
} |
181
|
|
|
return (int) $id; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |