Passed
Pull Request — 1.11.x (#4900)
by Angel Fernando Quiroz
08:35
created

ExerciseMonitoringPlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 1
nop 0
nc 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\PluginBundle\ExerciseMonitoring\Entity\Log;
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\ORM\Tools\ToolsException;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class ExerciseMonitoringPlugin extends Plugin
11
{
12
    public const SETTING_TOOL_ENABLE = 'tool_enable';
13
14
    public const FIELD_SELECTED = 'exercisemonitoring_selected';
15
16
    private const TABLE_LOG = 'plugin_exercisemonitoring_log';
17
18
    protected function __construct()
19
    {
20
        $version = '0.0.1';
21
22
        $settings = [
23
            self::SETTING_TOOL_ENABLE => 'boolean',
24
        ];
25
26
        parent::__construct(
27
            $version,
28
            "Angel Fernando Quiroz Campos <[email protected]>",
29
            $settings
30
        );
31
    }
32
33
    public static function create(): self
34
    {
35
        static $result = null;
36
37
        return $result ?: $result = new self();
38
    }
39
40
    /**
41
     * @throws ToolsException
42
     */
43
    public function install()
44
    {
45
        $em = Database::getManager();
46
47
        if ($em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
48
            return;
49
        }
50
51
        $schemaTool = new SchemaTool($em);
52
        $schemaTool->createSchema(
53
            [
54
                $em->getClassMetadata(Log::class),
55
            ]
56
        );
57
58
        $pluginDirName = api_get_path(SYS_UPLOAD_PATH).'plugins/exercisemonitoring';
59
60
        $fs = new Filesystem();
61
        $fs->mkdir(
62
            $pluginDirName,
63
            api_get_permissions_for_new_directories()
64
        );
65
66
        $objField = new ExtraField('exercise');
67
        $objField->save([
68
            'variable' => self::FIELD_SELECTED,
69
            'field_type' => ExtraField::FIELD_TYPE_CHECKBOX,
70
            'display_text' => $this->get_title(),
71
            'visible_to_self' => true,
72
            'changeable' => true,
73
            'filter' => false,
74
        ]);
75
    }
76
77
    public function uninstall()
78
    {
79
        $em = Database::getManager();
80
81
        if (!$em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
82
            return;
83
        }
84
85
        $schemaTool = new SchemaTool($em);
86
        $schemaTool->dropSchema(
87
            [
88
                $em->getClassMetadata(Log::class),
89
            ]
90
        );
91
92
        $objField = new ExtraField('exercise');
93
        $extraFieldInfo = $objField->get_handler_field_info_by_field_variable(self::FIELD_SELECTED);
94
95
        if ($extraFieldInfo) {
96
            $objField->delete($extraFieldInfo['id']);
97
        }
98
    }
99
100
    public function getAdminUrl(): string
101
    {
102
        $name = $this->get_name();
103
        $webPath = api_get_path(WEB_PLUGIN_PATH).$name;
104
105
        return "$webPath/admin.php";
106
    }
107
}
108