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

ExerciseFocusedPlugin::getLinkReporting()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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