PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / trunk
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses vtrunk
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 1.1.10 1.1.11 All 75 releases
fluent-community / Modules / Course / Model / CourseTopic.php

CourseTopic.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses trunk, at Modules/Course/Model/CourseTopic.php

265 lines 6.6 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\Course\Model;
4
5 use FluentCommunity\App\Functions\Utility;
6 use FluentCommunity\App\Models\Comment;
7 use FluentCommunity\App\Models\Model;
8 use FluentCommunity\App\Models\Reaction;
9 use FluentCommunity\App\Models\Term;
10 use FluentCommunity\App\Models\User;
11 use FluentCommunity\App\Models\Meta;
12
13 /**
14 * @property int $id
15 * @property int|null $user_id
16 * @property string|null $title
17 * @property string|null $slug
18 * @property string|null $message
19 * @property string|null $message_rendered
20 * @property string|null $type
21 * @property int|null $space_id
22 * @property string|null $privacy
23 * @property string|null $status
24 * @property string|null $featured_image
25 * @property int|null $is_sticky
26 * @property string|null $expired_at
27 * @property int $comments_count
28 * @property int $reactions_count
29 * @property array $meta
30 * @property int|null $priority
31 * @property string|null $scheduled_at
32 * @property string|null $created_at
33 * @property string|null $updated_at
34 * @property-read Course|null $course
35 * @property-read mixed $lessons
36 */
37 class CourseTopic extends Model
38 {
39 protected $table = 'fcom_posts';
40
41 protected $guarded = [ 'id' ];
42
43 protected $casts = [
44 'comments_count' => 'int',
45 'reactions_count' => 'int',
46 ];
47
48 protected $fillable = [
49 'user_id',
50 'title',
51 'slug',
52 'message',
53 'message_rendered',
54 'type',
55 'space_id',
56 'privacy',
57 'status',
58 'featured_image',
59 'is_sticky',
60 'expired_at',
61 'comments_count',
62 'reactions_count',
63 'meta',
64 'priority',
65 'scheduled_at',
66 ];
67
68 protected $searchable = [
69 'message',
70 'title',
71 ];
72
73 public static $publicColumns = [
74 'id', 'slug', 'title', 'message_rendered', 'featured_image', 'created_at', 'privacy', 'type', 'status', 'slug', 'space_id', 'user_id', 'meta', 'comments_count', 'reactions_count',
75 ];
76
77 protected static $type = 'course_section';
78
79 public static function boot()
80 {
81 parent::boot();
82
83 static::creating(function ($model) {
84 $model->user_id = get_current_user_id();
85 if (empty($model->slug)) {
86 $model->slug = self::generateNewSlug($model);
87 }
88
89 $model->type = self::$type;
90
91 if (empty($model->meta)) {
92 $model->meta = self::getDefaultMeta();
93 }
94 });
95
96 static::addGlobalScope('type', function ($builder) {
97 $builder->where('type', self::$type);
98 });
99 }
100
101 public function lessons()
102 {
103 return $this->hasMany(CourseLesson::class, 'parent_id')->orderBy('priority', 'ASC')->orderBy('id', 'ASC');
104 }
105
106 protected static function generateNewSlug($newModel)
107 {
108 $slug = Utility::slugify($newModel->title, 'topic');
109
110 $exist = self::where('slug', $slug)
111 ->exists();
112
113 if ($exist) {
114 $slug = $slug . '-' . time();
115 }
116
117 return $slug;
118 }
119
120 protected static function getDefaultMeta()
121 {
122 return [
123 'preview_data' => null,
124 ];
125 }
126
127 public function setMetaAttribute($value)
128 {
129 $this->attributes['meta'] = maybe_serialize($value);
130 }
131
132 public function getMetaAttribute($value)
133 {
134 $meta = Utility::safeUnserialize($value);
135
136 if (!$meta) {
137 $meta = [];
138 }
139
140 return $meta;
141 }
142
143 public function scopeSearchBy($query, $search)
144 {
145 if (!$search) {
146 return $query;
147 }
148
149 $fields = $this->searchable;
150 $query->where(function ($query) use ($fields, $search) {
151 $query->where(array_shift($fields), 'LIKE', "%$search%");
152 foreach ($fields as $field) {
153 $query->orWhere($field, 'LIKE', "$search%");
154 }
155 });
156
157 return $query;
158 }
159
160 public function owner()
161 {
162 return $this->belongsTo(User::class, 'user_id', 'ID');
163 }
164
165 public function course()
166 {
167 return $this->belongsTo(Course::class, 'space_id', 'id');
168 }
169
170 public function comments()
171 {
172 return $this->hasMany(Comment::class, 'post_id', 'id');
173 }
174
175 public function reactions()
176 {
177 return $this->hasMany(Reaction::class, 'object_id', 'id')
178 ->where('object_type', 'feed');
179 }
180
181 public function terms()
182 {
183 return $this->belongsToMany(Term::class, 'fcom_term_feed', 'post_id', 'term_id');
184 }
185
186 public function hasUserReact($userId, $type = 'like')
187 {
188 if (!$userId) {
189 return false;
190 }
191
192 return (bool)Reaction::where('object_id', $this->id)
193 ->select([ 'id' ])
194 ->where('object_type', 'feed')
195 ->where('user_id', $userId)
196 ->where('type', $type)
197 ->first();
198 }
199
200 public function updateCustomMeta($key, $value)
201 {
202 $exist = Meta::where('object_id', $this->id)
203 ->where('object_type', 'course_topic')
204 ->where('meta_key', $key)
205 ->first();
206
207 if ($exist) {
208 $exist->value = $value;
209 $exist->save();
210 } else {
211 Meta::create([
212 'object_id' => $this->id,
213 'object_type' => 'course_topic',
214 'meta_key' => $key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
215 'value' => $value,
216 ]);
217 }
218
219 return true;
220 }
221
222 public function getCustomMeta($key, $default = null)
223 {
224 $exist = Meta::where('object_id', $this->id)
225 ->where('object_type', 'course_topic')
226 ->where('meta_key', $key)
227 ->first();
228
229 if ($exist) {
230 return $exist->value;
231 }
232
233 return $default;
234 }
235
236 public function getHumanExcerpt($length = 40)
237 {
238 $content = $this->title;
239
240 if (!$content) {
241 $content = $this->message;
242 if (!$content) {
243 return '';
244 }
245 // remove all tags
246 $content = wp_strip_all_tags($content);
247 // remove new lines and tabs
248 $content = str_replace([ "\r", "\n", "\t" ], ' ', $content);
249 // remove multiple spaces
250 $content = preg_replace('/\s+/', ' ', $content);
251
252 // trim
253 $content = trim($content);
254 }
255
256 if (!$content) {
257 return '';
258 }
259
260 // return the first $length chars of the content with ... at the end
261 return mb_substr($content, 0, $length) . '...';
262 }
263
264 }
265