1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\Module\UploadFile\CompletionConditions; |
4
|
|
|
|
5
|
|
|
use BristolSU\Module\UploadFile\Models\File; |
6
|
|
|
use BristolSU\Support\ActivityInstance\ActivityInstance; |
7
|
|
|
use BristolSU\Support\Completion\Contracts\CompletionCondition; |
8
|
|
|
use BristolSU\Support\ModuleInstance\Contracts\ModuleInstance; |
9
|
|
|
use FormSchema\Schema\Form; |
10
|
|
|
|
11
|
|
|
class NumberOfDocumentsWithStatus extends CompletionCondition |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
|
14
|
4 |
|
public function isComplete($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): bool |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
return File::forResource($activityInstance->id, $moduleInstance->id())->get()->filter(function(File $file) use ($settings) { |
|
|
|
|
17
|
4 |
|
return $file->status === $settings['status']; |
18
|
4 |
|
})->count() >= $settings['number_of_files']; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
4 |
|
public function percentage($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): int |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$count = File::forResource($activityInstance->id, $moduleInstance->id())->get()->filter(function(File $file) use ($settings) { |
|
|
|
|
24
|
3 |
|
return $file->status === $settings['status']; |
25
|
4 |
|
})->count(); |
|
|
|
|
26
|
4 |
|
$needed = ( $settings['number_of_files'] ?? 1); |
27
|
|
|
|
28
|
4 |
|
$percentage = (int) round(($count/$needed) * 100, 0); |
29
|
|
|
|
30
|
4 |
|
if($percentage > 100) { |
|
|
|
|
31
|
1 |
|
return 100; |
32
|
|
|
} |
33
|
3 |
|
return $percentage; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
1 |
|
public function options(): Form |
|
|
|
|
38
|
|
|
{ |
39
|
1 |
|
return \FormSchema\Generator\Form::make()->withField( |
40
|
1 |
|
\FormSchema\Generator\Field::input('number_of_files')->inputType('number')->label('Number of Files') |
41
|
1 |
|
->required(true)->default(1)->hint('The number of files that must be submitted') |
42
|
1 |
|
->help('The number of documents that need to be submitted for the module to be marked as complete.') |
43
|
1 |
|
)->withField( |
44
|
1 |
|
\FormSchema\Generator\Field::input('status')->inputType('text')->label('Status') |
45
|
1 |
|
->required(true)->default('Awaiting Approval')->hint('The status the files must be') |
46
|
1 |
|
->help('The status of the files. The module will be complete when the number of files submitted have this status.') |
47
|
1 |
|
)->getSchema(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function name(): string |
|
|
|
|
51
|
|
|
{ |
52
|
1 |
|
return 'A number of documents with a given status have been submitted'; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
public function description(): string |
|
|
|
|
56
|
|
|
{ |
57
|
1 |
|
return 'Complete when a given number of documents have been submitted and given a specific status'; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function alias(): string |
|
|
|
|
61
|
|
|
{ |
62
|
1 |
|
return 'number_of_files_submitted_with_status'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |