From cbd59c727b375fa698a22648bf10fc5081ff71ad Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Fri, 8 Nov 2019 20:14:41 -0500 Subject: [PATCH] MigrationMoodle: Add task for lp items - refs BT#15992 --- plugin/migrationmoodle/admin.php | 6 ++ .../src/Loader/LpItemsLoader.php | 40 ++++++++++ .../migrationmoodle/src/Task/LpItemsTask.php | 77 +++++++++++++++++++ .../Property/LoadedCourseFromLessonLookup.php | 42 ++++++++++ .../Property/LoadedLpDirFromLessonLookup.php | 55 +++++++++++++ .../Property/LoadedLpFromLessonLookup.php | 57 ++++++++++++++ .../Property/LoadedLpItemLookup.php | 22 ++++++ .../Transformer/Property/LpItemTypeLookup.php | 38 +++++++++ 8 files changed, 337 insertions(+) create mode 100644 plugin/migrationmoodle/src/Loader/LpItemsLoader.php create mode 100644 plugin/migrationmoodle/src/Task/LpItemsTask.php create mode 100644 plugin/migrationmoodle/src/Transformer/Property/LoadedCourseFromLessonLookup.php create mode 100644 plugin/migrationmoodle/src/Transformer/Property/LoadedLpDirFromLessonLookup.php create mode 100644 plugin/migrationmoodle/src/Transformer/Property/LoadedLpFromLessonLookup.php create mode 100644 plugin/migrationmoodle/src/Transformer/Property/LoadedLpItemLookup.php create mode 100644 plugin/migrationmoodle/src/Transformer/Property/LpItemTypeLookup.php diff --git a/plugin/migrationmoodle/admin.php b/plugin/migrationmoodle/admin.php index 4de64a89f6..93f7a4d9f6 100644 --- a/plugin/migrationmoodle/admin.php +++ b/plugin/migrationmoodle/admin.php @@ -8,6 +8,7 @@ use Chamilo\PluginBundle\MigrationMoodle\Task\CourseUsersTask; use Chamilo\PluginBundle\MigrationMoodle\Task\CQuizTask; use Chamilo\PluginBundle\MigrationMoodle\Task\LearningPathsTask; use Chamilo\PluginBundle\MigrationMoodle\Task\LpDirsTask; +use Chamilo\PluginBundle\MigrationMoodle\Task\LpItemsTask; use Chamilo\PluginBundle\MigrationMoodle\Task\UsersTask; require_once __DIR__.'/../../main/inc/global.inc.php'; @@ -24,6 +25,7 @@ $actionNames = [ 'quizzes' => 'Quizzes', 'learning_paths' => 'Learning Paths', 'learning_path_chatpers' => 'Learning Paths: Chapters', + 'learning_path_items' => 'Learning Paths: Items', ]; foreach ($actionNames as $actionName => $actionTitle) { @@ -59,6 +61,10 @@ if (!empty($action)) { break; case 'learning_path_chatpers': $task = new LpDirsTask(); + break; + case 'learning_path_items': + $task = new LpItemsTask(); + break; } if ($task) { diff --git a/plugin/migrationmoodle/src/Loader/LpItemsLoader.php b/plugin/migrationmoodle/src/Loader/LpItemsLoader.php new file mode 100644 index 0000000000..6bbc77c867 --- /dev/null +++ b/plugin/migrationmoodle/src/Loader/LpItemsLoader.php @@ -0,0 +1,40 @@ +add_item( + $incomingData['parent'], + $incomingData['previous'], + $incomingData['item_type'], + 0, + $incomingData['title'], + '' + ); + + return $itemId; + } +} diff --git a/plugin/migrationmoodle/src/Task/LpItemsTask.php b/plugin/migrationmoodle/src/Task/LpItemsTask.php new file mode 100644 index 0000000000..9dc9c7eb1c --- /dev/null +++ b/plugin/migrationmoodle/src/Task/LpItemsTask.php @@ -0,0 +1,77 @@ + BaseExtractor::class, + 'query' => 'SELECT id, lessonid, prevpageid, qtype, title + FROM mdl_lesson_pages + WHERE qtype NOT IN (21, 31)', + ]; + } + + /** + * @return array + */ + public function getTransformConfiguration() + { + return [ + 'class' => BaseTransformer::class, + 'map' => [ + 'c_code' => [ + 'class' => LoadedCourseFromLessonLookup::class, + 'properties' => ['lessonid'], + ], + 'lp_id' => [ + 'class' => LoadedLpFromLessonLookup::class, + 'properties' => ['lessonid'] + ], + 'parent' => [ + 'class' => LoadedLpDirFromLessonLookup::class, + 'properties' => ['lessonid'] + ], + 'previous' => [ + 'class' => LoadedLpItemLookup::class, + 'properties' => ['prevpageid'], + ], + 'item_type' => [ + 'class' => LpItemTypeLookup::class, + 'properties' => ['qtype'], + ], + 'title' => 'title', + ], + ]; + } + + /** + * @return array + */ + public function getLoadConfiguration() + { + return [ + 'class' => LpItemsLoader::class, + ]; + } +} diff --git a/plugin/migrationmoodle/src/Transformer/Property/LoadedCourseFromLessonLookup.php b/plugin/migrationmoodle/src/Transformer/Property/LoadedCourseFromLessonLookup.php new file mode 100644 index 0000000000..55e88d2c80 --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/LoadedCourseFromLessonLookup.php @@ -0,0 +1,42 @@ +getConnection(); + } catch (DBALException $e) { + throw new \Exception('Unable to start connection.', 0, $e); + } + + $query = "SELECT course FROM mdl_lesson WHERE id = ?"; + + $lessonId = current($data); + + try { + $statement = $connection->executeQuery($query, [$lessonId]); + } catch (DBALException $e) { + throw new \Exception("Unable to execute query \"$query\".", 0, $e); + } + + $result = $statement->fetch(FetchMode::ASSOCIATIVE); + + $connection->close(); + + $courseId = $result['course']; + + return parent::transform([$courseId]); + } +} diff --git a/plugin/migrationmoodle/src/Transformer/Property/LoadedLpDirFromLessonLookup.php b/plugin/migrationmoodle/src/Transformer/Property/LoadedLpDirFromLessonLookup.php new file mode 100644 index 0000000000..d38c8633a4 --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/LoadedLpDirFromLessonLookup.php @@ -0,0 +1,55 @@ +calledClass = LpDirsTask::class; + } + + public function transform(array $data) + { + try { + $connection = \MigrationMoodlePlugin::create()->getConnection(); + } catch (DBALException $e) { + throw new \Exception('Unable to start connection.', 0, $e); + } + + $query = "SELECT cm.id FROM mdl_course_modules cm + INNER JOIN mdl_modules m ON cm.module = m.id + INNER JOIN mdl_lesson l ON (cm.course = l.course AND cm.instance = l.id) + WHERE m.name = 'lesson' + AND l.id = ?"; + + $lessonId = current($data); + + try { + $statement = $connection->executeQuery($query, [$lessonId]); + } catch (DBALException $e) { + throw new \Exception("Unable to execute query \"$query\".", 0, $e); + } + + $result = $statement->fetch(FetchMode::ASSOCIATIVE); + + $connection->close(); + + $lessonId = $result['id']; + + return parent::transform([$lessonId]); + } +} diff --git a/plugin/migrationmoodle/src/Transformer/Property/LoadedLpFromLessonLookup.php b/plugin/migrationmoodle/src/Transformer/Property/LoadedLpFromLessonLookup.php new file mode 100644 index 0000000000..0a17dd9f7e --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/LoadedLpFromLessonLookup.php @@ -0,0 +1,57 @@ +getConnection(); + } catch (DBALException $e) { + throw new \Exception('Unable to start connection.', 0, $e); + } + + $query = "SELECT cs.id FROM mdl_course_sections cs + INNER JOIN mdl_course_modules cm ON (cs.id = cm.section AND cs.course = cm.course) + INNER JOIN mdl_modules m ON cm.module = m.id + INNER JOIN mdl_lesson l ON (cm.course = l.course AND cm.instance = l.id) + WHERE m.name = 'lesson' + AND l.id = ?"; + + $lessonId = current($data); + + try { + $statement = $connection->executeQuery($query, [$lessonId]); + } catch (DBALException $e) { + throw new \Exception("Unable to execute query \"$query\".", 0, $e); + } + + $result = $statement->fetch(FetchMode::ASSOCIATIVE); + + $connection->close(); + + $sectionId = $result['id']; + + return parent::transform([$sectionId]); + } +} diff --git a/plugin/migrationmoodle/src/Transformer/Property/LoadedLpItemLookup.php b/plugin/migrationmoodle/src/Transformer/Property/LoadedLpItemLookup.php new file mode 100644 index 0000000000..c90b5cde6e --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/LoadedLpItemLookup.php @@ -0,0 +1,22 @@ +calledClass = LpItemsTask::class; + } +} diff --git a/plugin/migrationmoodle/src/Transformer/Property/LpItemTypeLookup.php b/plugin/migrationmoodle/src/Transformer/Property/LpItemTypeLookup.php new file mode 100644 index 0000000000..bb42cc19d8 --- /dev/null +++ b/plugin/migrationmoodle/src/Transformer/Property/LpItemTypeLookup.php @@ -0,0 +1,38 @@ +