PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.96
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.96
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 1.0.96, at Modules/Course/Model/CourseLesson.php

219 lines 5.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\Course\Model;
4
5
6 use FluentCommunity\App\Models\Model;
7 use FluentCommunity\App\Models\Reaction;
8 use FluentCommunity\App\Models\Term;
9 use FluentCommunity\App\Models\User;
10 use FluentCommunity\App\Services\Helper;
11
12 /**
13 * Clourse Lesson Model - DB Model for Individual Clourse Lesson
14 *
15 * Database Model
16 *
17 * @package FluentCrm\App\Models
18 *
19 * @version 1.1.0
20 */
21 class CourseLesson extends Model
22 {
23 protected $table = 'fcom_posts';
24
25 protected $guarded = ['id'];
26
27 protected $casts = [
28 'comments_count' => 'int',
29 'reactions_count' => 'int'
30 ];
31
32 protected $fillable = [
33 'user_id',
34 'title',
35 'slug',
36 'message',
37 'message_rendered',
38 'type',
39 'space_id',
40 'privacy',
41 'status',
42 'featured_image',
43 'is_sticky',
44 'expired_at',
45 'comments_count',
46 'reactions_count',
47 'meta',
48 'parent_id'
49 ];
50
51 protected $searchable = [
52 'message',
53 'title'
54 ];
55
56 public static $publicColumns = [
57 'id', 'slug', 'title', 'message_rendered', 'featured_image', 'created_at', 'privacy', 'type', 'status', 'slug', 'space_id', 'user_id', 'meta', 'comments_count', 'reactions_count'
58 ];
59
60 protected static $type = 'course_lesson';
61
62 public static function boot()
63 {
64 parent::boot();
65
66 static::creating(function ($model) {
67 $model->user_id = get_current_user_id();
68 if (empty($model->slug)) {
69 $model->slug = self::generateNewSlug($model);
70 }
71
72 $model->type = self::$type;
73
74 if (empty($model->meta)) {
75 $model->meta = self::getDefaultMeta();
76 }
77 });
78
79 static::addGlobalScope('type', function ($builder) {
80 $builder->where('type', self::$type);
81 });
82 }
83
84 protected static function getDefaultMeta()
85 {
86 return [
87 'media' => [
88 'type' => 'oembed',
89 'url' => '',
90 'content_type' => 'video',
91 'html' => ''
92 ],
93 'enable_comments' => 'yes',
94 'enable_media' => 'yes'
95 ];
96 }
97
98 protected static function generateNewSlug($newModel)
99 {
100 $slug = sanitize_title($newModel->title);
101
102 // check if the slug is available for this type
103 $exist = self::where('slug', $slug)
104 ->exists();
105
106 if ($exist) {
107 $slug = $slug . '-' . time();
108 }
109
110 return $slug;
111 }
112
113 public function topic()
114 {
115 return $this->belongsTo(CourseTopic::class, 'parent_id');
116 }
117
118 public function course()
119 {
120 return $this->belongsTo(Course::class, 'space_id');
121 }
122
123 public function setMetaAttribute($value)
124 {
125 $this->attributes['meta'] = maybe_serialize($value);
126 }
127
128 public function getMetaAttribute($value)
129 {
130 $meta = maybe_unserialize($value);
131
132 if (!$meta) {
133 $meta = self::getDefaultMeta();
134 }
135
136 return $meta;
137 }
138
139 public function scopeSearchBy($query, $search)
140 {
141 if (!$search) {
142 return $query;
143 }
144
145 $fields = $this->searchable;
146 $query->where(function ($query) use ($fields, $search) {
147 $query->where(array_shift($fields), 'LIKE', "%$search%");
148 foreach ($fields as $field) {
149 $query->orWhere($field, 'LIKE', "$search%");
150 }
151 });
152
153 return $query;
154 }
155
156 public function owner()
157 {
158 return $this->belongsTo(User::class, 'user_id', 'ID');
159 }
160
161 public function reactions()
162 {
163 return $this->hasMany(Reaction::class, 'object_id', 'id')
164 ->where('object_type', 'feed');
165 }
166
167 public function terms()
168 {
169 return $this->belongsToMany(Term::class, 'fcom_term_feed', 'post_id', 'term_id');
170 }
171
172 public function getPermalink()
173 {
174 return Helper::baseUrl() . '/course/' . $this->course ? $this->course->slug : 'undefined' . '/lessons/' . $this->slug . '/view';
175 }
176
177 public function hasUserReact($userId, $type = 'like')
178 {
179 if (!$userId) {
180 return false;
181 }
182
183 return (bool)Reaction::where('object_id', $this->id)
184 ->select(['id'])
185 ->where('object_type', 'feed')
186 ->where('user_id', $userId)
187 ->where('type', $type)
188 ->first();
189 }
190
191 public function getHumanExcerpt($length = 40)
192 {
193 $content = $this->title;
194
195 if (!$content) {
196 $content = $this->message;
197 if (!$content) {
198 return '';
199 }
200 // remove all tags
201 $content = strip_tags($content);
202 // remove new lines and tabs
203 $content = str_replace(["\r", "\n", "\t"], ' ', $content);
204 // remove multiple spaces
205 $content = preg_replace('/\s+/', ' ', $content);
206
207 // trim
208 $content = trim($content);
209 }
210
211 if (!$content) {
212 return '';
213 }
214
215 // return the first $length chars of the content with ... at the end
216 return mb_substr($content, 0, $length) . '...';
217 }
218 }
219