Internal: Add duration field to multiple tables and handle migration of extra fields - refs #5633
parent
d9adeb85b1
commit
50e74ac5a9
@ -0,0 +1,42 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||||
|
|
||||||
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||||
|
use Doctrine\DBAL\Schema\Schema; |
||||||
|
|
||||||
|
final class Version20240704120400 extends AbstractMigrationChamilo |
||||||
|
{ |
||||||
|
public function getDescription(): string |
||||||
|
{ |
||||||
|
return 'Add duration field to multiple tables'; |
||||||
|
} |
||||||
|
|
||||||
|
public function up(Schema $schema): void |
||||||
|
{ |
||||||
|
$this->addSql('ALTER TABLE course ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_survey ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_quiz ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_quiz_question ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_lp ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_lp_item ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_student_publication ADD duration INT DEFAULT NULL'); |
||||||
|
$this->addSql('ALTER TABLE c_attendance_calendar ADD duration INT DEFAULT NULL'); |
||||||
|
} |
||||||
|
|
||||||
|
public function down(Schema $schema): void |
||||||
|
{ |
||||||
|
$this->addSql('ALTER TABLE course DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_survey DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_quiz DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_quiz_question DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_lp DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_lp_item DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_student_publication DROP duration'); |
||||||
|
$this->addSql('ALTER TABLE c_attendance_calendar DROP duration'); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,91 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
||||||
|
|
||||||
|
use Chamilo\CoreBundle\Entity\ExtraField; |
||||||
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
||||||
|
use Doctrine\DBAL\Schema\Schema; |
||||||
|
|
||||||
|
final class Version20240704120500 extends AbstractMigrationChamilo |
||||||
|
{ |
||||||
|
public function getDescription(): string |
||||||
|
{ |
||||||
|
return 'Migrate extra fields to duration field in multiple tables'; |
||||||
|
} |
||||||
|
|
||||||
|
public function up(Schema $schema): void |
||||||
|
{ |
||||||
|
$this->migrateStudentPublicationDuration(); |
||||||
|
$this->migrateAttendanceCalendarDuration(); |
||||||
|
} |
||||||
|
|
||||||
|
public function down(Schema $schema): void |
||||||
|
{ |
||||||
|
// Revert changes if necessary |
||||||
|
$this->addSql('UPDATE c_student_publication SET duration = NULL WHERE duration IS NOT NULL'); |
||||||
|
$this->addSql('UPDATE c_attendance_calendar SET duration = NULL WHERE duration IS NOT NULL'); |
||||||
|
} |
||||||
|
|
||||||
|
private function migrateStudentPublicationDuration(): void |
||||||
|
{ |
||||||
|
$sql = 'SELECT selected_value FROM settings_current WHERE variable = "considered_working_time" AND selected_value IS NOT NULL AND selected_value != "" AND selected_value != "false"'; |
||||||
|
$selectedValue = $this->connection->fetchOne($sql); |
||||||
|
|
||||||
|
if ($selectedValue) { |
||||||
|
$sql = 'SELECT s.*, efv.field_value |
||||||
|
FROM c_student_publication s |
||||||
|
INNER JOIN extra_field_values efv ON s.iid = efv.item_id |
||||||
|
INNER JOIN extra_field ef ON efv.field_id = ef.id |
||||||
|
WHERE ef.variable = ? AND ef.item_type = ?'; |
||||||
|
|
||||||
|
$params = [$selectedValue, ExtraField::WORK_FIELD_TYPE]; |
||||||
|
$data = $this->connection->fetchAllAssociative($sql, $params); |
||||||
|
|
||||||
|
foreach ($data as $item) { |
||||||
|
$id = $item['iid']; |
||||||
|
$workTime = (int) $item['field_value']; |
||||||
|
|
||||||
|
$durationInSeconds = $workTime * 60; |
||||||
|
|
||||||
|
$this->addSql("UPDATE c_student_publication SET duration = ? WHERE iid = ?", [$durationInSeconds, $id]); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private function migrateAttendanceCalendarDuration(): void |
||||||
|
{ |
||||||
|
$sql = 'SELECT s.*, efv.field_value |
||||||
|
FROM c_attendance_calendar s |
||||||
|
INNER JOIN extra_field_values efv ON s.iid = efv.item_id |
||||||
|
INNER JOIN extra_field ef ON efv.field_id = ef.id |
||||||
|
WHERE ef.variable = "duration" AND ef.item_type = ?'; |
||||||
|
|
||||||
|
$params = [ExtraField::ATTENDANCE_CALENDAR_TYPE]; |
||||||
|
$data = $this->connection->fetchAllAssociative($sql, $params); |
||||||
|
|
||||||
|
foreach ($data as $item) { |
||||||
|
$id = $item['iid']; |
||||||
|
$duration = $item['field_value']; |
||||||
|
|
||||||
|
$matches = []; |
||||||
|
$newDuration = null; |
||||||
|
|
||||||
|
if (preg_match('/(\d+)([h:](\d+)?)?/', $duration, $matches)) { |
||||||
|
$hours = (int)$matches[1]; |
||||||
|
$minutes = 0; |
||||||
|
if (!empty($matches[3])) { |
||||||
|
$minutes = (int)$matches[3]; |
||||||
|
} |
||||||
|
$newDuration = ($hours * 3600) + ($minutes * 60); |
||||||
|
} |
||||||
|
|
||||||
|
if ($newDuration !== null) { |
||||||
|
$this->addSql('UPDATE c_attendance_calendar SET duration = ? WHERE iid = ?', [$newDuration, $id]); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue