Skip to content

Commit

Permalink
Improved importer lesson description
Browse files Browse the repository at this point in the history
  • Loading branch information
ptsavdar committed Mar 12, 2015
1 parent 0c31d09 commit 70e791e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
21 changes: 9 additions & 12 deletions Manager/LessonManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@ public function importLesson(array $data, $rootPath)
$lessonData = $data['data'];

$chaptersMap = array();
foreach ($lessonData as $chapter) {
$chapterData = $chapter['chapter'];
foreach ($lessonData['chapters'] as $chapter) {
$entityChapter = new Chapter();
$entityChapter->setLesson($lesson);
$entityChapter->setTitle($chapterData['title']);
$entityChapter->setTitle($chapter['title']);
$text = file_get_contents(
$rootPath . DIRECTORY_SEPARATOR . $chapterData['path']
$rootPath . DIRECTORY_SEPARATOR . $chapter['path']
);
$entityChapter->setText($text);
if ($chapterData['is_root']) {
if ($chapter['is_root']) {
$lesson->setRoot($entityChapter);
}
$parentChapter = null;
if ($chapterData['parent_id'] !== null) {
$parentChapter = $chaptersMap[$chapterData['parent_id']];
if ($chapter['parent_id'] !== null) {
$parentChapter = $chaptersMap[$chapter['parent_id']];
$entityChapter->setParent($parentChapter);
$this->chapterRepository->persistAsLastChildOf($entityChapter, $parentChapter);
} else {
$this->chapterRepository->persistAsFirstChild($entityChapter);
}
$chaptersMap[$chapterData['id']] = $entityChapter;
$chaptersMap[$chapter['id']] = $entityChapter;
}
}

Expand All @@ -93,7 +92,7 @@ public function importLesson(array $data, $rootPath)
*/
public function exportLesson(Workspace $workspace, array &$files, Lesson $object)
{
$data = array();
$data = array('chapters' => array());

// Getting all sections and building array
$rootChapter = $object->getRoot();
Expand All @@ -113,9 +112,7 @@ public function exportLesson(Workspace $workspace, array &$files, Lesson $object
'path' => $uid
);

$data[] = array(
'chapter' => $chapterArray
);
$data['chapters'][] = $chapterArray;
}

return $data;
Expand Down
18 changes: 10 additions & 8 deletions Transfert/LessonImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ public function addLessonDescription($rootNode)
$rootPath = $this->getRootPath();
$rootNode
->children()
->arrayNode('chapter')
->children()
->scalarNode('id')->end()
->scalarNode('parent_id')->end()
->booleanNode('is_root')->defaultFalse()->end()
->scalarNode('title')->end()
->scalarNode('path')->end()
->arrayNode('chapters')
->prototype('array')
->children()
->scalarNode('id')->end()
->scalarNode('parent_id')->end()
->booleanNode('is_root')->defaultFalse()->end()
->scalarNode('title')->end()
->scalarNode('path')->end()
->end()
->end()
->end()
->end()
Expand All @@ -76,7 +78,7 @@ public function addLessonDescription($rootNode)
public function validate(array $data)
{
$processor = new Processor();
$result = $processor->processConfiguration($this, $data);
$result = $processor->processConfiguration($this, ['data' => $data]);
}

public function import(array $data)
Expand Down

0 comments on commit 70e791e

Please sign in to comment.