parent
be66580f66
commit
c261965871
@ -0,0 +1,40 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\PluginBundle\MigrationMoodle\Loader; |
||||
|
||||
use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface; |
||||
|
||||
/** |
||||
* Class QuestionCategoriesLoader. |
||||
* |
||||
* Loader for create a category for Chamilo quiz questions coming from a Moodle question category. |
||||
* |
||||
* @package Chamilo\PluginBundle\MigrationMoodle\Loader |
||||
*/ |
||||
class QuestionCategoriesLoader implements LoaderInterface |
||||
{ |
||||
/** |
||||
* Load the data and return the ID inserted. |
||||
* |
||||
* @param array $incomingData |
||||
* |
||||
* @throws \Exception |
||||
* |
||||
* @return int |
||||
*/ |
||||
public function load(array $incomingData) |
||||
{ |
||||
$category = new \TestCategory(); |
||||
$category->name = $incomingData['name']; |
||||
$category->description = $incomingData['description']; |
||||
|
||||
$id = $category->save($incomingData['c_id']); |
||||
|
||||
if (false === $id) { |
||||
throw new \Exception("The quiz category \"{$incomingData['name']}\" already exists."); |
||||
} |
||||
|
||||
return $id; |
||||
} |
||||
} |
||||
@ -0,0 +1,62 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\PluginBundle\MigrationMoodle\Task; |
||||
|
||||
use Chamilo\PluginBundle\MigrationMoodle\Extractor\BaseExtractor; |
||||
use Chamilo\PluginBundle\MigrationMoodle\Loader\QuestionCategoriesLoader; |
||||
use Chamilo\PluginBundle\MigrationMoodle\Transformer\BaseTransformer; |
||||
use Chamilo\PluginBundle\MigrationMoodle\Transformer\Property\LoadedCourseFromQuestionCategoryLookup; |
||||
|
||||
/** |
||||
* Class QuestionCategoriesTask. |
||||
* |
||||
* Task for create categories for Chamilo quiz questions coming from a Moodle questions categories. |
||||
* |
||||
* @package Chamilo\PluginBundle\MigrationMoodle\Task |
||||
*/ |
||||
class QuestionCategoriesTask extends BaseTask |
||||
{ |
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getExtractConfiguration() |
||||
{ |
||||
return [ |
||||
'class' => BaseExtractor::class, |
||||
'query' => "SELECT qc.id, qc.name, qc.info, c.contextlevel, c.instanceid |
||||
FROM mdl_question_categories qc |
||||
INNER JOIN mdl_context c ON qc.contextid = c.id |
||||
WHERE c.contextlevel IN (50, 70) |
||||
AND qc.parent != 0", |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getTransformConfiguration() |
||||
{ |
||||
return [ |
||||
'class' => BaseTransformer::class, |
||||
'map' => [ |
||||
'c_id' => [ |
||||
'class' => LoadedCourseFromQuestionCategoryLookup::class, |
||||
'properties' => ['contextlevel', 'instanceid'] |
||||
], |
||||
'name' => 'name', |
||||
'description' => 'info', |
||||
], |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getLoadConfiguration() |
||||
{ |
||||
return [ |
||||
'class' => QuestionCategoriesLoader::class, |
||||
]; |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\PluginBundle\MigrationMoodle\Transformer\Property; |
||||
|
||||
use Doctrine\DBAL\DBALException; |
||||
use Doctrine\DBAL\FetchMode; |
||||
|
||||
/** |
||||
* Class LoadedCourseFromQuestionCategoryLookup. |
||||
* |
||||
* Lookup for a course ID loaded based on the context from a Moodle question category. |
||||
* |
||||
* @package Chamilo\PluginBundle\MigrationMoodle\Transformer\Property |
||||
*/ |
||||
class LoadedCourseFromQuestionCategoryLookup extends LoadedCourseLookup |
||||
{ |
||||
/** |
||||
* @param array $data |
||||
* |
||||
* @throws \Exception |
||||
* |
||||
* @return mixed |
||||
*/ |
||||
public function transform(array $data) |
||||
{ |
||||
$instanceId = $data['instanceid']; |
||||
$contextLevel = $data['contextlevel']; |
||||
|
||||
$mCourseId = 0; |
||||
|
||||
switch ($contextLevel) { |
||||
case 50: // course |
||||
$mCourseId = $instanceId; |
||||
break; |
||||
case 70: // module |
||||
$mCourseId = $this->searchInModuleContext($instanceId); |
||||
break; |
||||
} |
||||
|
||||
if (empty($mCourseId)) { |
||||
throw new \Exception("Course not found for context $instanceId with level $contextLevel"); |
||||
} |
||||
|
||||
return parent::transform([$mCourseId]); |
||||
} |
||||
|
||||
/** |
||||
* @param int $instanceId |
||||
* |
||||
* @throws \Exception |
||||
* |
||||
* @return int |
||||
*/ |
||||
private function searchInModuleContext($instanceId) |
||||
{ |
||||
try { |
||||
$connection = \MigrationMoodlePlugin::create()->getConnection(); |
||||
} catch (DBALException $e) { |
||||
throw new \Exception('Unable to start connection.', 0, $e); |
||||
} |
||||
|
||||
$query = "SELECT course FROM mdl_course_modules WHERE id = ?"; |
||||
|
||||
try { |
||||
$statement = $connection->executeQuery($query, [$instanceId]); |
||||
} catch (DBALException $e) { |
||||
throw new \Exception("Unable to execute query \"$query\".", 0, $e); |
||||
} |
||||
|
||||
$result = $statement->fetch(FetchMode::ASSOCIATIVE); |
||||
|
||||
$connection->close(); |
||||
|
||||
if (false === $result) { |
||||
return 0; |
||||
} |
||||
|
||||
return (int) $result['course']; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue