Passed
Push — master ( dc373e...baaca7 )
by Julito
11:31
created

ImsLtiPlugin::uninstall()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 6
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* For license terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CTool;
5
use Chamilo\CoreBundle\Entity\Course;
6
use Doctrine\DBAL\Schema\Schema;
7
use Doctrine\DBAL\Types\Type;
8
use Doctrine\DBAL\DBALException;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
11
12
/**
13
 * Description of MsiLti
14
 *
15
 * @author Angel Fernando Quiroz Campos <[email protected]>
16
 */
17
class ImsLtiPlugin extends Plugin
18
{
19
    const TABLE_TOOL = 'plugin_ims_lti_tool';
20
21
    public $isAdminPlugin = true;
22
23
    /**
24
     * Class constructor
25
     */
26
    protected function __construct()
27
    {
28
        $version = '1.0 (beta)';
29
        $author = 'Angel Fernando Quiroz Campos';
30
31
        parent::__construct($version, $author, ['enabled' => 'boolean']);
32
33
        $this->setCourseSettings();
34
    }
35
36
    /**
37
     * Get the class instance
38
     * @staticvar MsiLtiPlugin $result
39
     * @return ImsLtiPlugin
40
     */
41
    public static function create()
42
    {
43
        static $result = null;
44
45
        return $result ?: $result = new self();
46
    }
47
48
    /**
49
     * Get the plugin directory name
50
     */
51
    public function get_name()
52
    {
53
        return 'ims_lti';
54
    }
55
56
    /**
57
     * Install the plugin. Setup the database
58
     */
59
    public function install()
60
    {
61
        $pluginEntityPath = $this->getEntityPath();
62
63
        if (!is_dir($pluginEntityPath)) {
64
            if (!is_writable(dirname($pluginEntityPath))) {
65
                $message = get_lang('ErrorCreatingDir').': '.$pluginEntityPath;
66
                Display::addFlash(Display::return_message($message, 'error'));
67
68
                return false;
69
            }
70
71
            mkdir($pluginEntityPath, api_get_permissions_for_new_directories());
72
        }
73
74
        $fs = new Filesystem();
75
        $fs->mirror(__DIR__.'/Entity/', $pluginEntityPath, null, ['override']);
76
77
        $this->createPluginTables();
78
    }
79
80
    /**
81
     * Unistall plugin. Clear the database
82
     */
83
    public function uninstall()
84
    {
85
        $pluginEntityPath = $this->getEntityPath();
86
        $fs = new Filesystem();
87
88
        if ($fs->exists($pluginEntityPath)) {
89
            $fs->remove($pluginEntityPath);
90
        }
91
92
        try {
93
            $this->dropPluginTables();
94
            $this->removeTools();
95
        } catch (DBALException $e) {
96
            error_log('Error while uninstalling IMS/LTI plugin: '.$e->getMessage());
97
        }
98
    }
99
100
    /**
101
     * Creates the plugin tables on database
102
     * @return boolean
103
     * @throws \Doctrine\DBAL\DBALException
104
     */
105
    private function createPluginTables()
106
    {
107
        $entityManager = Database::getManager();
108
        $connection = $entityManager->getConnection();
109
        $pluginSchema = new Schema();
110
        $platform = $connection->getDatabasePlatform();
111
112
        $toolTable = $pluginSchema->createTable(self::TABLE_TOOL);
113
        $toolTable->addColumn(
114
            'id',
115
            \Doctrine\DBAL\Types\Type::INTEGER,
116
            ['autoincrement' => true, 'unsigned' => true]
117
        );
118
        $toolTable->addColumn('name', Type::STRING);
119
        $toolTable->addColumn('description', Type::TEXT)->setNotnull(false);
120
        $toolTable->addColumn('launch_url', Type::TEXT);
121
        $toolTable->addColumn('consumer_key', Type::STRING);
122
        $toolTable->addColumn('shared_secret', Type::STRING);
123
        $toolTable->addColumn('custom_params', Type::TEXT)->setNotnull(false);
124
        $toolTable->addColumn('is_global', Type::BOOLEAN);
125
        $toolTable->setPrimaryKey(['id']);
126
127
        $queries = $pluginSchema->toSql($platform);
128
129
        foreach ($queries as $query) {
130
            Database::query($query);
131
        }
132
133
        return true;
134
    }
135
136
    /**
137
     * Drops the plugin tables on database
138
     * @return boolean
139
     * @throws \Doctrine\DBAL\DBALException
140
     */
141
    private function dropPluginTables()
142
    {
143
        $entityManager = Database::getManager();
144
        $connection = $entityManager->getConnection();
145
        $chamiloSchema = $connection->getSchemaManager();
146
147
        if (!$chamiloSchema->tablesExist([self::TABLE_TOOL])) {
148
            return false;
149
        }
150
151
        $sql = 'DROP TABLE IF EXISTS '.self::TABLE_TOOL;
152
        Database::query($sql);
153
154
        return true;
155
    }
156
157
    /**
158
     * @throws \Doctrine\DBAL\DBALException
159
     */
160
    private function removeTools()
161
    {
162
        $sql = "DELETE FROM c_tool WHERE link LIKE 'ims_lti/start.php%' AND category = 'plugin'";
163
        Database::query($sql);
164
    }
165
166
    /**
167
     * Set the course settings
168
     */
169
    private function setCourseSettings()
170
    {
171
        $button = Display::toolbarButton(
172
            $this->get_lang('AddExternalTool'),
173
            api_get_path(WEB_PLUGIN_PATH).'ims_lti/add.php?'.api_get_cidreq(),
174
            'cog',
175
            'primary'
176
        );
177
178
        $this->course_settings = [
179
            [
180
                'name' => $this->get_lang('ImsLtiDescription').$button.'<hr>',
181
                'type' => 'html'
182
            ]
183
        ];
184
    }
185
186
    /**
187
     * Add the course tool
188
     * @param Course $course
189
     * @param ImsLtiTool $tool
190
     * @throws \Doctrine\ORM\OptimisticLockException
191
     */
192
    public function addCourseTool(Course $course, ImsLtiTool $tool)
193
    {
194
        $em = Database::getManager();
195
        $cTool = new CTool();
196
        $cTool
197
            ->setCId($course->getId())
198
            ->setName($tool->getName())
199
            ->setLink($this->get_name().'/start.php?'.http_build_query(['id' => $tool->getId()]))
200
            ->setImage($this->get_name().'.png')
201
            ->setVisibility(1)
202
            ->setAdmin(0)
203
            ->setAddress('squaregray.gif')
204
            ->setAddedTool('NO')
205
            ->setTarget('_self')
206
            ->setCategory('plugin')
207
            ->setSessionId(0);
208
209
        $em->persist($cTool);
210
        $em->flush();
211
212
        $cTool->setId($cTool->getIid());
213
214
        $em->persist($cTool);
215
        $em->flush();
216
    }
217
218
    /**
219
     * @return string
220
     */
221
    protected function getConfigExtraText()
222
    {
223
        $text = $this->get_lang('ImsLtiDescription');
224
        $text .= sprintf(
225
            $this->get_lang('ManageToolButton'),
226
            api_get_path(WEB_PLUGIN_PATH).'ims_lti/admin.php'
227
        );
228
229
        return $text;
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getEntityPath()
236
    {
237
        return api_get_path(SYS_PATH).'src/Chamilo/PluginBundle/Entity/'.$this->getCamelCaseName();
238
    }
239
}
240