PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.6.01
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.6.01
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 / Course.php

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

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