From 054d7ce7bfdf08e5373f31be8f863f60c24f60ae Mon Sep 17 00:00:00 2001 From: Julio Montoya Date: Fri, 10 Apr 2015 10:31:30 +0200 Subject: [PATCH] Use abstract so we can call entities. --- main/install/index.php | 2 +- main/install/install.lib.php | 9 +- .../Migrations/AbstractMigrationChamilo.php | 28 +++ .../Migrations/Schema/v1/Version110.php | 164 +++++++++++++++++- 4 files changed, 197 insertions(+), 6 deletions(-) create mode 100644 src/Chamilo/CoreBundle/Migrations/AbstractMigrationChamilo.php diff --git a/main/install/index.php b/main/install/index.php index 6f4d36ac0f..eb922783c7 100755 --- a/main/install/index.php +++ b/main/install/index.php @@ -652,7 +652,7 @@ if (@$_POST['step2']) { Database::query("ALTER TABLE c_survey MODIFY COLUMN anonymous char(10) NOT NULL default '0'"); // Migrate using the file Version110.php - migrate('110', 1, $dbNameForm, $dbUsernameForm, $dbPassForm, $dbHostForm); + migrate('110', 1, $dbNameForm, $dbUsernameForm, $dbPassForm, $dbHostForm, $manager); include 'update-files-1.9.0-1.10.0.inc.php'; // Only updates the configuration.inc.php with the new version include 'update-configuration.inc.php'; diff --git a/main/install/install.lib.php b/main/install/install.lib.php index af8b52dbbc..f17dae2e13 100755 --- a/main/install/install.lib.php +++ b/main/install/install.lib.php @@ -2492,7 +2492,7 @@ function installSettings( Database::query($sql); } -function migrate($to, $chamiloVersion, $dbNameForm, $dbUsernameForm, $dbPassForm, $dbHostForm) +function migrate($to, $chamiloVersion, $dbNameForm, $dbUsernameForm, $dbPassForm, $dbHostForm, $manager) { $debug = true; // Config doctrine migrations @@ -2520,7 +2520,14 @@ function migrate($to, $chamiloVersion, $dbNameForm, $dbUsernameForm, $dbPassForm $config->setMigrationsDirectory(api_get_path(SYS_PATH).'src/Chamilo/CoreBundle/Migrations/Schema/v'.$chamiloVersion); // Load your migrations $config->registerMigrationsFromDirectory($config->getMigrationsDirectory()); + $migration = new \Doctrine\DBAL\Migrations\Migration($config); + $migrations = $config->getMigrations(); + + foreach ($migrations as $migration) { + $migration->setEntityManager($manager); + } + //$to = 'Version110'; // Retrieve SQL queries that should be run to migrate you schema to $to version, if $to == null - schema will be migrated to latest version $versions = $migration->getSql($to); diff --git a/src/Chamilo/CoreBundle/Migrations/AbstractMigrationChamilo.php b/src/Chamilo/CoreBundle/Migrations/AbstractMigrationChamilo.php new file mode 100644 index 0000000000..a6c2efc0b0 --- /dev/null +++ b/src/Chamilo/CoreBundle/Migrations/AbstractMigrationChamilo.php @@ -0,0 +1,28 @@ +manager = $manager; + } + + /** + * @return EntityManager + */ + public function getEntityManager() + { + return $this->manager; + } +} diff --git a/src/Chamilo/CoreBundle/Migrations/Schema/v1/Version110.php b/src/Chamilo/CoreBundle/Migrations/Schema/v1/Version110.php index d12928026d..1d70c9e06d 100644 --- a/src/Chamilo/CoreBundle/Migrations/Schema/v1/Version110.php +++ b/src/Chamilo/CoreBundle/Migrations/Schema/v1/Version110.php @@ -3,14 +3,14 @@ namespace Chamilo\CoreBundle\Migrations\Schema\v1; -use Doctrine\DBAL\Migrations\AbstractMigration, +use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo, Doctrine\DBAL\Schema\Schema; /** * Class Core * @package Chamilo\CoreBundle\Migrations\Schema\v1 */ -class Version110 extends AbstractMigration +class Version110 extends AbstractMigrationChamilo { /** * @param Schema $schema @@ -306,7 +306,6 @@ class Version110 extends AbstractMigration $this->addSql("ALTER TABLE c_quiz_question_rel_category DROP PRIMARY KEY"); $this->addSql("ALTER TABLE c_quiz_question_rel_category ADD COLUMN iid int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT"); - $this->addSql("ALTER TABLE session_rel_user MODIFY COLUMN id_session int unsigned DEFAULT NULL"); $this->addSql("ALTER TABLE session_rel_user MODIFY COLUMN id_user int unsigned DEFAULT NULL"); @@ -324,7 +323,6 @@ class Version110 extends AbstractMigration $table = $schema->getTable('c_item_property'); $table->renameColumn('id_session', 'session_id'); - $this->addSql("ALTER TABLE session_rel_user DROP PRIMARY KEY"); $this->addSql("ALTER TABLE session_rel_user ADD COLUMN id int unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT"); @@ -441,6 +439,12 @@ class Version110 extends AbstractMigration $this->addSql("DELETE FROM settings_options WHERE variable = 'show_glossary_in_extra_tools'"); $schema->renameTable('track_e_exercices', 'track_e_exercises'); + + $this->addSql("UPDATE c_student_publication SET date_of_qualification = NULL WHERE date_of_qualification = '0000-00-00 00:00:00'"); + $this->addSql("UPDATE c_student_publication SET sent_date = NULL WHERE sent_date = '0000-00-00 00:00:00'"); + + $this->addSql("UPDATE c_student_publication_assignment SET expires_on = NULL WHERE expires_on = '0000-00-00 00:00:00'"); + $this->addSql("UPDATE c_student_publication_assignment SET ends_on = NULL WHERE ends_on = '0000-00-00 00:00:00'"); } public function postUp(Schema $schema) @@ -462,6 +466,158 @@ class Version110 extends AbstractMigration $this->addSql("DROP TABLE track_c_providers"); $this->addSql("DROP TABLE track_c_referers"); + // Fix ids + /* + // Fix c_lp_item + $connection = $this->connection; + + $sql = "SELECT * FROM c_lp_item"; + $result = $connection->fetchAll($sql); + foreach ($result as $item) { + $courseId = $item['c_id']; + $iid = $item['iid']; + $ref = $item['ref']; + $sql = null; + + switch ($item['item_type']) { + case TOOL_LINK: + $sql = "SELECT * c_link WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_STUDENTPUBLICATION: + $sql = "SELECT * c_student_publication WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_QUIZ: + $sql = "SELECT * c_quiz WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_DOCUMENT: + $sql = "SELECT * c_document WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_FORUM: + $sql = "SELECT * c_forum_forum WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case 'thread': + $sql = "SELECT * c_forum_thread WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + } + + if (!empty($sql)) { + $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid"; + $connection->executeQuery($sql); + } + } + + // Fix c_item_property + + $sql = "SELECT * FROM c_item_property"; + $result = $connection->fetchAll($sql); + foreach ($result as $item) { + $courseId = $item['c_id']; + $sessionId = intval($item['session_id']); + $iid = $item['iid']; + $ref = $item['ref']; + $sql = null; + + switch ($item['tool']) { + case TOOL_LINK: + $sql = "SELECT * c_link WHERE c_id = $courseId AND id = $ref "; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_STUDENTPUBLICATION: + $sql = "SELECT * c_student_publication WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_QUIZ: + $sql = "SELECT * c_quiz WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_DOCUMENT: + $sql = "SELECT * c_document WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_FORUM: + $sql = "SELECT * c_forum_forum WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case 'thread': + $sql = "SELECT * c_forum_thread WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + } + + if (!empty($sql)) { + $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid"; + $connection->executeQuery($sql); + } + } + + // Fix gradebook_link + $sql = "SELECT * FROM gradebook_link"; + $result = $connection->fetchAll($sql); + foreach ($result as $item) { + $courseId = $item['c_id']; + $ref = $item['ref_id']; + $sql = null; + + switch ($item['tool']) { + case TOOL_LINK: + $sql = "SELECT * c_link WHERE c_id = $courseId AND id = $ref "; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_STUDENTPUBLICATION: + $sql = "SELECT * c_student_publication WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_QUIZ: + $sql = "SELECT * c_quiz WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_DOCUMENT: + $sql = "SELECT * c_document WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case TOOL_FORUM: + $sql = "SELECT * c_forum_forum WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + case 'thread': + $sql = "SELECT * c_forum_thread WHERE c_id = $courseId AND id = $ref"; + $data = $connection->fetchArray($sql); + $newId = $data['iid']; + break; + } + + if (!empty($sql)) { + $sql = "UPDATE c_item_property SET ref_id = $newId WHERE iid = $iid"; + $connection->executeQuery($sql); + } + } + + + */ + //$this->addSql('ALTER TABLE user DROP COLUMN user_id'); //$this->addSql("UPDATE settings_current SET selected_value = '1.10.0.35' WHERE variable = 'chamilo_database_version'");