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

ExerciseFocusedPlugin   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 20
eloc 81
c 1
b 0
f 0
dl 0
loc 159
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getActionTitle() 0 14 5
A createLinkToCourseTool() 0 17 2
A create() 0 5 2
A __construct() 0 17 1
A install() 0 23 2
A uninstall() 0 20 3
A getSessionFieldList() 0 7 1
A getLinkReporting() 0 25 4
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_ABANDONMENT = 'enable_abandonment_limit';
15
    public const SETTING_ABANDONMENT_LIMIT = 'abandonment_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_ABANDONMENT => 'boolean',
28
            self::SETTING_ABANDONMENT_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
        $this->isAdminPlugin = true;
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' => ExerciseFocusedPlugin::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(ExerciseFocusedPlugin::FIELD_SELECTED);
94
95
        if ($extraFieldInfo) {
96
            $objField->delete($extraFieldInfo['id']);
97
        }
98
    }
99
100
    public function getActionTitle($action): string
101
    {
102
        switch ($action) {
103
            case Log::TYPE_OUTFOCUSED:
104
                return $this->get_lang('Outfocused');
105
            case Log::TYPE_RETURN:
106
                return $this->get_lang('Return');
107
            case Log::TYPE_ABANDONMENT_LIMIT:
108
                return $this->get_lang('MaxAbandonments');
109
            case Log::TYPE_TIME_LIMIT:
110
                return $this->get_lang('TimeLimit');
111
        }
112
113
        return '';
114
    }
115
116
    public function getLinkReporting(int $exerciseId): string
117
    {
118
        if (!$this->isEnabled(true)) {
119
            return '';
120
        }
121
122
        $values = (new ExtraFieldValue('exercise'))
123
            ->get_values_by_handler_and_field_variable($exerciseId, self::FIELD_SELECTED);
124
125
        if (!$values || !$values['value']) {
126
            return '';
127
        }
128
129
        $icon = Display::return_icon(
130
            'window_list_slide.png',
131
            $this->get_lang('ReportByAttempts'),
132
            [],
133
            ICON_SIZE_MEDIUM
134
        );
135
136
        $url = api_get_path(WEB_PLUGIN_PATH)
137
            .'exercisefocused/pages/reporting.php?'
138
            .api_get_cidreq().'&'.http_build_query(['id' => $exerciseId]);
139
140
        return Display::url($icon, $url);
141
    }
142
143
    public function getSessionFieldList(): array
144
    {
145
        $settingField = $this->get(self::SETTING_SESSION_FIELD_FILTERS);
146
147
        $fields = explode(',', $settingField);
148
149
        return array_map('trim', $fields);
150
    }
151
152
    protected function createLinkToCourseTool($name, $courseId, $iconName = null, $link = null, $sessionId = 0, $category = 'plugin'): ?CTool
153
    {
154
        $tool = parent::createLinkToCourseTool($name, $courseId, $iconName, $link, $sessionId, $category);
155
156
        if (!$tool) {
157
            return null;
158
        }
159
160
        $tool->setName(
161
            $tool->getName().':teacher'
162
        );
163
164
        $em = Database::getManager();
165
        $em->persist($tool);
166
        $em->flush();
167
168
        return $tool;
169
    }
170
}
171