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