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

ExerciseFocusedPlugin::uninstall()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 20
rs 9.9332
cc 3
nc 3
nop 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CourseBundle\Entity\CTool;
6
use Chamilo\PluginBundle\ExerciseFocused\Entity\Log;
7
use Doctrine\ORM\Tools\SchemaTool;
8
use Doctrine\ORM\Tools\ToolsException;
9
10
class ExerciseFocusedPlugin extends Plugin
11
{
12
    public const SETTING_TOOL_ENABLE = 'tool_enable';
13
    public const SETTING_TIME_LIMIT = 'time_limit';
14
    public const SETTING_ENABLE_OUTFOCUSED_LIMIT = 'enable_outfocused_limit';
15
    public const SETTING_OUTFOCUSED_LIMIT = 'outfocused_limit';
16
    public const SETTING_SESSION_FIELD_FILTERS = 'session_field_filters';
17
    public const SETTING_PERCENTAGE_SAMPLING = 'percentage_sampling';
18
19
    public const FIELD_SELECTED = 'exercisefocused_selected';
20
21
    private const TABLE_LOG = 'plugin_exercisefocused_log';
22
23
    protected function __construct()
24
    {
25
        $settings = [
26
            self::SETTING_TOOL_ENABLE => 'boolean',
27
            self::SETTING_TIME_LIMIT => 'text',
28
            self::SETTING_ENABLE_OUTFOCUSED_LIMIT => 'boolean',
29
            self::SETTING_OUTFOCUSED_LIMIT => 'text',
30
            self::SETTING_SESSION_FIELD_FILTERS => 'text',
31
            self::SETTING_PERCENTAGE_SAMPLING => 'text',
32
        ];
33
34
        parent::__construct(
35
            "0.0.1",
36
            "Angel Fernando Quiroz Campos <[email protected]>",
37
            $settings
38
        );
39
    }
40
41
    public static function create(): ?ExerciseFocusedPlugin
42
    {
43
        static $result = null;
44
45
        return $result ?: $result = new self();
46
    }
47
48
    /**
49
     * @throws ToolsException
50
     */
51
    public function install()
52
    {
53
        $em = Database::getManager();
54
55
        if ($em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
56
            return;
57
        }
58
59
        $schemaTool = new SchemaTool($em);
60
        $schemaTool->createSchema(
61
            [
62
                $em->getClassMetadata(Log::class),
63
            ]
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
    public function getActionTitle($action): string
109
    {
110
        switch ($action) {
111
            case Log::TYPE_OUTFOCUSED:
112
                return $this->get_lang('Outfocused');
113
            case Log::TYPE_RETURN:
114
                return $this->get_lang('Return');
115
            case Log::TYPE_OUTFOCUSED_LIMIT:
116
                return $this->get_lang('MaxOutfocusedReached');
117
            case Log::TYPE_TIME_LIMIT:
118
                return $this->get_lang('TimeLimitReached');
119
        }
120
121
        return '';
122
    }
123
124
    public function getLinkReporting(int $exerciseId): string
125
    {
126
        if (!$this->isEnabled(true)) {
127
            return '';
128
        }
129
130
        $values = (new ExtraFieldValue('exercise'))
131
            ->get_values_by_handler_and_field_variable($exerciseId, self::FIELD_SELECTED);
132
133
        if (!$values || !$values['value']) {
134
            return '';
135
        }
136
137
        $icon = Display::return_icon(
138
            'window_list_slide.png',
139
            $this->get_lang('ReportByAttempts'),
140
            [],
141
            ICON_SIZE_MEDIUM
142
        );
143
144
        $url = api_get_path(WEB_PLUGIN_PATH)
145
            .'exercisefocused/pages/reporting.php?'
146
            .api_get_cidreq().'&'.http_build_query(['id' => $exerciseId]);
147
148
        return Display::url($icon, $url);
149
    }
150
151
    public function getSessionFieldList(): array
152
    {
153
        $settingField = $this->get(self::SETTING_SESSION_FIELD_FILTERS);
154
155
        $fields = explode(',', $settingField);
156
157
        return array_map('trim', $fields);
158
    }
159
160
    public function isEnableForExercise(int $exerciseId): bool
161
    {
162
        $renderRegion = $this->isEnabled(true)
163
            && strpos($_SERVER['SCRIPT_NAME'], '/main/exercise/exercise_submit.php') !== false;
164
165
        if (!$renderRegion) {
166
            return false;
167
        }
168
169
        $objFieldValue = new ExtraFieldValue('exercise');
170
        $values = $objFieldValue->get_values_by_handler_and_field_variable(
171
            $exerciseId,
172
            self::FIELD_SELECTED
173
        );
174
175
        return $values && (bool) $values['value'];
176
    }
177
178
    protected function createLinkToCourseTool($name, $courseId, $iconName = null, $link = null, $sessionId = 0, $category = 'plugin'): ?CTool
179
    {
180
        $tool = parent::createLinkToCourseTool($name, $courseId, $iconName, $link, $sessionId, $category);
181
182
        if (!$tool) {
183
            return null;
184
        }
185
186
        $tool->setName(
187
            $tool->getName().':teacher'
188
        );
189
190
        $em = Database::getManager();
191
        $em->persist($tool);
192
        $em->flush();
193
194
        return $tool;
195
    }
196
}
197