Completed
Push — 1.10.x ( 2635b5...f0080b )
by Yannick
165:55 queued 120:08
created

Version20151214170800::up()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 98
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 61
nc 7
nop 1
dl 0
loc 98
rs 5.2653
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Application\Migrations\Schema\V110;
5
6
use Application\Migrations\AbstractMigrationChamilo;
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\DBAL\Types\Type;
9
10
/**
11
 * Fix track_e_hotspot table and migrate hotspot/values
12
 * In the wake of an issue with the hotspot code not being possible to update without moving from AS2 to AS3 (Flash), we
13
 * have decided to rewrite the hotspot tool in JS.
14
 * Little did we know that we would find out that the coordinates system in use by the Flash version of hotspot was in
15
 * fact a projection into a square form of 360x360, not depending on the original size of the image, and that a square
16
 * was defined by its center's coordinates and its width, with an initial not-null margin on the left and top borders.
17
 * The migration below fixes those parallax issues.
18
 */
19
class Version20151214170800 extends AbstractMigrationChamilo
20
{
21
    /**
22
     * @param Schema $schema
23
     */
24
    public function up(Schema $schema)
25
    {
26
        $this->addSql("ALTER TABLE track_e_hotspot ADD c_id INT NULL");
27
        $this->addSql("ALTER TABLE track_e_hotspot MODIFY COLUMN hotspot_coordinate LONGTEXT NOT NULL");
28
        $this->addSql("UPDATE track_e_hotspot SET c_id = (SELECT id FROM course WHERE code = hotspot_course_code)");
29
30
        $answers = $this->connection->fetchAll("
31
            SELECT a.iid, a.c_id, a.question_id, a.hotspot_coordinates, a.hotspot_type, q.picture, c.directory
32
            FROM c_quiz_answer a
33
            INNER JOIN c_quiz_question q
34
                ON a.question_id = q.id
35
            INNER JOIN course c
36
                ON (a.c_id = c.id AND q.c_id = c.id)
37
            WHERE a.hotspot_type IN ('square', 'circle', 'poly', 'delineation', 'oar')
38
        ");
39
40
        foreach ($answers as $answer) {
41
            // Recover the real image size to recalculate coordinates
42
            $imagePath = api_get_path(SYS_PATH) . "courses/{$answer['directory']}/document/images/{$answer['picture']}";
43
            $imageSize = getimagesize($imagePath);
44
            $widthRatio = $imageSize[0] / 360;
45
            $heightRatio = $imageSize[1] / 360;
46
            $oldCoords = $answer['hotspot_coordinates'];
47
            $oldPairedString = explode('|', $oldCoords);
48
            $newPairedString = [];
49
50
            switch ($answer['hotspot_type']) {
51
                case 'square':
52
                    $oldCenter = explode(';', $oldPairedString[0]);
53
54
                    $oldCenterX = intval($oldCenter[0]);
55
                    $oldCenterY = intval($oldCenter[1]);
56
                    $oldWidth = intval($oldPairedString[1]);
57
                    $oldHeight = intval($oldPairedString[2]);
58
59
                    $newX = floor(($oldCenterX - $oldWidth / 2) * $widthRatio) + ceil($widthRatio);
60
                    $newY = floor(($oldCenterY - $oldHeight / 2) * $heightRatio) + ceil($heightRatio);
61
                    $newWidth = ceil($oldWidth * $widthRatio) + ceil(1 * $heightRatio);
62
                    $newHeight = ceil($oldHeight * $heightRatio) + floor(1 * $heightRatio);
63
64
                    $newPairedString[] = implode(';', [$newX, $newY]);
65
                    $newPairedString[] = $newWidth;
66
                    $newPairedString[] = $newHeight;
67
                    break;
68
69
                case 'circle':
70
                    $oldCenter = explode(';', $oldPairedString[0]);
71
72
                    $oldCenterX = intval($oldCenter[0]);
73
                    $oldCenterY = intval($oldCenter[1]);
74
                    $oldRadiusX = intval($oldPairedString[1]) / 2;
75
                    $oldRadiusY = intval($oldPairedString[2]) / 2;
76
77
                    $newCenterX = floor($oldCenterX * $widthRatio) + ceil($widthRatio);
78
                    $newCenterY = floor($oldCenterY * $heightRatio) + ceil($heightRatio);
79
                    $newRadiusX = floor($oldRadiusX * $widthRatio);
80
                    $newRadiusY = floor($oldRadiusY * $heightRatio);
81
82
                    $newPairedString[] = implode(';', [$newCenterX, $newCenterY]);
83
                    $newPairedString[] = $newRadiusX;
84
                    $newPairedString[] = $newRadiusY;
85
                    break;
86
87
                case 'poly':
88
                    //no break;
89
                case 'delineation':
90
                    //no break
91
                case 'oar':
92
                    $paired = [];
93
94
                    foreach ($oldPairedString as $pairString) {
95
                        $pair = explode(';', $pairString);
96
                        $x = isset($pair[0]) ? intval($pair[0]) : 0;
97
                        $y = isset($pair[1]) ? intval($pair[1]) : 0;
98
99
                        $paired[] = [$x, $y];
100
                    }
101
102
                    foreach ($paired as $pair) {
103
                        $x = floor($pair[0] * $widthRatio) + ceil($widthRatio);
104
                        $y = ceil($pair[1] * $heightRatio);
105
106
                        $newPairedString[] = implode(';', [$x, $y]);
107
                    }
108
                    break;
109
            }
110
111
            $stmt = $this->connection->prepare("
112
                UPDATE c_quiz_answer
113
                SET hotspot_coordinates = :coordinates
114
                WHERE iid = :iid AND c_id = :cid
115
            ");
116
            $stmt->bindValue('coordinates', implode('|', $newPairedString), Type::TEXT);
117
            $stmt->bindValue('iid', $answer['iid'], Type::INTEGER);
118
            $stmt->bindValue('cid', $answer['c_id'], Type::INTEGER);
119
            $stmt->execute();
120
        }
121
    }
122
123
    /**
124
     * @param Schema $schema
125
     */
126
    public function down(Schema $schema)
127
    {
128
    }
129
}
130