This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
anu_lms.install
247 lines (228 loc) · 7.34 KB
/
anu_lms.install
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
<?php
/**
* @file
* Update hooks for the module.
*/
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Core\Database\Database;
/**
* Implements hook_schema().
*/
function anu_lms_schema() {
$schema['anu_lms_progress'] = [
'description' => 'Stores user progress across ANU LMS content.',
'fields' => [
'uid' => [
'description' => 'The {users}.id this record affects.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'nid' => [
'description' => 'The {node}.nid completed by the {users}.id.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'created' => [
'description' => 'The Unix timestamp when the entry was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'changed' => [
'description' => 'The Unix timestamp when the entry was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['uid', 'nid'],
'indexes' => [
'uid' => ['uid'],
],
// For documentation purposes only; foreign keys are not created in the
// database.
'foreign keys' => [
'data_user' => [
'table' => 'users',
'columns' => [
'uid' => 'uid',
],
],
],
];
return $schema;
}
/**
* Implements hook_install().
*/
function anu_lms_install($is_syncing) {
// The module is being installed as part of a config import so the changes
// below have been applied already and should be in the configuration set
// being imported.
if ($is_syncing) {
return;
}
// Change form display to show the correct widget.
// This way config overrides on features are avoided
// when new fields are added to the paragraph.
$storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
$id = 'paragraph.course_modules.default';
$formDisplay = $storage->load($id);
if (!$formDisplay) {
$formDisplay = $storage->create([
'id' => $id,
'status' => TRUE,
'targetEntityType' => 'paragraph',
'bundle' => 'course_modules',
'mode' => 'default',
]);
}
$formDisplay->set('hidden', [
'created' => TRUE,
'status' => TRUE,
]);
$content = $formDisplay->get('content');
$content = [
'field_module_lessons' => [
'type' => 'inline_entity_form_complex',
'weight' => 1,
'region' => 'content',
'settings' => [
'form_mode' => 'embedded',
'override_labels' => TRUE,
'label_singular' => 'lesson',
'label_plural' => 'lessons',
'allow_new' => TRUE,
'allow_existing' => TRUE,
'match_operator' => 'CONTAINS',
'revision' => FALSE,
'collapsible' => FALSE,
'collapsed' => FALSE,
'allow_duplicate' => FALSE,
],
'third_party_settings' => [],
],
'field_module_title' => [
'type' => 'string_textfield',
'weight' => 0,
'region' => 'content',
'settings' => [
'size' => 60,
'placeholder' => '',
],
'third_party_settings' => [],
],
];
$formDisplay->set('content', $content);
$formDisplay->save();
}
/**
* Migrate modules content from nodes to paragraphs.
*/
function anu_lms_update_8001() {
/** @var \Drupal\node\Entity\NodeInterface[] $courses */
$courses = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'course']);
foreach ($courses as $course) {
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem[] $modules */
$modules_references = $course->get('field_course_modules');
if (!empty($modules_references)) {
foreach ($modules_references as $modules_reference) {
$lesson_nids = [];
$quiz_nid = FALSE;
/** @var \Drupal\node\Entity\NodeInterface $module */
$module = Node::load($modules_reference->target_id);
if (!empty($module)) {
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem[] $lessons_references */
$lessons_references = $module->get('field_module_lessons');
foreach ($lessons_references as $lessons_reference) {
$lesson_nids[] = $lessons_reference->target_id;
}
/** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $quiz_references */
$quiz_references = $module->get('field_module_assessment');
foreach ($quiz_references as $quiz_reference) {
$quiz_nid = $quiz_reference->target_id;
}
/** @var \Drupal\paragraphs\Entity\ParagraphInterface $paragraph */
$paragraph = Paragraph::create(['type' => 'course_modules']);
$paragraph->set('field_module_title', $module->label());
foreach ($lesson_nids as $nid) {
$paragraph->field_module_lessons[] = $nid;
}
if (!empty($quiz_nid)) {
$paragraph->field_module_assessment = $quiz_nid;
}
$paragraph->save();
$current = $course->get('field_course_module')->getValue();
$current[] = [
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
$course->set('field_course_module', $current);
}
}
$course->save();
}
}
}
/**
* Migrate is_highlight boolean field on image bullet list paragraphs to
* default color.
*/
function anu_lms_update_8002() {
/** @var \Drupal\node\Entity\NodeInterface[] $lessons */
$lessons = Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'module_lesson']);
foreach ($lessons as $lesson) {
/** @var \Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevisionsItem[] $sections */
$sections = $lesson->get('field_module_lesson_content')
->referencedEntities();
foreach ($sections as $section) {
/** @var \Drupal\paragraphs\Entity\Paragraph $contents */
$contents = $section->get('field_lesson_section_content')
->referencedEntities();
foreach ($contents as $content) {
if ($content->getType() == 'lesson_img_list') {
/** @var \Drupal\Core\Field\FieldItemList $highlight_field */
$highlight_field = $content->get('field_lesson_img_list_highlight');
if ($highlight_field->getString() == "1") {
$content->set('field_lesson_highlight_color', 'yellow');
$content->save();
}
}
}
}
}
}
/**
* Create a new table for storing user progress.
*/
function anu_lms_update_8220() {
$schema = anu_lms_schema();
\Drupal::database()->schema()->createTable('anu_lms_progress', $schema['anu_lms_progress']);
}
/**
* Create new columns on the progress table.
*/
function anu_lms_update_8240() {
$createdSpec = [
'description' => 'The Unix timestamp when the entry was created.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
];
$changedSpec = [
'description' => 'The Unix timestamp when the entry was most recently saved.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
];
$schema = Database::getConnection()->schema();
$schema->addField('anu_lms_progress', 'created', $createdSpec);
$schema->addField('anu_lms_progress', 'changed', $changedSpec);
}