PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.4.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.4.01
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / Modules / Integrations / FluentCRM / CourseCompletedTrigger.php

CourseCompletedTrigger.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.4.01, at Modules/Integrations/FluentCRM/CourseCompletedTrigger.php

157 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Modules\Integrations\FluentCRM;
4
5 use FluentCommunity\App\Models\Space;
6 use FluentCommunity\Modules\Course\Model\Course;
7 use FluentCrm\App\Services\Funnel\BaseTrigger;
8 use FluentCrm\App\Services\Funnel\FunnelHelper;
9 use FluentCrm\App\Services\Funnel\FunnelProcessor;
10 use FluentCrm\Framework\Support\Arr;
11
12 class CourseCompletedTrigger extends BaseTrigger
13 {
14 public function __construct()
15 {
16 $this->triggerName = 'fluent_community/course/completed';
17 $this->priority = 31;
18 $this->actionArgNum = 2;
19 parent::__construct();
20 }
21
22 public function getTrigger()
23 {
24 return [
25 'category' => __('Community', 'fluent-community'),
26 'label' => __('Completed a course', 'fluent-community'),
27 'description' => __('This automation will be initiated when a user completes course.', 'fluent-community'),
28 'icon' => 'fc-icon-wp_new_user_signup',
29 'svg_icon' => '<svg viewBox="0 0 64 64" fill="none"><rect x="12" y="10" width="40" height="34" rx="6" stroke="currentColor" stroke-width="3"/><line x1="20" y1="20" x2="44" y2="20" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><line x1="20" y1="28" x2="38" y2="28" stroke="currentColor" stroke-width="3" stroke-linecap="round"/><circle cx="46" cy="42" r="9" fill="currentColor"/><path d="M42.8 42.3l2.6 2.6L49.5 39.8" stroke="#fff" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"/><path d="M24 44v12l6-4 6 4V44" stroke="currentColor" stroke-width="3" stroke-linejoin="round"/></svg>'
30 ];
31 }
32
33 public function getSettingsFields($funnel)
34 {
35 return [
36 'title' => __('Completed a course', 'fluent-community'),
37 'sub_title' => __('This automation will be initiated when a user completes a course.', 'fluent-community'),
38 'fields' => [
39 'subscription_status' => [
40 'type' => 'option_selectors',
41 'option_key' => 'editable_statuses',
42 'is_multiple' => false,
43 'label' => __('Subscription Status', 'fluent-community'),
44 'placeholder' => __('Select Status', 'fluent-community')
45 ],
46 'subscription_status_info' => [
47 'type' => 'html',
48 'info' => '<b>' . __('An Automated double-opt-in email will be sent for new subscribers', 'fluent-community') . '</b>',
49 'dependency' => [
50 'depends_on' => 'subscription_status',
51 'operator' => '=',
52 'value' => 'pending'
53 ]
54 ]
55 ]
56 ];
57 }
58
59 public function getFunnelSettingsDefaults()
60 {
61 return [
62 'subscription_status' => 'subscribed'
63 ];
64 }
65
66 public function getConditionFields($funnel)
67 {
68 $courses = Course::orderBy('title', 'ASC')->select(['id', 'title'])->get();
69
70 return [
71 'update_type' => [
72 'type' => 'radio',
73 'label' => __('If Contact Already Exists?', 'fluent-community'),
74 'help' => __('Please specify what will happen if the subscriber already exists in the database', 'fluent-community'),
75 'options' => FunnelHelper::getUpdateOptions()
76 ],
77 'course_ids' => [
78 'type' => 'multi-select',
79 'is_multiple' => true,
80 'label' => __('Targeted Courses', 'fluent-community'),
81 'help' => __('Select which courses this automation is for.', 'fluent-community'),
82 'placeholder' => __('Select Courses', 'fluent-community'),
83 'options' => $courses,
84 'inline_help' => __('Leave blank to run for all courses', 'fluent-community')
85 ],
86 'run_multiple' => [
87 'type' => 'yes_no_check',
88 'label' => '',
89 'check_label' => __('Restart the automation multiple times for a contact for this event. (Only enable this if you want to restart the automation for the same contact)', 'fluent-community'),
90 'inline_help' => __('If you enable this, it will restart the automation for a contact even if they are already in the automation. Otherwise, it will skip if the contact already exists.', 'fluent-community')
91 ]
92 ];
93 }
94
95 public function getFunnelConditionDefaults($funnel)
96 {
97 return [
98 'update_type' => 'update', // skip_all_actions, skip_update_if_exist
99 'course_ids' => [],
100 'run_multiple' => 'yes'
101 ];
102 }
103
104 public function handle($funnel, $originalArgs)
105 {
106 $course = $originalArgs[0];
107 $userId = $originalArgs[1];
108
109 $user = get_user_by('ID', $userId);
110
111 if (!$user || !$this->isProcessable($funnel, $user, $course)) {
112 return false;
113 }
114
115 $subscriberData = FunnelHelper::prepareUserData($user);
116
117 $subscriberData = wp_parse_args($subscriberData, $funnel->settings);
118 $subscriberData['status'] = $subscriberData['subscription_status'];
119 unset($subscriberData['subscription_status']);
120
121 (new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [
122 'source_trigger_name' => $this->triggerName,
123 'source_ref_id' => $course->id
124 ]);
125 }
126
127 private function isProcessable($funnel, $user, $course)
128 {
129 $conditions = $funnel->conditions;
130 // check update_type
131 $updateType = Arr::get($conditions, 'update_type');
132
133 $subscriber = FunnelHelper::getSubscriber($user->user_email);
134 if ($updateType == 'skip_all_if_exist' && $subscriber) {
135 return false;
136 }
137
138 // check user roles
139 if ($checkIds = Arr::get($conditions, 'course_ids', [])) {
140 if (!in_array($course->id, $checkIds)) {
141 return false;
142 }
143 }
144
145 // check run_only_one
146 if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) {
147 $multipleRun = Arr::get($conditions, 'run_multiple') == 'yes';
148 if ($multipleRun) {
149 FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]);
150 }
151 return $multipleRun;
152 }
153
154 return true;
155 }
156 }
157