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

ExerciseFocusedPlugin::createLinkToCourseTool()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 9.9666
cc 2
nc 2
nop 6
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_ENABLE_TIME_LIMIT = 'enable_time_limit';
14
    public const SETTING_TIME_LIMIT = 'time_limit';
15
    public const SETTING_ENABLE_OUTFOCUSED_LIMIT = 'enable_outfocused_limit';
16
    public const SETTING_OUTFOCUSED_LIMIT = 'outfocused_limit';
17
    public const SETTING_SESSION_FIELD_FILTERS = 'session_field_filters';
18
    public const SETTING_PERCENTAGE_SAMPLING = 'percentage_sampling';
19
20
    public const FIELD_SELECTED = 'exercisefocused_selected';
21
22
    private const TABLE_LOG = 'plugin_exercisefocused_log';
23
24
    protected function __construct()
25
    {
26
        $settings = [
27
            self::SETTING_TOOL_ENABLE => 'boolean',
28
            self::SETTING_ENABLE_TIME_LIMIT => 'boolean',
29
            self::SETTING_TIME_LIMIT => 'text',
30
            self::SETTING_ENABLE_OUTFOCUSED_LIMIT => 'boolean',
31
            self::SETTING_OUTFOCUSED_LIMIT => 'text',
32
            self::SETTING_SESSION_FIELD_FILTERS => 'text',
33
            self::SETTING_PERCENTAGE_SAMPLING => 'text',
34
        ];
35
36
        parent::__construct(
37
            "0.0.1",
38
            "Angel Fernando Quiroz Campos <[email protected]>",
39
            $settings
40
        );
41
    }
42
43
    public static function create(): ?ExerciseFocusedPlugin
44
    {
45
        static $result = null;
46
47
        return $result ?: $result = new self();
48
    }
49
50
    /**
51
     * @throws ToolsException
52
     */
53
    public function install()
54
    {
55
        $em = Database::getManager();
56
57
        if ($em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
58
            return;
59
        }
60
61
        $schemaTool = new SchemaTool($em);
62
        $schemaTool->createSchema(
63
            [
64
                $em->getClassMetadata(Log::class),
65
            ]
66
        );
67
68
        $objField = new ExtraField('exercise');
69
        $objField->save([
70
            'variable' => self::FIELD_SELECTED,
71
            'field_type' => ExtraField::FIELD_TYPE_CHECKBOX,
72
            'display_text' => $this->get_title(),
73
            'visible_to_self' => true,
74
            'changeable' => true,
75
            'filter' => false,
76
        ]);
77
    }
78
79
    public function uninstall()
80
    {
81
        $em = Database::getManager();
82
83
        if (!$em->getConnection()->getSchemaManager()->tablesExist([self::TABLE_LOG])) {
84
            return;
85
        }
86
87
        $schemaTool = new SchemaTool($em);
88
        $schemaTool->dropSchema(
89
            [
90
                $em->getClassMetadata(Log::class),
91
            ]
92
        );
93
94
        $objField = new ExtraField('exercise');
95
        $extraFieldInfo = $objField->get_handler_field_info_by_field_variable(self::FIELD_SELECTED);
96
97
        if ($extraFieldInfo) {
98
            $objField->delete($extraFieldInfo['id']);
99
        }
100
    }
101
102
    public function getAdminUrl(): string
103
    {
104
        $name = $this->get_name();
105
        $webPath = api_get_path(WEB_PLUGIN_PATH).$name;
106
107
        return "$webPath/admin.php";
108
    }
109
110
    public function getActionTitle($action): string
111
    {
112
        switch ($action) {
113
            case Log::TYPE_OUTFOCUSED:
114
                return $this->get_lang('Outfocused');
115
            case Log::TYPE_RETURN:
116
                return $this->get_lang('Return');
117
            case Log::TYPE_OUTFOCUSED_LIMIT:
118
                return $this->get_lang('MaxOutfocusedReached');
119
            case Log::TYPE_TIME_LIMIT:
120
                return $this->get_lang('TimeLimitReached');
121
        }
122
123
        return '';
124
    }
125
126
    public function getLinkReporting(int $exerciseId): string
127
    {
128
        if (!$this->isEnabled(true)) {
129
            return '';
130
        }
131
132
        $values = (new ExtraFieldValue('exercise'))
133
            ->get_values_by_handler_and_field_variable($exerciseId, self::FIELD_SELECTED);
134
135
        if (!$values || !$values['value']) {
136
            return '';
137
        }
138
139
        $icon = Display::return_icon(
140
            'window_list_slide.png',
141
            $this->get_lang('ReportByAttempts'),
142
            [],
143
            ICON_SIZE_MEDIUM
144
        );
145
146
        $url = api_get_path(WEB_PLUGIN_PATH)
147
            .'exercisefocused/pages/reporting.php?'
148
            .api_get_cidreq().'&'.http_build_query(['id' => $exerciseId]);
149
150
        return Display::url($icon, $url);
151
    }
152
153
    public function getSessionFieldList(): array
154
    {
155
        $settingField = $this->get(self::SETTING_SESSION_FIELD_FILTERS);
156
157
        $fields = explode(',', $settingField);
158
159
        return array_map('trim', $fields);
160
    }
161
162
    public function isEnableForExercise(int $exerciseId): bool
163
    {
164
        $renderRegion = $this->isEnabled(true)
165
            && strpos($_SERVER['SCRIPT_NAME'], '/main/exercise/exercise_submit.php') !== false;
166
167
        if (!$renderRegion) {
168
            return false;
169
        }
170
171
        $objFieldValue = new ExtraFieldValue('exercise');
172
        $values = $objFieldValue->get_values_by_handler_and_field_variable(
173
            $exerciseId,
174
            self::FIELD_SELECTED
175
        );
176
177
        return $values && (bool) $values['value'];
178
    }
179
180
    public function calculateMotive(int $outfocusedLimitCount, int $timeLimitCount)
181
    {
182
        $motive = get_lang('ExerciseFinished');
183
184
        if ($outfocusedLimitCount > 0) {
185
            $motive = $this->get_lang('MaxOutfocusedReached');
186
        }
187
188
        if ($timeLimitCount > 0) {
189
            $motive = $this->get_lang('TimeLimitReached');
190
        }
191
192
        return $motive;
193
    }
194
195
    protected function createLinkToCourseTool($name, $courseId, $iconName = null, $link = null, $sessionId = 0, $category = 'plugin'): ?CTool
196
    {
197
        $tool = parent::createLinkToCourseTool($name, $courseId, $iconName, $link, $sessionId, $category);
198
199
        if (!$tool) {
200
            return null;
201
        }
202
203
        $tool->setName(
204
            $tool->getName().':teacher'
205
        );
206
207
        $em = Database::getManager();
208
        $em->persist($tool);
209
        $em->flush();
210
211
        return $tool;
212
    }
213
}
214