-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLtiToolSettingRepository.php
More file actions
161 lines (149 loc) · 5.29 KB
/
LtiToolSettingRepository.php
File metadata and controls
161 lines (149 loc) · 5.29 KB
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
<?php
namespace App\Repositories\LtiTool;
use App\Exceptions\GeneralException;
use App\Models\LtiTool\LtiToolSetting;
use App\Models\Organization;
use App\Repositories\BaseRepository;
use Illuminate\Support\Facades\Log;
/**
* @author Asim Sarwar
* Date 11-10-2021
* Class LtiToolSettingRepository
*/
class LtiToolSettingRepository extends BaseRepository implements LtiToolSettingInterface
{
/**
* LtiToolSettingRepository constructor
* @param LtiToolSetting $model
*/
public function __construct(LtiToolSetting $model)
{
$this->model = $model;
}
/**
* @param $data
* @return mixed
*/
public function getAll($data, $suborganization)
{
$perPage = isset($data['size']) ? $data['size'] : config('constants.default-pagination-per-page');
$query = $this->model->with(['user', 'organization', 'mediaSources']);
if (isset($data['query']) && $data['query'] !== '') {
$query->where(function ($query) use ($data) {
$query = $query->whereHas('user', function ($qry) use ($data) {
$qry->where('email', 'iLIKE', '%' . $data['query'] . '%');
});
$query->orWhere('tool_name', 'iLIKE', '%' . $data['query'] . '%');
$query->orWhere('tool_url', 'iLIKE', '%' . $data['query'] . '%');
});
}
if (isset($data['order_by_column']) && $data['order_by_column'] !== '')
{
$orderByType = isset($data['order_by_type']) ? $data['order_by_type'] : 'ASC';
$query->orderBy($data['order_by_column'], $orderByType);
}
if (isset($data['filter']) && $data['filter'] > 0) {
$query = $query->whereHas('mediaSources', function ($qry) use ($data) {
$qry->where('id', $data['filter']);
});
}
return $query->where('organization_id', $suborganization->id)->paginate($perPage)->withQueryString();
}
/**
* @param $data
* @return mixed
* @throws GeneralException
*/
public function create($data)
{
try {
if ($createSetting = $this->model->create($data)) {
return ['message' => 'Lti Tool setting created successfully!', 'data' => $createSetting];
}
} catch (\Exception $e) {
Log::error($e->getMessage());
}
throw new GeneralException('Unable to create lti tool setting, please try again later!');
}
/**
* @param $data
* @return mixed
* @throws GeneralException
*/
public function update($id, $data)
{
try {
if ($this->find($id)->update($data)) {
return ['message' => 'Lti tool setting updated successfully!', 'data' => $this->find($id)];
}
} catch (\Exception $e) {
Log::error($e->getMessage());
}
throw new GeneralException('Unable to update lti tool setting, please try again later!');
}
/**
* @param $id
* @return mixed
* @throws GeneralException
*/
public function find($id)
{
if ($setting = $this->model->find($id)) {
return $setting;
}
throw new GeneralException('Lti tool setting not found.');
}
/**
* @param $id
* @return mixed
* @throws GeneralException
*/
public function destroy($id)
{
try {
$this->find($id)->delete();
return ['message' => 'Lti tool setting deleted!', 'data' => []];
} catch (\Exception $e) {
Log::error($e->getMessage());
}
throw new GeneralException('Unable to delete lti tool setting, please try again later!');
}
/**
* To clone Lti Tool Setting
* @param LtiToolSetting $ltiToolSetting
* @param Organization $subOrganization
* @param int $userId
* @return int id
*/
public function clone(LtiToolSetting $ltiToolSetting, Organization $subOrganization, $userId)
{
$ltiToolSettingData = [
"user_id" => $userId,
"organization_id" => $subOrganization->id,
"tool_name" => $ltiToolSetting->tool_name,
"tool_url" => $ltiToolSetting->tool_url,
"tool_domain" => $ltiToolSetting->tool_domain,
"lti_version" => $ltiToolSetting->lti_version,
"media_source_id" => $ltiToolSetting->media_source_id,
"tool_consumer_key" => $ltiToolSetting->tool_consumer_key,
"tool_secret_key" => $ltiToolSetting->tool_secret_key,
"tool_description" => $ltiToolSetting->tool_description,
"tool_custom_parameter" => $ltiToolSetting->tool_custom_parameter,
"tool_content_selection_url" => $ltiToolSetting->tool_content_selection_url
];
$clonedSetting = $this->create($ltiToolSettingData);
return $clonedSetting['id'];
}
/**
* @param $userId integer, $orgId integer $mediaSourceId int
* @return mixed
*/
public function getRowRecordByUserOrgAndToolType($userId, $orgId, $mediaSourceId)
{
try {
return $this->model->where([['user_id','=', $userId],['organization_id','=', $orgId],['media_source_id','=', $mediaSourceId]])->first();
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}