1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace BristolSU\AirTable\Progress; |
5
|
|
|
|
6
|
|
|
use BristolSU\AirTable\Jobs\CreateRecords; |
7
|
|
|
use BristolSU\AirTable\Jobs\FlushRows; |
8
|
|
|
use BristolSU\Support\ActivityInstance\Contracts\ActivityInstanceRepository; |
9
|
|
|
use BristolSU\Support\ModuleInstance\Contracts\ModuleInstanceRepository; |
10
|
|
|
use BristolSU\Support\ModuleInstance\ModuleInstance; |
11
|
|
|
use BristolSU\Support\Progress\Handlers\Handler; |
12
|
|
|
use BristolSU\Support\Progress\ModuleInstanceProgress; |
13
|
|
|
use BristolSU\Support\Progress\Progress; |
14
|
|
|
use Carbon\Carbon; |
15
|
|
|
use Illuminate\Support\Facades\Log; |
16
|
|
|
|
17
|
|
|
class AirtableHandler implements Handler |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected string $baseId; |
21
|
|
|
protected string $tableName; |
22
|
|
|
protected string $apiKey; |
23
|
|
|
private bool $debug; |
|
|
|
|
24
|
|
|
|
25
|
3 |
|
public function __construct(string $baseId, string $tableName, string $apiKey, bool $debug = false) |
|
|
|
|
26
|
|
|
{ |
27
|
3 |
|
$this->baseId = $baseId; |
28
|
3 |
|
$this->tableName = $tableName; |
29
|
3 |
|
$this->apiKey = $apiKey; |
30
|
3 |
|
$this->debug = $debug; |
31
|
3 |
|
} |
32
|
|
|
|
33
|
3 |
|
protected function filterModules(\Closure $filter, Progress $progress, $moduleInstances) |
|
|
|
|
34
|
|
|
{ |
35
|
3 |
|
return collect($progress->getModules()) |
36
|
|
|
->filter($filter)->map(function (ModuleInstanceProgress $moduleInstanceProgress) use ($moduleInstances) { |
|
|
|
|
37
|
2 |
|
return $moduleInstances[$moduleInstanceProgress->getModuleInstanceId()]; |
38
|
3 |
|
})->values()->toArray(); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
protected function parseProgress(Progress $progress) |
|
|
|
|
42
|
|
|
{ |
43
|
3 |
|
$activityInstance = app(ActivityInstanceRepository::class) |
44
|
3 |
|
->getById($progress->getActivityInstanceId()); |
45
|
3 |
|
$moduleInstances = app(ModuleInstanceRepository::class) |
46
|
3 |
|
->allThroughActivity($activityInstance->activity) |
47
|
|
|
->reduce(function ($carry, ModuleInstance $moduleInstance) { |
|
|
|
|
48
|
2 |
|
$carry[$moduleInstance->id()] = $moduleInstance->name; |
49
|
2 |
|
return $carry; |
50
|
3 |
|
}); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return [ |
53
|
3 |
|
'Participant Name' => $activityInstance->participantName(), |
54
|
|
|
'Mandatory Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
55
|
2 |
|
return $moduleInstanceProgress->isMandatory(); |
56
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
57
|
|
|
'Optional Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
58
|
2 |
|
return !$moduleInstanceProgress->isMandatory(); |
59
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
60
|
|
|
'Complete Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
61
|
2 |
|
return $moduleInstanceProgress->isComplete(); |
62
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
63
|
|
|
'Incomplete Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
64
|
2 |
|
return !$moduleInstanceProgress->isComplete(); |
65
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
66
|
|
|
'Active Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
67
|
2 |
|
return $moduleInstanceProgress->isActive(); |
68
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
69
|
|
|
'Inactive Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
70
|
2 |
|
return !$moduleInstanceProgress->isActive(); |
71
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
72
|
|
|
'Hidden Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
73
|
2 |
|
return !$moduleInstanceProgress->isVisible(); |
74
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
75
|
|
|
'Visible Modules' => $this->filterModules(function (ModuleInstanceProgress $moduleInstanceProgress) { |
|
|
|
|
76
|
2 |
|
return $moduleInstanceProgress->isVisible(); |
77
|
3 |
|
}, $progress, $moduleInstances), |
|
|
|
|
78
|
3 |
|
'% Complete' => $progress->getPercentage(), |
79
|
3 |
|
'Activity Instance ID' => $activityInstance->id, |
80
|
3 |
|
'Activity ID' => $progress->getActivityId(), |
81
|
3 |
|
'Participant ID' => $activityInstance->resource_id, |
82
|
3 |
|
'Snapshot Date' => $progress->getTimestamp()->format(\DateTime::ATOM) |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* @param array|Progress[] $progresses |
|
|
|
|
89
|
|
|
*/ |
|
|
|
|
90
|
3 |
|
public function saveMany(array $progresses): void |
91
|
|
|
{ |
92
|
3 |
|
$data = []; |
93
|
3 |
|
$this->log(sprintf('Parsing %d progresses', count($progresses))); |
94
|
3 |
|
foreach ($progresses as $progress) { |
95
|
3 |
|
$data[] = $this->parseProgress($progress); |
96
|
|
|
} |
97
|
3 |
|
$this->createRecords($data); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
2 |
|
public function save(Progress $progress): void |
|
|
|
|
101
|
|
|
{ |
102
|
2 |
|
$this->saveMany([$progress]); |
103
|
2 |
|
} |
104
|
|
|
|
105
|
3 |
|
protected function createRecords(array $data) |
|
|
|
|
106
|
|
|
{ |
107
|
3 |
|
$this->log('Flushing rows'); |
108
|
3 |
|
dispatch_now( |
109
|
3 |
|
(new FlushRows($this->apiKey, $this->baseId, $this->tableName)) |
110
|
3 |
|
->withDebug($this->debug) |
111
|
|
|
); |
112
|
|
|
|
113
|
3 |
|
$this->log('Flushed rows'); |
114
|
|
|
|
115
|
3 |
|
foreach(array_chunk($data, 10) as $rows) { |
|
|
|
|
116
|
3 |
|
dispatch((new CreateRecords( |
|
|
|
|
117
|
3 |
|
$rows, |
118
|
3 |
|
$this->apiKey, |
119
|
3 |
|
$this->baseId, |
120
|
3 |
|
$this->tableName |
121
|
3 |
|
))->withDebug($this->debug)); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
$this->log('Created Records'); |
125
|
3 |
|
} |
126
|
|
|
|
127
|
3 |
|
private function log(string $string) |
|
|
|
|
128
|
|
|
{ |
129
|
3 |
|
if($this->debug) { |
|
|
|
|
130
|
|
|
Log::debug($string); |
131
|
|
|
} |
132
|
3 |
|
} |
133
|
|
|
} |
134
|
|
|
|