Passed
Pull Request — 1.11.x (#4120)
by Angel Fernando Quiroz
09:31
created

TopLinksPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For license terms, see /license.txt */
4
5
use Chamilo\PluginBundle\Entity\TopLinks\TopLink;
6
use Chamilo\PluginBundle\Entity\TopLinks\TopLinkRelTool;
7
use Doctrine\ORM\Tools\SchemaTool;
8
9
/**
10
 * Class TopLinksPlugin.
11
 */
12
class TopLinksPlugin extends Plugin implements HookPluginInterface
13
{
14
    /**
15
     * TopLinksPlugin constructor.
16
     */
17
    protected function __construct()
18
    {
19
        $settings = [
20
        ];
21
22
        parent::__construct(
23
            '0.1',
24
            'Angel Fernando Quiroz Campos <[email protected]>',
25
            $settings
26
        );
27
    }
28
29
    /**
30
     * @return \TopLinksPlugin
31
     */
32
    public static function create(): TopLinksPlugin
33
    {
34
        static $result = null;
35
36
        return $result ? $result : $result = new self();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAdminUrl()
43
    {
44
        $webPath = api_get_path(WEB_PLUGIN_PATH).$this->get_name();
45
46
        return "$webPath/admin.php";
47
    }
48
49
    public function addToolInCourse(int $courseId, TopLink $link)
50
    {
51
        $tool = $this->createLinkToCourseTool(
52
            $link->getTitle(),
53
            $courseId,
54
            'external_link.png',
55
            '../plugin/toplinks/start.php?'.http_build_query(['link' => $link->getId()]),
56
            'authoring'
57
        );
58
59
        if (null === $tool) {
60
            return;
61
        }
62
63
        $tool->setTarget($link->getTarget());
64
65
        $link->addTool($tool);
66
67
        $em = Database::getManager();
68
        $em->persist($link);
69
        $em->flush();
70
    }
71
72
    public function install()
73
    {
74
        $em = Database::getManager();
75
        $schemaManager = $em->getConnection()->getSchemaManager();
76
77
        $tableReferences = [
78
            'toplinks_link' => $em->getClassMetadata(TopLink::class),
79
            'toplinks_link_rel_tool' => $em->getClassMetadata(TopLinkRelTool::class),
80
        ];
81
82
        $tablesExists = $schemaManager->tablesExist(array_keys($tableReferences));
83
84
        if ($tablesExists) {
85
            return;
86
        }
87
88
        $schemaTool = new SchemaTool($em);
89
        $schemaTool->createSchema(array_values($tableReferences));
90
91
        $this->installHook();
92
    }
93
94
    public function installHook(): int
95
    {
96
        $createCourseObserver = TopLinksCreateCourseHookObserver::create();
97
        HookCreateCourse::create()->attach($createCourseObserver);
98
99
        return 1;
100
    }
101
102
    public function uninstall()
103
    {
104
        $em = Database::getManager();
105
106
        $tableReferences = [
107
            'toplinks_link' => $em->getClassMetadata(TopLink::class),
108
            'toplinks_link_rel_tool' => $em->getClassMetadata(TopLinkRelTool::class),
109
        ];
110
111
        $schemaTool = new SchemaTool($em);
112
        $schemaTool->dropSchema(array_values($tableReferences));
113
114
        $this->uninstallHook();
115
116
        $this->deleteCourseTools();
117
    }
118
119
    public function uninstallHook(): int
120
    {
121
        $createCourseObserver = TopLinksCreateCourseHookObserver::create();
122
        HookCreateCourse::create()->detach($createCourseObserver);
123
124
        return 1;
125
    }
126
127
    private function deleteCourseTools()
128
    {
129
        Database::getManager()
130
            ->createQuery('DELETE FROM ChamiloCourseBundle:CTool t WHERE t.category = :category AND t.link LIKE :link')
131
            ->execute(['category' => 'authoring', 'link' => '../plugin/toplinks/start.php%']);
132
    }
133
}
134