-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLmsController.php
More file actions
259 lines (239 loc) · 9.98 KB
/
LmsController.php
File metadata and controls
259 lines (239 loc) · 9.98 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
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
257
258
259
<?php
namespace App\Http\Controllers\Api\V1\CurrikiGo;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\CurrikiGo\OrganizationSearchRequest;
use App\Http\Requests\V1\CurrikiGo\TeamsSearchRequest;
use App\Http\Resources\V1\ProjectPublicResource;
use App\Http\Resources\V1\TeamResource;
use App\Http\Resources\V1\SearchResource;
use App\Repositories\CurrikiGo\LmsSetting\LmsSettingRepositoryInterface;
use App\Repositories\Activity\ActivityRepositoryInterface;
use App\Http\Resources\V1\OrganizationResource;
use App\Models\CurrikiGo\LmsSetting;
use App\CurrikiGo\Canvas\Client;
use App\CurrikiGo\Canvas\SaveTeacherData;
use App\Http\Requests\V1\IndependentActivityForDeeplink;
use App\Http\Resources\V1\IndependentActivityResource;
use App\Repositories\Project\ProjectRepositoryInterface;
use App\Repositories\IndependentActivity\IndependentActivityRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Validator;
use App\Models\Project;
use App\Repositories\GoogleClassroom\GoogleClassroomRepositoryInterface;
use App\Repositories\User\UserRepositoryInterface;
use App\Services\SaveStudentdataService;
use App\User;
use stdClass;
/**
* @group 26. LMS Settings
*
* APIs for LMS settings used for publishing
*/
class LmsController extends Controller
{
private $lmsSettingRepository;
private $projectRepository;
private $activityRepository;
private $independentActivityRepository;
/**
* LmsController constructor.
*
* @param $lmsSettingRepository LmsSettingRepositoryInterface
* @param $projectRepository ProjectRepositoryInterface
* @param $activityRepository ActivityRepositoryInterface
* @param $independentActivityRepository IndependentActivityRepositoryInterface
*/
public function __construct(
LmsSettingRepositoryInterface $lmsSettingRepository,
ProjectRepositoryInterface $projectRepository,
ActivityRepositoryInterface $activityRepository,
IndependentActivityRepositoryInterface $independentActivityRepository
)
{
$this->lmsSettingRepository = $lmsSettingRepository;
$this->projectRepository = $projectRepository;
$this->activityRepository = $activityRepository;
$this->independentActivityRepository = $independentActivityRepository;
}
/**
* Get Projects based on LMS/LTI settings
*
* Get a list of projects that belong to the same LMS/LTI settings
*
* @bodyParam lms_url string required The url of a lms Example: quo
* @bodyParam lti_client_id integer required The Id of a lti client Example: 12
*
* @responseFile responses/project/projects.json
*
* @param Request $request
* @param GoogleClassroomRepositoryInterface $googleClassroomRepository
* @param UserRepositoryInterface $userRepository
* @return Response
*/
// TODO: need to update
public function projects(Request $request, GoogleClassroomRepositoryInterface $googleClassroomRepository, UserRepositoryInterface $userRepository)
{
$validator = Validator::make($request->all(), [
'lti_client_id' => 'required',
'user_email' => 'required|email',
'course_id' => 'required',
'api_domain_url' => 'required',
'course_name' => 'required'
]);
if ($validator->fails()) {
$messages = $validator->messages();
return response(['error' => $messages], 400);
}
// format data to make compatible with saveData function
$data = new stdClass();
$data->issuerClient = $request->lti_client_id;
$data->courseId = $request->course_id;
$data->customApiDomainUrl = $request->api_domain_url;
if (config('student-data.save_student_data') && $request->isLearner) {
$data->studentId = $request->studentId;
$data->customPersonNameGiven = $request->customPersonNameGiven;
$data->customPersonNameFamily = $request->customPersonNameFamily;
$service = new SaveStudentdataService();
$service->saveStudentData($data);
}
$lmsSetting = $this->lmsSettingRepository->getOrganization($data->issuerClient, $request->user_email);
$lms_organization_id = null;
if ($lmsSetting && $lmsSetting['organization_obj']) {
$lms_organization_id = $lmsSetting['organization_obj']->organization_id;
$duplicateRecord = $googleClassroomRepository->duplicateRecordValidation($data->courseId, $request->user_email);
$userExists = $userRepository->findByField('email', $request->user_email);
if (!$userExists) {
$userExists = $userRepository->getFirstUser();
}
if (!$duplicateRecord) {
$teacherInfo = new \stdClass();
$teacherInfo->user_id = $userExists->id;
$teacherInfo->id = $data->courseId;
$teacherInfo->name = $request->course_name;
$teacherInfo->alternateLink = $data->customApiDomainUrl . '/' . $data->courseId;
$teacherInfo->curriki_teacher_email = $request->user_email;
$teacherInfo->curriki_teacher_org = $lms_organization_id;
$response[] = $googleClassroomRepository->saveCourseShareToGcClass($teacherInfo);
}
}
//For new deeplink UI
$searchTerm = $request->search_keyword ?? null;
$projects = $this->projectRepository->fetchByLtiClientAndEmail(
$request->input('lti_client_id'),
$request->input('user_email'),
$searchTerm,
$lmsSetting['organization_id']
);
return response([
'projects' => SearchResource::collection($projects),
], 200);
}
public function project(Project $project) {
return response([
'project' => new ProjectPublicResource($project),
], 200);
}
public function activities(Request $request)
{
$request->validate([
'query' => 'string|max:255',
'from' => 'integer',
'subject' => 'string|max:255',
'level' => 'string|max:255',
'start' => 'string|max:255',
'end' => 'string|max:255',
'author' => 'string|max:255',
'private' => 'integer',
'userEmail' => 'string|required|max:255',
'ltiClientId' => 'string|required',
]);
return response([
'activities' => $this->activityRepository->ltiSearchForm($request),
], 200);
}
/**
* Get organizations based on LMS/LTI settings
*
* Get a list of organizations that belong to the same LMS/LTI settings
*
* @bodyParam userEmail string required The email of a user: quo
* @bodyParam lti_client_id integer required The Id of a lti client Example: 12
*
* @responseFile responses/organization/organizations.json
*
* @param OrganizationSearchRequest $request
* @return Response
*/
public function organizations(OrganizationSearchRequest $request)
{
$verifyValidCall = LmsSetting::where('lti_client_id', $request->ltiClientId)->where('lms_login_id', 'ilike', $request->userEmail)->pluck('organization_id');
if ($verifyValidCall) {
$user = User::where('email', $request->input('userEmail'))->first();
$organizations = OrganizationResource::collection($user->specificOrganizationsWithClientId($verifyValidCall)->with('parent')->get());
return response([
'organizations' => $organizations,
], 200);
}
return response([
'organizations' => [],
], 400);
}
/**
* Get teams based on LMS/LTI settings
*
* Get a list of teams that belong to the same LMS/LTI settings
*
* @bodyParam user_email string required The email of a user Example: somebody@somewhere.com
* @bodyParam lti_client_id integer required The Id of a lti client Example: 12
*
* @responseFile responses/team/teams.json
*
* @param TeamsSearchRequest $request
* @return Response
*/
public function teams(TeamsSearchRequest $request)
{
$verifyValidCall = LmsSetting::where('lti_client_id', $request->lti_client_id)->where('lms_login_id', 'ilike', $request->user_email)->pluck('organization_id');
if ($verifyValidCall) {
$user = User::where('email', $request->input('user_email'))->first();
$teams = TeamResource::collection($user->teamsWithOrgs($verifyValidCall)->get());
return response([
'teams' => $teams,
], 200);
}
return response([
'teams' => [],
], 400);
}
/**
* Get independent Activity based on user_id
*
* Get independent Activity based on user_id of a user who launched the deeplink
*
* @bodyParam user_email string required The email of a user Example: somebody@somewhere.com
* @bodyParam query string For search-term Example: activity title
* @bodyParam size integer For pagination Example: 10
*
* @responseFile 200 responses/independent-activity/independent-activity.json
*
* @response 400 {
* "errors": [
* "Could not find any independent activity. Please try again later."
* ]
* }
*
* @param IndependentActivityForDeeplink $request
*/
public function independentActivities(IndependentActivityForDeeplink $request)
{
$orgs = LmsSetting::where('lti_client_id', $request->lti_client_id)->where('lms_login_id', 'ilike', $request->user_email)->pluck('organization_id');
$user = User::where('email', strtolower($request->user_email))->first();
if($user){
return IndependentActivityResource::collection($this->independentActivityRepository->independentActivities($request, $user->id, $orgs));
}
return response([
'data' => ['Could not find any independent activity. Please try again later.'],
], 400);
}
}