| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Course\Model; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\Activity; |
| 7 |
use FluentCommunity\App\Models\BaseSpace; |
| 8 |
use FluentCommunity\App\Models\Meta; |
| 9 |
use FluentCommunity\App\Models\Term; |
| 10 |
use FluentCommunity\App\Models\User; |
| 11 |
use FluentCommunity\Framework\Support\Arr; |
| 12 |
use FluentCommunity\App\Models\SpaceGroup; |
| 13 |
|
| 14 |
/** |
| 15 |
* Course Model - DB Model for Individual Courses |
| 16 |
* |
| 17 |
* Database Model |
| 18 |
* |
| 19 |
* @package FluentCrm\App\Models |
| 20 |
* |
| 21 |
* @version 1.1.0 |
| 22 |
*/ |
| 23 |
class Course extends BaseSpace |
| 24 |
{ |
| 25 |
protected static $type = 'course'; |
| 26 |
|
| 27 |
public static function boot() |
| 28 |
{ |
| 29 |
parent::boot(); |
| 30 |
} |
| 31 |
|
| 32 |
public function scopeByAdminAccess($query, $userId = null) |
| 33 |
{ |
| 34 |
if (!$userId) { |
| 35 |
$userId = get_current_user_id(); |
| 36 |
} |
| 37 |
|
| 38 |
$userModel = User::find($userId); |
| 39 |
|
| 40 |
if ($userModel->isCommunityAdmin()) { |
| 41 |
return $query; |
| 42 |
} |
| 43 |
|
| 44 |
return $this->where('user_id', $userId); |
| 45 |
} |
| 46 |
|
| 47 |
public function scopeByPostTopic($query, $topicSlug = null) |
| 48 |
{ |
| 49 |
if (!$topicSlug) { |
| 50 |
return $query; |
| 51 |
} |
| 52 |
|
| 53 |
$postTpic = Term::where('taxonomy_name', 'post_topic')->where('slug', $topicSlug)->first(); |
| 54 |
|
| 55 |
if (!$postTpic) { |
| 56 |
return $query; |
| 57 |
} |
| 58 |
|
| 59 |
return $query->whereHas('categories', function ($q) use ($postTpic) { |
| 60 |
$q->where('object_id', $postTpic->id); |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
public function students() |
| 65 |
{ |
| 66 |
return $this->belongsToMany(User::class, 'fcom_space_user', 'space_id', 'user_id') |
| 67 |
->wherePivot('role', 'student') |
| 68 |
->withPivot(['role', 'created_at', 'status']); |
| 69 |
} |
| 70 |
|
| 71 |
public function getCompletedStrundesCount() |
| 72 |
{ |
| 73 |
return Activity::where('feed_id', $this->id) |
| 74 |
->where('action_name', 'course_completed') |
| 75 |
->count(); |
| 76 |
} |
| 77 |
|
| 78 |
public function getCourseType() |
| 79 |
{ |
| 80 |
return Arr::get($this->settings, 'course_type', 'self_paced'); // possible values: slef_paced | scheduled | structured |
| 81 |
} |
| 82 |
|
| 83 |
public function group() |
| 84 |
{ |
| 85 |
return $this->belongsTo(SpaceGroup::class, 'parent_id', 'id'); |
| 86 |
} |
| 87 |
|
| 88 |
public function categories() |
| 89 |
{ |
| 90 |
return $this->belongsToMany(Term::class, 'fcom_meta', 'meta_key', 'object_id') |
| 91 |
->wherePivot('object_type', 'term_space_relation') |
| 92 |
->where('taxonomy_name', 'post_topic'); |
| 93 |
} |
| 94 |
|
| 95 |
public function syncCategories($categoryIds = []) |
| 96 |
{ |
| 97 |
$this->syncTopics($categoryIds); |
| 98 |
return $this; |
| 99 |
} |
| 100 |
} |
| 101 |
|