Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
08:13
created

Version20250927180003::up()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 33
c 4
b 0
f 0
nc 3
nop 1
dl 0
loc 48
rs 8.4586
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\TrackEDefault;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use DateTime;
12
use DateTimeZone;
13
use Doctrine\DBAL\Schema\Schema;
14
use Exception;
15
16
class Version20250927180003 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Save c_item_property.lastedit_user_id as track_e_default for portfolio items and comments';
21
    }
22
23
    /**
24
     * @throws Exception
25
     */
26
    public function up(Schema $schema): void
27
    {
28
        $itemsRows = $this->connection
29
            ->executeQuery(
30
                "SELECT * FROM c_item_property
31
                    WHERE tool IN ('portfolio', 'portfolio_comment')
32
                        AND (to_user_id IS NULL OR to_user_id = 0)
33
                        AND lastedit_type IN ('PortfolioUpdated', 'PortfolioCommentUpdated')"
34
            )
35
            ->fetchAllAssociative();
36
37
        foreach ($itemsRows as $itemRow) {
38
            $lastUserId = $itemRow['lastedit_user_id'];
39
            $courseId = $itemRow['c_id'] ?: null;
40
            $sessionId = $itemRow['session_id'] ?: null;
41
            $date = $itemRow['lastedit_date'] ?: $itemRow['insert_date'];
42
43
            $eventType = match ($itemRow['lastedit_type']) {
44
                'PortfolioCommentUpdated' => 'portfolio_comment_updated',
45
                'PortfolioUpdated' => 'portfolio_updated',
46
                default => null,
47
            };
48
49
            $valueType = match ($itemRow['lastedit_type']) {
50
                'PortfolioCommentUpdated' => 'portfolio_comment_id',
51
                'PortfolioUpdated' => 'portfolio_id',
52
                default => null,
53
            };
54
55
            if (!$eventType || !$valueType) {
56
                continue;
57
            }
58
59
            $trackEvent = new TrackEDefault();
60
            $trackEvent
61
                ->setDefaultUserId($lastUserId)
62
                ->setCId($courseId)
63
                ->setSessionId($sessionId)
64
                ->setDefaultDate(new DateTime($date, new DateTimeZone('UTC')))
65
                ->setDefaultEventType($eventType)
66
                ->setDefaultValueType($valueType)
67
                ->setDefaultValue((string) $itemRow['ref'])
68
            ;
69
70
            $this->entityManager->persist($trackEvent);
0 ignored issues
show
Bug introduced by
The method persist() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $this->entityManager->/** @scrutinizer ignore-call */ 
71
                                  persist($trackEvent);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
        }
72
73
        $this->entityManager->flush();
74
    }
75
}
76