| @@ -1,16 +1,17 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\Modules\Course\Model; |
| 4 | 4 | |
| 5 | -use FluentCommunity\App\Functions\Utility; | |
| 6 | 5 | use FluentCommunity\App\Models\Activity; |
| 7 | 6 | use FluentCommunity\App\Models\BaseSpace; |
| 8 | -use FluentCommunity\App\Models\Meta; | |
| 7 | +use FluentCommunity\App\Models\SpaceUserPivot; | |
| 9 | 8 | use FluentCommunity\App\Models\Term; |
| 10 | 9 | use FluentCommunity\App\Models\User; |
| 10 | +use FluentCommunity\App\Services\Helper; | |
| 11 | 11 | use FluentCommunity\Framework\Support\Arr; |
| 12 | 12 | use FluentCommunity\App\Models\SpaceGroup; |
| 13 | +use FluentCommunity\App\Models\XProfile; | |
| 13 | 14 | |
| 14 | 15 | /** |
| 15 | 16 | * Course Model - DB Model for Individual Courses |
| 16 | 17 | * |
| @@ -18,8 +19,19 @@ | ||
| 18 | 19 | * |
| 19 | 20 | * @package FluentCrm\App\Models |
| 20 | 21 | * |
| 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 | |
| 22 | 34 | */ |
| 23 | 35 | class Course extends BaseSpace |
| 24 | 36 | { |
| 25 | 37 | protected static $type = 'course'; |
| @@ -36,15 +48,43 @@ | ||
| 36 | 48 | } |
| 37 | 49 | |
| 38 | 50 | $userModel = User::find($userId); |
| 39 | 51 | |
| 40 | - if ($userModel->isCommunityAdmin()) { | |
| 52 | + if ($userModel->hasSpaceManageAccess()) { | |
| 41 | 53 | return $query; |
| 42 | 54 | } |
| 43 | 55 | |
| 44 | - return $this->where('user_id', $userId); | |
| 56 | + return $query->where('created_by', $userId); | |
| 45 | 57 | } |
| 46 | 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 | + | |
| 47 | 87 | public function scopeByPostTopic($query, $topicSlug = null) |
| 48 | 88 | { |
| 49 | 89 | if (!$topicSlug) { |
| 50 | 90 | return $query; |
| @@ -60,15 +100,44 @@ | ||
| 60 | 100 | $q->where('object_id', $postTpic->id); |
| 61 | 101 | }); |
| 62 | 102 | } |
| 63 | 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 | + | |
| 64 | 116 | public function students() |
| 65 | 117 | { |
| 66 | 118 | return $this->belongsToMany(User::class, 'fcom_space_user', 'space_id', 'user_id') |
| 67 | 119 | ->wherePivot('role', 'student') |
| 68 | - ->withPivot(['role', 'created_at', 'status']); | |
| 120 | + ->withPivot([ 'role', 'created_at', 'status' ]); | |
| 69 | 121 | } |
| 70 | 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 | + | |
| 71 | 140 | public function getCompletedStrundesCount() |
| 72 | 141 | { |
| 73 | 142 | return Activity::where('feed_id', $this->id) |
| 74 | 143 | ->where('action_name', 'course_completed') |
| @@ -76,9 +145,17 @@ | ||
| 76 | 145 | } |
| 77 | 146 | |
| 78 | 147 | public function getCourseType() |
| 79 | 148 | { |
| 80 | - return Arr::get($this->settings, 'course_type', 'self_paced'); // possible values: slef_paced | scheduled | structured | |
| 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; | |
| 81 | 158 | } |
| 82 | 159 | |
| 83 | 160 | public function group() |
| 84 | 161 | { |
| @@ -96,5 +173,30 @@ | ||
| 96 | 173 | { |
| 97 | 174 | $this->syncTopics($categoryIds); |
| 98 | 175 | return $this; |
| 99 | 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 | + | |
| 100 | 202 | } |