| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Modules\Course\Model; |
| 4 |
|
| 5 |
use FluentCommunity\App\Models\Activity; |
| 6 |
use FluentCommunity\App\Models\BaseSpace; |
| 7 |
use FluentCommunity\App\Models\SpaceUserPivot; |
| 8 |
use FluentCommunity\App\Models\Term; |
| 9 |
use FluentCommunity\App\Models\User; |
| 10 |
use FluentCommunity\App\Services\Helper; |
| 11 |
use FluentCommunity\Framework\Support\Arr; |
| 12 |
use FluentCommunity\App\Models\SpaceGroup; |
| 13 |
use FluentCommunity\App\Models\XProfile; |
| 14 |
|
| 15 |
/** |
| 16 |
* Course Model - DB Model for Individual Courses |
| 17 |
* |
| 18 |
* Database Model |
| 19 |
* |
| 20 |
* @package FluentCrm\App\Models |
| 21 |
* |
| 22 |
* @version 1.1.0 |
| 23 |
* |
| 24 |
* @property bool|null $is_course_admin |
| 25 |
* @property bool|null $can_self_enroll |
| 26 |
* @property int|null $students_count |
| 27 |
* @property string|null $course_type |
| 28 |
* @property mixed $lockscreen |
| 29 |
* @property mixed $category_ids |
| 30 |
* @property int|null $completed_students |
| 31 |
* @property float|null $overAllProgress |
| 32 |
* @property-read mixed $categories |
| 33 |
* @property-read mixed $creator |
| 34 |
*/ |
| 35 |
class Course extends BaseSpace |
| 36 |
{ |
| 37 |
protected static $type = 'course'; |
| 38 |
|
| 39 |
public static function boot() |
| 40 |
{ |
| 41 |
parent::boot(); |
| 42 |
} |
| 43 |
|
| 44 |
public function scopeByAdminAccess($query, $userId = null) |
| 45 |
{ |
| 46 |
if (!$userId) { |
| 47 |
$userId = get_current_user_id(); |
| 48 |
} |
| 49 |
|
| 50 |
$userModel = User::find($userId); |
| 51 |
|
| 52 |
if ($userModel->hasSpaceManageAccess()) { |
| 53 |
return $query; |
| 54 |
} |
| 55 |
|
| 56 |
return $query->where('created_by', $userId); |
| 57 |
} |
| 58 |
|
| 59 |
public function scopeSearchBy($query, $search) |
| 60 |
{ |
| 61 |
if ($search) { |
| 62 |
$fields = $this->searchable; |
| 63 |
$query->where(function ($query) use ($fields, $search) { |
| 64 |
$query->where(function ($q) use ($fields, $search) { |
| 65 |
$q->where(array_shift($fields), 'LIKE', "%$search%"); |
| 66 |
foreach ($fields as $field) { |
| 67 |
$q->orWhere($field, 'LIKE', "$search%"); |
| 68 |
} |
| 69 |
}) |
| 70 |
->orWhereHas('categories', function ($q) use ($search) { |
| 71 |
$q->where('title', 'LIKE', "%$search%") |
| 72 |
->orWhere('description', 'LIKE', "%$search%"); |
| 73 |
}) |
| 74 |
->orWhereHas('posts', function ($q) use ($search) { |
| 75 |
$q->where('status', 'published') |
| 76 |
->where(function ($q) use ($search) { |
| 77 |
$q->where('title', 'LIKE', "%$search%") |
| 78 |
->orWhere('message', 'LIKE', "%$search%"); |
| 79 |
}); |
| 80 |
}); |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
return $query; |
| 85 |
} |
| 86 |
|
| 87 |
public function scopeByPostTopic($query, $topicSlug = null) |
| 88 |
{ |
| 89 |
if (!$topicSlug) { |
| 90 |
return $query; |
| 91 |
} |
| 92 |
|
| 93 |
$postTpic = Term::where('taxonomy_name', 'post_topic')->where('slug', $topicSlug)->first(); |
| 94 |
|
| 95 |
if (!$postTpic) { |
| 96 |
return $query; |
| 97 |
} |
| 98 |
|
| 99 |
return $query->whereHas('categories', function ($q) use ($postTpic) { |
| 100 |
$q->where('object_id', $postTpic->id); |
| 101 |
}); |
| 102 |
} |
| 103 |
|
| 104 |
public function creator() |
| 105 |
{ |
| 106 |
return $this->hasOneThrough( |
| 107 |
XProfile::class, |
| 108 |
User::class, |
| 109 |
'ID', // Foreign key on the users table |
| 110 |
'user_id', // Foreign key on the xprofiles table |
| 111 |
'created_by', // Local key on the courses table |
| 112 |
'ID' // Local key on the users table |
| 113 |
)->withoutGlobalScopes(); |
| 114 |
} |
| 115 |
|
| 116 |
public function students() |
| 117 |
{ |
| 118 |
return $this->belongsToMany(User::class, 'fcom_space_user', 'space_id', 'user_id') |
| 119 |
->wherePivot('role', 'student') |
| 120 |
->withPivot([ 'role', 'created_at', 'status' ]); |
| 121 |
} |
| 122 |
|
| 123 |
public function course_topics() |
| 124 |
{ |
| 125 |
return $this->hasMany(CourseTopic::class, 'space_id'); |
| 126 |
} |
| 127 |
|
| 128 |
public function enrollment() |
| 129 |
{ |
| 130 |
return $this->belongsTo(SpaceUserPivot::class, 'id', 'space_id'); |
| 131 |
} |
| 132 |
|
| 133 |
public function student_enrollments() |
| 134 |
{ |
| 135 |
return $this->hasMany(SpaceUserPivot::class, 'space_id', 'id') |
| 136 |
->where('role', 'student') |
| 137 |
->where('status', 'active'); |
| 138 |
} |
| 139 |
|
| 140 |
public function getCompletedStrundesCount() |
| 141 |
{ |
| 142 |
return Activity::where('feed_id', $this->id) |
| 143 |
->where('action_name', 'course_completed') |
| 144 |
->count(); |
| 145 |
} |
| 146 |
|
| 147 |
public function getCourseType() |
| 148 |
{ |
| 149 |
$courseType = Arr::get($this->settings, 'course_type', 'self_paced'); // possible values: self_paced | scheduled | structured |
| 150 |
|
| 151 |
$validTypes = [ 'self_paced', 'scheduled', 'structured' ]; |
| 152 |
|
| 153 |
if (!in_array($courseType, $validTypes)) { |
| 154 |
return 'self_paced'; |
| 155 |
} |
| 156 |
|
| 157 |
return $courseType; |
| 158 |
} |
| 159 |
|
| 160 |
public function group() |
| 161 |
{ |
| 162 |
return $this->belongsTo(SpaceGroup::class, 'parent_id', 'id'); |
| 163 |
} |
| 164 |
|
| 165 |
public function categories() |
| 166 |
{ |
| 167 |
return $this->belongsToMany(Term::class, 'fcom_meta', 'meta_key', 'object_id') |
| 168 |
->wherePivot('object_type', 'term_space_relation') |
| 169 |
->where('taxonomy_name', 'post_topic'); |
| 170 |
} |
| 171 |
|
| 172 |
public function syncCategories($categoryIds = []) |
| 173 |
{ |
| 174 |
$this->syncTopics($categoryIds); |
| 175 |
return $this; |
| 176 |
} |
| 177 |
|
| 178 |
public function isCourseAdmin($user = null) |
| 179 |
{ |
| 180 |
if (!$user) { |
| 181 |
$user = Helper::getCurrentUser(); |
| 182 |
} |
| 183 |
|
| 184 |
if (!$user) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
$permissions = $user->getPermissions(); |
| 189 |
|
| 190 |
if (!empty($permissions['course_admin'])) { |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
// Check if course creator |
| 195 |
if (!empty($permissions['course_creator'])) { |
| 196 |
return $this->created_by == $user->ID; |
| 197 |
} |
| 198 |
|
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
} |
| 203 |
|