-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathOrganization.php
256 lines (230 loc) · 6.7 KB
/
Organization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Traits\GlobalScope;
use App\Models\DeepRelations\HasManyDeep;
use App\Models\DeepRelations\HasRelationships;
use Illuminate\Support\Arr;
use DB;
class Organization extends Model
{
use SoftDeletes, GlobalScope, HasRelationships;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'description',
'domain',
'image',
'favicon',
'parent_id',
'self_registration',
'account_id',
'api_key',
'unit_path',
'noovo_client_id',
'gcr_project_visibility',
'gcr_playlist_visibility',
'gcr_activity_visibility',
'tos_type',
'tos_url',
'tos_content',
'privacy_policy_type',
'privacy_policy_url',
'privacy_policy_content',
'primary_color',
'secondary_color',
'tertiary_color',
'primary_font_family',
'secondary_font_family',
'msteam_client_id',
'msteam_secret_id',
'msteam_tenant_id',
'msteam_secret_id_expiry',
'msteam_project_visibility',
'msteam_playlist_visibility',
'msteam_activity_visibility',
'auto_approve',
'activity_title_text',
'activity_title_placeholder',
'h5p_embed_option',
'h5p_reuse_option'
];
/**
* The users that belong to the organization.
*/
public function users()
{
return $this->belongsToMany('App\User', 'organization_user_roles')->using('App\Models\OrganizationUserRole')->withPivot('organization_role_type_id')->withTimestamps();
}
/**
* The admin users that belong to the organization.
*/
public function admins()
{
return $this->users()->wherePivot('organization_role_type_id', 1);
}
/**
* Get the projects for the organization.
*/
public function projects()
{
return $this->hasMany('App\Models\Project');
}
/**
* Get the teams for the organization.
*/
public function teams()
{
return $this->hasMany('App\Models\Team');
}
/**
* Get the groups for the organization.
*/
public function groups()
{
return $this->hasMany('App\Models\Group');
}
/**
* Get playlists directly from organizations model via hasManyThrough
* @return HasManyThrough
*/
public function playlists()
{
return $this->hasManyThrough('App\Models\Playlist', 'App\Models\Project', 'organization_id', 'project_id', 'id', 'id');
}
/**
* Get far away relations data using custom Deep classes
* @return HasManyDeep
*/
public function activities()
{
return $this->hasManyDeep(
'App\Models\Activity',
// Intermediate models, beginning at the far parent (Organizations).
['App\Models\Project', 'App\Models\Playlist'],
[
// Foreign key on the "project" table.
'organization_id',
// Foreign key on the "playlist" table.
'project_id',
// Foreign key on the "activity" table.
'playlist_id'
],
[
// Local key on the "organizations" table.
'id',
// Local key on the "project" table.
'id',
// Local key on the "playlist" table.
'id'
]
);
}
/**
* Get the children for the organization.
*/
public function children()
{
return $this->hasMany('App\Models\organization', 'parent_id')->with('children');
}
/**
* Get the parent that owns the organization.
*/
public function parent()
{
return $this->belongsTo('App\Models\Organization');
}
/**
* Get the roles for the organization.
*/
public function roles()
{
return $this->hasMany('App\Models\OrganizationRoleType');
}
/**
* The user roles that belong to the organization.
*/
public function userRoles()
{
return $this->belongsToMany('App\Models\OrganizationRoleType', 'organization_user_roles');
}
/**
* Get the media sources for the organization.
*/
public function mediaSources()
{
return $this->belongsToMany('App\Models\MediaSource', 'organization_media_sources')
->withPivot('h5p_library', 'lti_tool_settings_status', 'media_sources_show_status')
->withTimestamps();
}
/**
* Get organization IDs for the full tree (ancestors and children) of this org
*/
public function getOrgTreeAttribute()
{
$results = DB::select( DB::raw("
WITH RECURSIVE child_orgs AS (
SELECT
o1.id, o1.parent_id, o1.name
FROM
organizations o1
WHERE
o1.deleted_at IS NULL AND
o1.id = :thisOrgId
UNION ALL
SELECT
o2.id, o2.parent_id, o2.name
FROM
organizations o2
JOIN
child_orgs co
ON
co.id = o2.parent_id
WHERE
o2.deleted_at IS NULL
), parent_orgs AS (
SELECT
o3.id, o3.parent_id, o3.name
FROM
organizations o3
WHERE
o3.deleted_at IS NULL AND
o3.id = :thisOrgId
UNION ALL
SELECT
o4.id, o4.parent_id, o4.name
FROM
organizations o4
JOIN
parent_orgs po
ON
po.parent_id = o4.id
WHERE
o4.deleted_at IS NULL
)
SELECT DISTINCT * FROM child_orgs
UNION ALL
SELECT DISTINCT * FROM parent_orgs
"), array('thisOrgId' => $this->id));
return Arr::pluck($results, 'id');
}
/**
* Get the independent activities for the organization
*/
public function independentActivities()
{
return $this->hasMany('App\Models\IndependentActivity', 'organization_id');
}
/**
* Get the allowed visibility types for the organization.
*/
public function allowedVisibilityTypes()
{
return $this->belongsToMany('App\Models\OrganizationVisibilityType', 'allowed_organization_visibility_types');
}
}