-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathExtractXAPIJSONController.php
More file actions
288 lines (262 loc) · 16.2 KB
/
ExtractXAPIJSONController.php
File metadata and controls
288 lines (262 loc) · 16.2 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
namespace App\Http\Controllers\Api\V1\CurrikiGo;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Repositories\Activity\ActivityRepositoryInterface;
use App\Repositories\LRSStatementsData\LRSStatementsDataRepositoryInterface;
use App\Repositories\LRSStatementsSummaryData\LRSStatementsSummaryDataRepositoryInterface;
use App\Services\LearnerRecordStoreService;
use Illuminate\Support\Facades\DB;
use App\CurrikiGo\LRS\InteractionFactory;
use App\Repositories\GoogleClassroom\GoogleClassroomRepositoryInterface;
use App\Repositories\IndependentActivity\IndependentActivityRepositoryInterface;
/**
* @group 16. XAPI Cron
*
* Cron job for XAPI extract
*/
class ExtractXAPIJSONController extends Controller
{
/**
* xAPI extract job script
*
* Runs the xAPI extract job script
*
* @param ActivityRepositoryInterface $activityRepository
* @param LRSStatementsDataRepositoryInterface $lrsStatementsRepository
* @param LRSStatementsSummaryDataRepositoryInterface $lrsStatementsSummaryDataRepositoryInterface
* @param GoogleClassroomRepositoryInterface $googleClassroom
* @return void
*/
public function runJob(
ActivityRepositoryInterface $activityRepository,
LRSStatementsDataRepositoryInterface $lrsStatementsRepository,
LRSStatementsSummaryDataRepositoryInterface $lrsStatementsSummaryDataRepositoryInterface,
GoogleClassroomRepositoryInterface $googleClassroom,
IndependentActivityRepositoryInterface $independentActivityRepository
) {
$max_statement_id = $lrsStatementsRepository->findMaxByField('statement_id');
if (!$max_statement_id) {
$max_statement_id = 0;
}
\Log::info(date('Y-m-d h:i:s') . ' - Extract XAPI script - started from max ID: ' . $max_statement_id);
$offset = 0;
$limit = config('xapi.lrs_job_row_limit');
$xapiStatements = DB::connection('lrs_pgsql')->table(config('xapi.lrs_db_statements_table'))->select()
->offset($offset)
->limit($limit)
->where('voided', false)
->where('id', '>', $max_statement_id)
->orderby('id', 'ASC')
->get();
try {
$service = new LearnerRecordStoreService();
foreach ($xapiStatements as $row) {
$insertData = [];
$statement = $service->buildStatementfromJSON($row->data);
$actor = $statement->getActor();
$target = $statement->getTarget();
$verb = $service->getVerbFromStatement($statement->getVerb());
$context = $statement->getContext();
$nameOfActivity = 'Unknown Quiz set';
$defaultActivityName = true;
// At the moment, we're only tackling targets of 'activity' type.
$definition = ($target->getObjectType() === 'Activity' ? $target->getDefinition() : '');
// In some cases, we do not have a 'name' property for the object.
// So, we've added an additional check here.
// @todo - the LRS statements generated need to have this property
if ($target->getObjectType() === 'Activity' && !empty($definition) && !$definition->getName()->isEmpty()) {
$nameOfActivity = $definition->getName()->getNegotiatedLanguageString();
$defaultActivityName = false;
} elseif ($target->getObjectType() === 'StatementRef') {
$nameOfActivity = $target->getId();
}
$result = $statement->getResult();
if (!empty($actor->getAccount())) {
$insertData['actor_id'] = $actor->getAccount()->getName();
$insertData['actor_homepage'] = $actor->getAccount()->getHomePage();
} else {
// If will be mbox / name
$insertData['actor_id'] = $actor->getMbox();
$insertData['actor_homepage'] = $actor->getName();
}
$insertData['statement_id'] = $row->id;
$insertData['statement_uuid'] = $statement->getId();
$insertData['object_name'] = $nameOfActivity;
$insertData['datetime'] = $row->created_at;
$insertData['object_id'] = $target->getId();
$insertData['verb'] = $verb;
if (!empty($context)) {
$contextActivities = $context->getContextActivities();
$other = $contextActivities->getOther();
$groupingInfo = $service->findGroupingInfo($other);
$platform = $context->getPlatform();
}
// Skip if we don't have the activity.
if (empty($groupingInfo['activity']) || empty($groupingInfo['class']) || empty($context)) {
// It maybe an old format statement. Just save verb, object and actor, and move on.
$inserted = $lrsStatementsRepository->create($insertData);
continue;
}
$activity = $activityRepository->find($groupingInfo['activity']);
$activityId = null;
$activityName = null;
$projectId = null;
$projectName = null;
$playlistId = null;
$playlistTitle = null;
if ($activity) {
if ($activity->activity_type === config('constants.activity_type.activity')) {
$activityId = $activity->id;
$activityName = $activity->title;
$project = $activity->playlist->project;
$projectId = $project->id;
$projectName = $project->name;
$playlistId = $activity->playlist_id;
$playlistTitle = $activity->playlist->title;
} else {
$activityId = $activity->id;
$activityName = $activity->title;
$project = NULL;
$projectId = NULL;
$projectName = NULL;
$playlistId = NULL;
$playlistTitle = NULL;
}
}
$category = $contextActivities->getCategory();
$categoryId = '';
$h5pInteraction = '';
if (!empty($category)) {
$categoryId = end($category)->getId();
$h5pInteraction = explode("/", $categoryId);
$h5pInteraction = end($h5pInteraction);
}
$insertData['class_id'] = $groupingInfo['class'];
$insertData['project_id'] = $projectId;
$insertData['playlist_id'] = $playlistId;
$insertData['assignment_submitted'] = ($verb === LearnerRecordStoreService::SUBMITTED_VERB_NAME ? TRUE : FALSE);
$insertData['activity_category'] = $h5pInteraction;
$insertData['platform'] = $platform;
$insertData['project_name'] = $projectName;
$insertData['playlist_name'] = $playlistTitle;
$insertData['assignment_id'] = $activityId;
$insertData['assignment_name'] = $activityName;
//added submitted_id and attempt_id column for new summary page
$insertData['submission_id'] = $groupingInfo['submission'];
$insertData['attempt_id'] = $groupingInfo['attempt'];
//organization id of which this activity belongs to
if (isset($project->organization_id)) {
$insertData['activity_org_id'] = $project->organization_id;
}
// Extract information from object.definition.extensions
if ($target->getObjectType() === 'Activity' && !empty($definition)) {
$glassAltCourseId = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_GCLASS_ALTERNATE_COURSE_ID);
$glassEnrollmentCode = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_GCLASS_ENROLLMENT_CODE);
$courseName = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_COURSE_NAME);
if (empty($glassAltCourseId)) {
$courseName = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_LMS_COURSE_NAME);
$glassAltCourseId = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_LMS_DOMAIN_URL);
// CANVAS: Need to make this alt course id URL unique; appending class id to the domain URL
// Only if API domain is available, only then we save the 'alternate URL', else we keep it blank.
if (!empty($glassAltCourseId)) {
$glassAltCourseId .= '/' . $groupingInfo['class'];
}
$glassEnrollmentCode = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_LMS_COURSE_CODE);
}
// Only fetching teacher_email_id of gclass_api_data column as in each respective case (LTI DL, Publishing from CS) email of publisher is already being stored
if ($glassAltCourseId) {
$publisherData = $googleClassroom->fetchPublisherData($glassAltCourseId);
if ($publisherData && $publisherData->publisher_user != null) {
$insertData['publisher_id'] = $publisherData['publisherUser']['id'];
$insertData['publisher_org_id'] = $publisherData['curriki_teacher_org'];
}
}
$chapterName = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_H5P_CHAPTER_NAME);
$chapterIndex = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_H5P_CHAPTER_INDEX);
$referrer = $service->getExtensionValueFromList($definition, LearnerRecordStoreService::EXTENSION_REFERRER);
$insertData['glass_alternate_course_id'] = $glassAltCourseId;
$insertData['glass_enrollment_code'] = $glassEnrollmentCode;
$insertData['course_name'] = $courseName;
$insertData['chapter_name'] = (!empty($chapterName) ? $chapterName : 0);
$insertData['chapter_index'] = $chapterIndex;
$insertData['referrer'] = $referrer;
// Saving publisherId and publisherOrg in shared links case
if ($referrer != null && !empty($project) && $project->users && count($project->users) > 0) {
$insertData['publisher_id'] = $project->users[0]->id;
$insertData['publisher_org_id'] = $project->organization_id;
}
}
$interactionFactory = new InteractionFactory();
$interaction = $interactionFactory->initInteraction($statement);
$interactionSummaryGlobal = '';
if ($interaction) {
$interactionSummary = $interaction->summary();
$interactionSummaryGlobal = $interactionSummary;
// Pull this from interaction...
$insertData['question'] = $interactionSummary['description'];
$insertData['duration'] = (!empty($interactionSummary['raw-duration']) ? $interactionSummary['raw-duration'] : null);
$insertData['options'] = (isset($interactionSummary['choices']) ? implode(", ", $interactionSummary['choices']) : null);
if (isset($interactionSummary['response']) && !empty($interactionSummary['response'])) {
$insertData['answer'] = (is_array($interactionSummary['response']) ? implode(", ", $interactionSummary['response']) : $interactionSummary['response']);
}
if ($interactionSummary['scorable'] || (isset($interactionSummary['score']) && $interactionSummary['score']['max'] > 0)) {
$insertData['score_scaled'] = $interactionSummary['score']['scaled'];
$insertData['score_min'] = $interactionSummary['score']['min'];
$insertData['score_max'] = $interactionSummary['score']['max'];
$insertData['score_raw'] = $interactionSummary['score']['raw'];
}
}
// Overriding object name, when we have Questionnaire H5P, and object name is not available.
if (
$defaultActivityName &&
($h5pInteraction &&
in_array($insertData['verb'], ['completed', 'progressed']) &&
preg_match('/^H5P.Questionnaire/', $h5pInteraction, $matches))
) {
$insertData['object_name'] = $matches[0];
}
// need to determine column layout interaction on 'completed'.
if (($h5pInteraction &&
in_array($insertData['verb'], ['completed', 'progressed']) &&
preg_match('/^H5P.Column/', $h5pInteraction))
) {
$insertData['page'] = $insertData['object_name'];
$insertData['page_completed'] = $insertData['verb'] === 'completed' ? true : false;
}
$inserted = $lrsStatementsRepository->create($insertData);
if ($inserted) {
//Capturing the custom verb "summary-curriki" for submit event with full summary rdbms..
if ($verb === 'summary-curriki' && !empty($interactionSummaryGlobal)) {
if (isset($interactionSummaryGlobal['response']) && !empty($interactionSummaryGlobal['response'])) {
$responseObject = (is_array(json_decode($interactionSummaryGlobal['response'], true)) ? json_decode($interactionSummaryGlobal['response'], true) : false);
if (!empty($responseObject)) {
foreach ($responseObject as $response) {
$insertSummary = [];
$insertSummary['statement_id'] = $insertData['statement_id'];
$insertSummary['statement_uuid'] = $insertData['statement_uuid'];
$insertSummary['actor_homepage'] = $insertData['actor_homepage'];
$insertSummary['class_id'] = $insertData['class_id'];
$insertSummary['assignment_name'] = $insertData['assignment_name'];
$insertSummary['actor_id'] = $insertData['actor_id'];
$insertSummary['page_name'] = $response['title'];
$insertSummary['is_page_accessed'] = $response['accessed'];
$insertSummary['is_event_interacted'] = $response['interacted'];
$insertSummary['interacted_count'] = $response['interacted_count'];
$insertSummary['total_interactions'] = $response['total_interactions'];
$insertSummary['score'] = $response['score'];
$lrsStatementsSummaryDataRepositoryInterface->create($insertSummary);
\Log::info(date('Y-m-d h:i:s') . ' - RDBMS custom verb statement with statement id : ' . $insertData['statement_id'] . ' processed');
}
}
}
}
}
}
\Log::info(date('Y-m-d h:i:s') . ' - Extract XAPI script ended');
return 'Extract XAPI JSON Cron run successfully.';
} catch (Exception $e) {
\Log::error($e->getMessage());
}
}
}