1 | <?php |
||
2 | |||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
3 | |||
4 | namespace BristolSU\Module\StaticPage\CompletionConditions; |
||
5 | |||
6 | |||
7 | use BristolSU\Module\StaticPage\Models\PageView; |
||
8 | use BristolSU\Support\ActivityInstance\ActivityInstance; |
||
9 | use BristolSU\Support\Completion\Contracts\CompletionCondition; |
||
10 | use BristolSU\Support\ModuleInstance\Contracts\ModuleInstance; |
||
11 | use FormSchema\Schema\Form; |
||
12 | |||
13 | class HasViewedPage extends CompletionCondition |
||
0 ignored issues
–
show
|
|||
14 | { |
||
15 | |||
16 | /** |
||
17 | * Is the condition fully complete? |
||
18 | * |
||
19 | * @param array $settings Settings of the completion condition |
||
0 ignored issues
–
show
|
|||
20 | * @param ActivityInstance $activityInstance Activity instance to test |
||
0 ignored issues
–
show
|
|||
21 | * @param ModuleInstance $moduleInstance Module instance to test |
||
0 ignored issues
–
show
|
|||
22 | * @return bool If the condition is complete |
||
0 ignored issues
–
show
|
|||
23 | */ |
||
24 | 3 | public function isComplete($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): bool |
|
25 | { |
||
26 | 3 | return PageView::forResource($activityInstance->id, $moduleInstance->id)->count() >= ( $settings['number_of_views'] ?? 1); |
|
0 ignored issues
–
show
|
|||
27 | } |
||
28 | |||
29 | 5 | public function percentage($settings, ActivityInstance $activityInstance, ModuleInstance $moduleInstance): int |
|
0 ignored issues
–
show
|
|||
30 | { |
||
31 | 5 | $count = PageView::forResource($activityInstance->id, $moduleInstance->id)->count(); |
|
0 ignored issues
–
show
|
|||
32 | 5 | $needed = ( $settings['number_of_views'] ?? 1); |
|
33 | |||
34 | 5 | $percentage = (int) round(($count/$needed) * 100, 0); |
|
35 | |||
36 | 5 | if($percentage > 100) { |
|
0 ignored issues
–
show
|
|||
37 | 1 | return 100; |
|
38 | } |
||
39 | 4 | return $percentage; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Options required by the completion condition. |
||
44 | * |
||
45 | * Any settings requested in here will be passed into the percentage or isComplete methods. |
||
46 | * |
||
47 | * @return Form |
||
48 | * @throws \Exception |
||
49 | */ |
||
50 | 1 | public function options(): Form |
|
51 | { |
||
52 | 1 | return \FormSchema\Generator\Form::make()->withField( |
|
53 | 1 | \FormSchema\Generator\Field::input('number_of_views')->inputType('number')->label('Number of Views') |
|
54 | 1 | ->required(true)->default(1)->hint('The number of times a user needs to view the page') |
|
55 | 1 | ->help('The number of times a user should view the page before the module is marked as complete. 1 will mark the module as complete on the first view, 2 on the second etc.') |
|
56 | 1 | )->getSchema(); |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * A name for the completion condition |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | 1 | public function name(): string |
|
65 | { |
||
66 | 1 | return 'Has viewed the page'; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * A description of the completion condition |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | 1 | public function description(): string |
|
75 | { |
||
76 | 1 | return 'Marked as complete when the user/group/role has viewed the page a number of times.'; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * The alias of the completion condition |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | 1 | public function alias(): string |
|
85 | { |
||
86 | 1 | return 'static_page_has_viewed_page'; |
|
87 | } |
||
88 | } |