Passed
Pull Request — master (#6835)
by Angel Fernando Quiroz
17:36 queued 08:49
created

Version20250927180003::up()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 49
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 49
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
38
        foreach ($itemsRows as $itemRow) {
39
            $lastUserId = $itemRow['lastedit_user_id'];
40
            $courseId = $itemRow['c_id'] ?: null;
41
            $sessionId = $itemRow['session_id'] ?: null;
42
            $date = $itemRow['lastedit_date'] ?: $itemRow['insert_date'];
43
44
            $eventType = match ($itemRow['lastedit_type']) {
45
                'PortfolioCommentUpdated' => 'portfolio_comment_updated',
46
                'PortfolioUpdated' => 'portfolio_updated',
47
                default => null,
48
            };
49
50
            $valueType = match ($itemRow['lastedit_type']) {
51
                'PortfolioCommentUpdated' => 'portfolio_comment_id',
52
                'PortfolioUpdated' => 'portfolio_id',
53
                default => null,
54
            };
55
56
            if (!$eventType || !$valueType) {
57
                continue;
58
            }
59
60
            $trackEvent = new TrackEDefault();
61
            $trackEvent
62
                ->setDefaultUserId($lastUserId)
63
                ->setCId($courseId)
64
                ->setSessionId($sessionId)
65
                ->setDefaultDate(new DateTime($date, new DateTimeZone('UTC')))
66
                ->setDefaultEventType($eventType)
67
                ->setDefaultValueType($valueType)
68
                ->setDefaultValue((string) $itemRow['ref'])
69
            ;
70
71
            $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

71
            $this->entityManager->/** @scrutinizer ignore-call */ 
72
                                  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...
72
        }
73
74
        $this->entityManager->flush();
75
    }
76
}
77