PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.6.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.6.0
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 / Course / Model / CourseLesson.php

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

312 lines 7.8 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
6 use FluentCommunity\App\Functions\Utility;
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\Media;
12 use FluentCommunity\App\Models\Comment;
13 use FluentCommunity\App\Services\Helper;
14 use FluentCommunity\Framework\Support\Arr;
15
16 /**
17 * Course Lesson Model - DB Model for Individual Course Lesson
18 *
19 * Database Model
20 *
21 * @package FluentCrm\App\Models
22 *
23 * @version 1.1.0
24 */
25 class CourseLesson extends Model
26 {
27 protected $table = 'fcom_posts';
28
29 protected $guarded = ['id'];
30
31 protected $casts = [
32 'comments_count' => 'int',
33 'reactions_count' => 'int'
34 ];
35
36 protected $fillable = [
37 'user_id',
38 'title',
39 'slug',
40 'message',
41 'message_rendered',
42 'type',
43 'space_id',
44 'privacy',
45 'status',
46 'featured_image',
47 'is_sticky',
48 'scheduled_at',
49 'expired_at',
50 'content_type',
51 'comments_count',
52 'reactions_count',
53 'meta',
54 'priority',
55 'parent_id'
56 ];
57
58 protected $searchable = [
59 'message',
60 'title'
61 ];
62
63 public static $publicColumns = [
64 'id', 'slug', 'title', 'message_rendered', 'featured_image', 'created_at', 'privacy', 'type', 'status', 'slug', 'space_id', 'user_id', 'meta', 'content_type', 'comments_count', 'reactions_count'
65 ];
66
67 protected static $type = 'course_lesson';
68
69 public static function boot()
70 {
71 parent::boot();
72
73 static::creating(function ($model) {
74 $model->user_id = get_current_user_id();
75 if (empty($model->slug)) {
76 $model->slug = self::generateNewSlug($model);
77 }
78
79 $model->type = self::$type;
80
81 if (empty($model->content_type)) {
82 $model->content_type = 'text';
83 }
84
85 if (empty($model->message)) {
86 $model->message = '';
87 }
88
89 if (empty($model->meta)) {
90 $model->meta = self::getDefaultMeta();
91 }
92 });
93
94 static::addGlobalScope('type', function ($builder) {
95 $builder->where('type', self::$type);
96 });
97 }
98
99 protected static function getDefaultMeta()
100 {
101 return [
102 'media' => [
103 'type' => 'oembed',
104 'url' => '',
105 'content_type' => 'video',
106 'html' => ''
107 ],
108 'enable_comments' => 'yes',
109 'enable_media' => 'yes',
110 'document_lists' => []
111 ];
112 }
113
114 protected static function generateNewSlug($newModel)
115 {
116 $slug = Utility::slugify($newModel->title, 'lesson-' . time());
117
118 // check if the slug is available for this type
119 $exist = self::where('slug', $slug)
120 ->exists();
121
122 if ($exist) {
123 $slug = $slug . '-' . time();
124 }
125
126 return $slug;
127 }
128
129 public function topic()
130 {
131 return $this->belongsTo(CourseTopic::class, 'parent_id');
132 }
133
134 public function course()
135 {
136 return $this->belongsTo(Course::class, 'space_id', 'id');
137 }
138
139 public function setMetaAttribute($value)
140 {
141 $this->attributes['meta'] = maybe_serialize($value);
142 }
143
144 public function getMetaAttribute($value)
145 {
146 $meta = Utility::safeUnserialize($value);
147
148 if (!$meta) {
149 $meta = self::getDefaultMeta();
150 }
151
152 return $meta;
153 }
154
155 public function getQuestionsAttribute()
156 {
157 return Arr::get($this->meta, 'quiz_questions', []);
158 }
159
160 public function getEnabledQuestionsAttribute()
161 {
162 return array_filter($this->questions, function($question) {
163 return Arr::isTrue($question, 'enabled');
164 });
165 }
166
167 public function getIsEnforcePassAttribute()
168 {
169 return Arr::isTrue($this->meta, 'enforce_passing_score');
170 }
171
172 public function getIsFreePreviewAttribute()
173 {
174 return Arr::isTrue($this->meta, 'free_preview_lesson');
175 }
176
177 public function getPassingScoreAttribute()
178 {
179 if (Arr::isTrue($this->meta, 'enable_passing_score')) {
180 return Arr::get($this->meta, 'passing_score', 0);
181 }
182 return 0;
183 }
184
185 public function scopeSearchBy($query, $search)
186 {
187 if (!$search) {
188 return $query;
189 }
190
191 $fields = $this->searchable;
192 $query->where(function ($query) use ($fields, $search) {
193 $query->where(array_shift($fields), 'LIKE', "%$search%");
194 foreach ($fields as $field) {
195 $query->orWhere($field, 'LIKE', "$search%");
196 }
197 });
198
199 return $query;
200 }
201
202 public function owner()
203 {
204 return $this->belongsTo(User::class, 'user_id', 'ID');
205 }
206
207 public function comments()
208 {
209 return $this->hasMany(Comment::class, 'post_id', 'id');
210 }
211
212 public function reactions()
213 {
214 return $this->hasMany(Reaction::class, 'parent_id', 'id')
215 ->where('object_type', 'comment');
216 }
217
218 public function lessonCompleted()
219 {
220 return $this->hasMany(Reaction::class, 'object_id', 'id')
221 ->where('object_type', 'lesson_completed');
222 }
223
224 public function media()
225 {
226 return $this->hasMany(Media::class, 'feed_id', 'id');
227 }
228
229 public function terms()
230 {
231 return $this->belongsToMany(Term::class, 'fcom_term_feed', 'post_id', 'term_id');
232 }
233
234 public function isQuizType()
235 {
236 return $this->content_type == 'quiz';
237 }
238
239 public function getPermalink()
240 {
241 $uri = '/course/' . ($this->course ? $this->course->slug : 'undefined') . '/lessons/' . $this->slug . '/view';
242 return Helper::baseUrl($uri);
243 }
244
245 public function hasUserReact($userId, $type = 'like')
246 {
247 if (!$userId) {
248 return false;
249 }
250
251 return (bool)Reaction::where('object_id', $this->id)
252 ->select(['id'])
253 ->where('object_type', 'feed')
254 ->where('user_id', $userId)
255 ->where('type', $type)
256 ->first();
257 }
258
259 public function getHumanExcerpt($length = 40)
260 {
261 $content = $this->title;
262
263 if (!$content) {
264 $content = $this->message;
265 if (!$content) {
266 return '';
267 }
268 // remove all tags
269 $content = wp_strip_all_tags($content);
270 // remove new lines and tabs
271 $content = str_replace(["\r", "\n", "\t"], ' ', $content);
272 // remove multiple spaces
273 $content = preg_replace('/\s+/', ' ', $content);
274
275 // trim
276 $content = trim($content);
277 }
278
279 if (!$content) {
280 return '';
281 }
282
283 // return the first $length chars of the content with ... at the end
284 return mb_substr($content, 0, $length) . '...';
285 }
286
287 public function getPublicLessonMeta($canView = true)
288 {
289 $meta = $this->meta;
290
291 if(!empty($meta['document_lists'])) {
292 $docLists = $meta['document_lists'];
293 foreach ($docLists as $index => $docList) {
294 if(!empty($docList['media_key']) && !empty($docList['id'])) {
295 $docLists[$index]['url'] = Helper::baseUrl('?fcom_action=download_document&media_key='.$docList['media_key'].'&media_id='.$docList['id']);
296 }
297 }
298 $meta['document_lists'] = $docLists;
299 }
300
301 if(!$canView) {
302 $meta['document_lists'] = [];
303 $meta['document_ids'] = [];
304 unset($meta['media']);
305 }
306
307 $meta = apply_filters('fluent_community/lesson/get_public_meta', $meta, $this);
308
309 return $meta;
310 }
311 }
312