PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.98
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.98
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 / CourseTopic.php

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

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