PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.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 / CourseTopicCompletedTrigger.php

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

190 lines 7.2 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\Modules\Course\Model\Course;
6 use FluentCommunity\Modules\Course\Model\CourseTopic;
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 CourseTopicCompletedTrigger extends BaseTrigger
13 {
14 public function __construct()
15 {
16 $this->triggerName = 'fluent_community/course/topic_completed';
17 $this->priority = 50;
18 $this->actionArgNum = 2;
19 parent::__construct();
20 }
21
22 public function getTrigger()
23 {
24 return [
25 'category' => __('Community', 'fluent-community'),
26 'label' => __('Section/Topic Completed', 'fluent-community'),
27 'icon' => 'fc-icon-wp_new_user_signup',
28 'svg_icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none"><path d="M4 6a3 3 0 0 1 3-3h4v14H7a3 3 0 0 0-3 3V6zM20 6a3 3 0 0 0-3-3h-4v14h4a3 3 0 0 1 3 3V6z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><circle cx="18" cy="18" r="4" fill="currentColor"/><path d="M16.5 18l1.3 1.3L20 17" stroke="#fff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
29 'description' => __('This Automation runs when a student completes a Section/Topic', 'fluent-community')
30 ];
31 }
32
33 public function getFunnelSettingsDefaults()
34 {
35 return [
36 'subscription_status' => 'subscribed'
37 ];
38 }
39
40 public function getSettingsFields($funnel)
41 {
42 return [
43 'title' => __('Student completes a Section/Topic in FluentCommunity', 'fluent-community'),
44 'sub_title' => __('This Funnel will start when a student completes a Section/Topic', 'fluent-community'),
45 'fields' => [
46 'subscription_status' => [
47 'type' => 'option_selectors',
48 'option_key' => 'editable_statuses',
49 'is_multiple' => false,
50 'label' => __('Subscription Status', 'fluent-community'),
51 'placeholder' => __('Select Status', 'fluent-community')
52 ],
53 'subscription_status_info' => [
54 'type' => 'html',
55 'info' => '<b>' . __('An Automated double-opt-in email will be sent for new subscribers', 'fluent-community') . '</b>',
56 'dependency' => [
57 'depends_on' => 'subscription_status',
58 'operator' => '=',
59 'value' => 'pending'
60 ]
61 ]
62 ]
63 ];
64 }
65
66 public function getFunnelConditionDefaults($funnel)
67 {
68 return [
69 'update_type' => 'update', // skip_all_actions, skip_update_if_exist
70 'topic_ids' => []
71 ];
72 }
73
74 public function getConditionFields($funnel)
75 {
76 return [
77 'update_type' => [
78 'type' => 'radio',
79 'label' => __('If Contact Already Exists?', 'fluent-community'),
80 'help' => __('Please specify what will happen if the subscriber already exists in the database', 'fluent-community'),
81 'options' => FunnelHelper::getUpdateOptions()
82 ],
83 'topic_ids' => [
84 'type' => 'grouped-select',
85 'label' => __('Target Course Sections', 'fluent-community'),
86 'help' => __('Select for which sections this automation will run', 'fluent-community'),
87 'options' => $this->getTopicsByCourseGroup(),
88 'is_multiple' => true,
89 'inline_help' => __('Keep it blank to run on any section', 'fluent-community')
90 ],
91 'run_multiple' => [
92 'type' => 'yes_no_check',
93 'label' => '',
94 'check_label' => __('Restart the Automation Multiple times for a contact for this event. (Only enable if you want to restart automation for the same contact)', 'fluent-community'),
95 'inline_help' => __('If you enable, it will restart the automation for a contact if the contact is already in the automation. Otherwise, it will skip if it already exists.', 'fluent-community')
96 ]
97 ];
98 }
99
100 public function handle($funnel, $originalArgs)
101 {
102 $topic = $originalArgs[0];
103 $userId = $originalArgs[1];
104
105 $subscriberData = FunnelHelper::prepareUserData($userId);
106
107 $subscriberData['source'] = 'fluent-community';
108
109 if (empty($subscriberData['email'])) {
110 return;
111 }
112
113 $willProcess = $this->isProcessable($funnel, $topic->id, $subscriberData);
114
115 $willProcess = apply_filters('fluentcrm_funnel_will_process_' . $this->triggerName, $willProcess, $funnel, $subscriberData, $originalArgs);
116 if (!$willProcess) {
117 return;
118 }
119
120 $subscriberData = wp_parse_args($subscriberData, $funnel->settings);
121
122 $subscriberData['status'] = $subscriberData['subscription_status'];
123 unset($subscriberData['subscription_status']);
124
125 (new FunnelProcessor())->startFunnelSequence($funnel, $subscriberData, [
126 'source_trigger_name' => $this->triggerName,
127 'source_ref_id' => $topic->id
128 ]);
129
130 }
131
132 private function isProcessable($funnel, $topicId, $subscriberData)
133 {
134 $conditions = $funnel->conditions;
135 // check update_type
136 $updateType = Arr::get($conditions, 'update_type');
137
138 $subscriber = FunnelHelper::getSubscriber($subscriberData['email']);
139 if ($subscriber && $updateType == 'skip_all_if_exist') {
140 return false;
141 }
142
143 // check the products ids
144 if ($conditions['topic_ids']) {
145 return in_array($topicId, $conditions['topic_ids']);
146 }
147
148 // check run_only_one
149 if ($subscriber && FunnelHelper::ifAlreadyInFunnel($funnel->id, $subscriber->id)) {
150 $multipleRun = Arr::get($conditions, 'run_multiple') == 'yes';
151 if ($multipleRun) {
152 FunnelHelper::removeSubscribersFromFunnel($funnel->id, [$subscriber->id]);
153 } else {
154 return false;
155 }
156 }
157
158 return true;
159 }
160
161 public function getTopicsByCourseGroup()
162 {
163 $courses = Course::query()->orderBy('title', 'ASC')->get();
164
165 $groups = [];
166 foreach ($courses as $course) {
167 $group = [
168 'title' => $course->title,
169 'slug' => $course->slug,
170 'options' => []
171 ];
172
173 $sections = CourseTopic::where('space_id', $course->id)
174 ->orderBy('priority', 'ASC')
175 ->with(['lessons'])
176 ->get();
177
178
179 foreach ($sections as $section) {
180 $group['options'][] = [
181 'id' => (string)$section->id,
182 'title' => $section->title
183 ];
184 }
185 $groups[] = $group;
186 }
187 return $groups;
188 }
189 }
190