Skip to content

Commit fe30f53

Browse files
author
Ron Lucke
authored
Add missing data and settings
1 parent 5ec32ab commit fe30f53

1 file changed

Lines changed: 101 additions & 41 deletions

File tree

command/MigrateCoursewareCommand.php

Lines changed: 101 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Courseware\StructuralElement as StructuralElement;
1212
use Courseware\Container as Container;
1313
use Courseware\Block as Block;
14+
use Courseware\Instance as Instance;
1415

1516
/**
1617
* @author Ron Lucke <lucke@elan-ev.de>
@@ -54,7 +55,7 @@ public function execute(InputInterface $input, OutputInterface $output)
5455
} else {
5556
$helper = $this->getHelper('question');
5657
$question = new ConfirmationQuestion('<question>Are you sure you want to migrate all coursewares at once? (y|n)</question> ', false);
57-
if(!$helper->ask($input, $output, $question)) {
58+
if (!$helper->ask($input, $output, $question)) {
5859
$output->writeln('<comment>Migration canceled.</comment>');
5960
$output->writeln('<info>You can migrate individual courses if you use the course id as an argument.</info>');
6061
return 0;
@@ -68,7 +69,7 @@ public function execute(InputInterface $input, OutputInterface $output)
6869
$output->writeln('<batch-info>Batch migration for ' . $coursewares_count . ' Coursewares.</batch-info>');
6970
}
7071

71-
foreach($coursewares as $courseware) {
72+
foreach ($coursewares as $courseware) {
7273
$this->migrateCourse($courseware, $output);
7374
}
7475
}
@@ -83,8 +84,9 @@ private function migrateCourse($courseware, $output)
8384
$cid = $courseware->seminar_id;
8485
$course = \Course::find($cid);
8586

86-
if(empty($course)) {
87+
if (empty($course)) {
8788
$output->writeln('<skiped>Course (' . $cid . ')' . ' does not exist in database.</skiped>');
89+
8890
return false;
8991
}
9092

@@ -93,8 +95,9 @@ private function migrateCourse($courseware, $output)
9395
$plugin_manager->setPluginActivated($plugin_info['id'], $cid, true);
9496

9597
$is_migrated = \Mooc\DB\MigrationStatus::findBySQL('seminar_id = ?', array($cid));
96-
if($is_migrated) {
98+
if ($is_migrated) {
9799
$output->writeln('<skiped>Courseware for ' . $course->name . '(' . $course->id . ')' . ' has already been migrated.</skiped>');
100+
98101
return false;
99102
}
100103
$grouped = $this->getGrouped($cid, true);
@@ -127,6 +130,7 @@ function($memo, $item) use($remote) {
127130
$arr['fields'] = $ui_block->getFields();
128131
}
129132
$memo[$item->parent_id][] = $arr;
133+
130134
return $memo;
131135
},
132136
array());
@@ -136,7 +140,7 @@ function($memo, $item) use($remote) {
136140

137141
private function getSubElement($type) {
138142
$sub_element = null;
139-
switch($type) {
143+
switch ($type) {
140144
case 'Courseware':
141145
$sub_element = 'Chapter';
142146
break;
@@ -190,14 +194,9 @@ private function createNewCourseware($courseware, $output)
190194
$courseTeacher = $course->getMembersWithStatus('dozent')[0];
191195
$teacher = \User::find($courseTeacher->user_id);
192196

193-
//get courseware settings
194-
$settings = [];
195-
$settings['progression'] = ($courseware['ui_block']->progression);
196-
$settings['editing_permission'] = ($courseware['ui_block']->editing_permission);
197-
198197
$root = StructuralElement::getCoursewareCourse($cid);
199198

200-
if($root === null) {
199+
if ($root === null) {
201200
$root = StructuralElement::build([
202201
'parent_id' => null,
203202
'range_id' => $cid,
@@ -210,37 +209,47 @@ private function createNewCourseware($courseware, $output)
210209

211210
$root->store();
212211
}
212+
213+
//get courseware settings
214+
$instance = new Instance($root);
215+
if ($courseware['ui_block']->editing_permission === 'dozent') {
216+
$instance->setEditingPermissionLevel('dozent');
217+
}
218+
if ($courseware['ui_block']->progression === 'seq') {
219+
$instance->setSequentialProgression(true);
220+
}
221+
213222
$subchapter_map = [];
214223
$section_map = [];
215224
$new_link_blocks = [];
216225
$structural_element_counter = 0;
217226
$container_counter = 0;
218227
$block_counter = 0;
219-
foreach($courseware['children'] as $chapter) {
228+
foreach ($courseware['children'] as $chapter) {
220229
$new_chapter = $this->createStructuralElement($chapter, $root->id, $teacher, $cid, $output);
221230
$structural_element_counter++;
222-
foreach($chapter['children'] as $subchapter) {
231+
foreach ($chapter['children'] as $subchapter) {
223232
$new_subchapter = $this->createStructuralElement($subchapter, $new_chapter->id, $teacher, $cid, $output);
224233
$structural_element_counter++;
225234
$subchapter_map[$subchapter['id']] = $new_subchapter->id;
226-
foreach($subchapter['children'] as $section) {
235+
foreach ($subchapter['children'] as $section) {
227236
$new_section = $this->createStructuralElement($section, $new_subchapter->id, $teacher, $cid, $output);
228237
$structural_element_counter++;
229238
$section_map[$section['id']] = $new_section->id;
230239
$new_container = $this->createContainer($new_section->id, $teacher, $output);
231240
$container_counter++;
232-
foreach($section['children'] as $block) {
241+
foreach ($section['children'] as $block) {
233242
$create_new_block = $this->createBlock($block, $new_container, $teacher, $cid, $courseware['ui_block'], $output);
234243
$new_block = $create_new_block['new_block'];
235-
if($new_block === null) {
244+
if ($new_block === null) {
236245
continue;
237246
}
238247
$block_counter++;
239-
if($new_block->type->getType() === 'link') {
248+
if ($new_block->type->getType() === 'link') {
240249
array_push($new_link_blocks, array('block' => $new_block, 'link_target' => $create_new_block['link_target']));
241250
}
242251
$user_progresses = \Mooc\DB\UserProgress::findBySQL('block_id = ?', array($block['id']));
243-
foreach($user_progresses as $user_progress) {
252+
foreach ($user_progresses as $user_progress) {
244253
$progress = \Courseware\UserProgress::build([
245254
'user_id' => $user_progress['user_id'],
246255
'block_id' => $new_block->id,
@@ -257,15 +266,15 @@ private function createNewCourseware($courseware, $output)
257266
}
258267
}
259268
}
260-
foreach($new_link_blocks as $link_block) {
269+
foreach ($new_link_blocks as $link_block) {
261270
$new_link_block = $link_block['block'];
262271
$old_target = $link_block['link_target']['id'];
263272
$payload = json_decode($new_link_block->payload);
264273
if ($payload->type === 'internal') {
265-
if($link_block['link_target']['element'] === 'section') {
274+
if ($link_block['link_target']['element'] === 'section') {
266275
$payload->target = $section_map[$old_target];
267276
}
268-
if($link_block['link_target']['element'] === 'subchapter') {
277+
if ($link_block['link_target']['element'] === 'subchapter') {
269278
$payload->target = $subchapter_map[$old_target];
270279
}
271280
$new_link_block->payload = json_encode($payload);
@@ -285,19 +294,69 @@ private function createNewCourseware($courseware, $output)
285294

286295
private function createStructuralElement($element, $parent_id, $user, $cid, $output)
287296
{
288-
$element = StructuralElement::build([
297+
$structural_element = StructuralElement::build([
289298
'parent_id' => $parent_id,
290299
'range_id' => $cid,
291300
'range_type' => 'course',
292301
'owner_id' => $user->id,
293302
'editor_id' => $user->id,
303+
'position' => $element['position'],
294304
'purpose' => 'content',
295305
'title' => $element['title'],
306+
'release_date' => $element['publication_date'] !== null ? $element['publication_date'] : '',
307+
'withdraw_date' => $element['withdraw_date'] !== null ? $element['withdraw_date'] : ''
296308
]);
297309

298-
$element->store();
310+
if (isset($element['approval'])) {
311+
$approval = json_decode($element['approval']);
312+
313+
$has_read = false;
314+
$read_approval = [];
315+
$read_approval['all'] = $approval->settings->defaultRead !== null ? $approval->settings->defaultRead : true;
316+
$read_approval['users'] = [];
317+
$read_approval['groups'] = [];
318+
319+
$has_write = false;
320+
$write_approval = [];
321+
$write_approval['all'] = false;
322+
$write_approval['users'] = [];
323+
$write_approval['groups'] = [];
324+
325+
foreach ($approval->users as $user => $permission) {
326+
if ($permission === 'read') {
327+
array_push($read_approval['users'], $user);
328+
$has_read = true;
329+
}
330+
if ($permission === 'write') {
331+
array_push($write_approval['users'], $user);
332+
$has_write = true;
333+
}
334+
}
299335

300-
return $element;
336+
foreach ($approval->groups as $group => $permission) {
337+
if ($permission === 'read') {
338+
array_push($read_approval['groups'], $group);
339+
$has_read = true;
340+
}
341+
if ($permission === 'write') {
342+
array_push($write_approval['groups'], $group);
343+
$has_write = true;
344+
}
345+
}
346+
347+
if ($has_read) {
348+
$structural_element->read_approval = $read_approval;
349+
}
350+
351+
if ($has_write) {
352+
$structural_element->write_approval = $write_approval;
353+
}
354+
355+
}
356+
357+
$structural_element->store();
358+
359+
return $structural_element;
301360
}
302361
private function createContainer($structural_element_id, $user, $output)
303362
{
@@ -322,7 +381,7 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
322381
$addBlock = false;
323382
$block_type = '';
324383
$payload = [];
325-
switch($block['type']) {
384+
switch ($block['type']) {
326385
case 'AssortBlock':
327386
// we skip this block type
328387
break;
@@ -579,12 +638,12 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
579638
$addBlock = true;
580639
break;
581640
case 'InteractiveVideoBlock':
582-
$source = json_decode($block['fields']['source']);
641+
$source = json_decode($block['fields']['iav_source']);
583642
$payload = array(
584643
'assignment_id' => $block['fields']['assignment_id'],
585-
'overlays' => $block['fields']['iav_overlays'],
586-
'stops' => $block['fields']['iav_stops'],
587-
'tests' => $block['fields']['iav_tests'],
644+
'overlays' => json_decode($block['fields']['iav_overlays']),
645+
'stops' => json_decode($block['fields']['iav_stops']),
646+
'tests' => json_decode($block['fields']['iav_tests']),
588647
'file_id' => $source->file_id,
589648
'file_name' => $source->file_name,
590649
'external_source' => $source->external,
@@ -717,7 +776,7 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
717776
//skip all exotic blocks
718777
break;
719778
}
720-
if($addBlock){
779+
if ($addBlock){
721780
$new_block = Block::build([
722781
'container_id' => $container->id,
723782
'owner_id' => $user->id,
@@ -726,16 +785,16 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
726785
'position' => $container->countBlocks(),
727786
'block_type' => $block_type,
728787
'payload' => json_encode($payload),
729-
'visible' => 1,
788+
'visible' => $block['visible']
730789
]);
731790
$new_block->store();
732791
} else {
733792
$new_block = null;
734793
}
735794

736-
if($new_block && $block['type'] === 'PostBlock') {
795+
if ($new_block && $block['type'] === 'PostBlock') {
737796
$thread_id = $block['fields']['thread_id'];
738-
$posts = \Mooc\DB\Post::findBySQL('thread_id = ? AND post_id > 0', array($thread_id));
797+
$posts = \Mooc\DB\Post::findBySQL('thread_id = ? AND seminar_id = ? AND post_id > 0', array($thread_id, $cid));
739798
foreach($posts as $post) {
740799
$block_comment = \Courseware\BlockComment::build([
741800
'block_id' => $new_block->id,
@@ -748,9 +807,9 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
748807
}
749808
}
750809

751-
if($new_block && $block['type'] === 'AudioGalleryBlock') {
810+
if ($new_block && $block['type'] === 'AudioGalleryBlock') {
752811
$recordings = \Mooc\DB\Field::findBySQL('block_id = ? AND name = ?', array($block['id'], "audio_gallery_user_recording"));
753-
foreach($recordings as $record) {
812+
foreach ($recordings as $record) {
754813
$data = json_decode($record['json_data']);
755814
$recording_user = \User::find($data->user_id);
756815
$file_ref = \FileRef::find($data->file_ref_id);
@@ -760,7 +819,7 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
760819
$audioGalleryFolder,
761820
$recording_user
762821
);
763-
if(!is_object($record_file)) {
822+
if (!is_object($record_file)) {
764823
continue;
765824
}
766825
$record_file_ref = $record_file->getFileRef();
@@ -770,18 +829,18 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
770829

771830
}
772831
}
773-
if($new_block && $block['type'] === 'CanvasBlock') {
832+
if ($new_block && $block['type'] === 'CanvasBlock') {
774833
$drawings = \Mooc\DB\Field::findBySQL('block_id = ? AND name = ?', array($block['id'], "canvas_draw"));
775-
foreach($drawings as $draw) {
834+
foreach ($drawings as $draw) {
776835
$data = json_decode($draw['json_data']);
777836
$canvas_draw = json_decode($data);
778837
$clickX = json_decode($canvas_draw->clickX);
779-
foreach($clickX as &$cx) {
838+
foreach ($clickX as &$cx) {
780839
$cx = intval($cx * 1.26);
781840
}
782841
$canvas_draw->clickX = json_encode($clickX);
783842
$clickY = json_decode($canvas_draw->clickY);
784-
foreach($clickY as &$cy) {
843+
foreach ($clickY as &$cy) {
785844
$cy = intval($cy * 1.26);
786845
}
787846
$canvas_draw->clickY = json_encode($clickY);
@@ -798,7 +857,7 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
798857
}
799858

800859
$link_target = [];
801-
if($new_block && $block['type'] === 'LinkBlock') {
860+
if ($new_block && $block['type'] === 'LinkBlock') {
802861
$target = $block['fields']['link_target'];
803862
$id = null;
804863

@@ -852,6 +911,7 @@ private function createBlock($block, $container, $user, $cid, $courseware, $outp
852911
private function convertCoursewareDate($date)
853912
{
854913
$new_date = date_create_from_format('Y-m-d H:i:s', $date);
914+
855915
return date_timestamp_get($new_date);
856916
}
857917
}

0 commit comments

Comments
 (0)