Passed
Push — master ( 621302...15a517 )
by Angel Fernando Quiroz
16:26 queued 07:49
created

ExerciseSignaturePlugin   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 96
dl 0
loc 188
rs 9.76
c 0
b 0
f 0
wmc 33

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getSignature() 0 14 5
A create() 0 5 2
A saveSignature() 0 31 5
A __construct() 0 9 1
A exerciseHasSignatureActivated() 0 15 6
B validateSignatureAccess() 0 13 7
A uninstall() 0 13 3
A install() 0 61 4
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Framework\Container;
6
7
class ExerciseSignaturePlugin extends Plugin
8
{
9
    public function __construct()
10
    {
11
        parent::__construct(
12
            '0.1',
13
            'Julio Montoya',
14
            [
15
            ]
16
        );
17
        $this->isAdminPlugin = true;
18
    }
19
20
    /**
21
     * @return $this
22
     */
23
    public static function create()
24
    {
25
        static $instance = null;
26
27
        return $instance ? $instance : $instance = new self();
28
    }
29
30
    public static function exerciseHasSignatureActivated(Exercise $exercise)
31
    {
32
        if (empty($exercise->iId)) {
33
            return false;
34
        }
35
36
        if (Container::getPluginHelper()->isPluginEnabled('ExerciseSignature')) {
37
            $extraFieldValue = new ExtraFieldValue('exercise');
38
            $result = $extraFieldValue->get_values_by_handler_and_field_variable($exercise->iId, 'signature_activated');
39
            if ($result && isset($result['value']) && 1 === (int) $result['value']) {
40
                return true;
41
            }
42
        }
43
44
        return false;
45
    }
46
47
    public static function saveSignature($userId, $trackInfo, $file)
48
    {
49
        if (false === self::validateSignatureAccess($userId, $trackInfo)) {
50
            return false;
51
        }
52
53
        $signature = self::getSignature($userId, $trackInfo);
54
        if (false !== $signature) {
55
            return false;
56
        }
57
58
        if (empty($file)) {
59
            return false;
60
        }
61
62
        $params = [
63
            'item_id' => $trackInfo['exe_id'],
64
            'extra_signature' => $file,
65
        ];
66
        $extraFieldValue = new ExtraFieldValue('track_exercise');
67
        $extraFieldValue->saveFieldValues(
68
            $params,
69
            true
70
        );
71
72
        $signature = self::getSignature($userId, $trackInfo);
73
        if (false !== $signature) {
74
            return true;
75
        }
76
77
        return true;
78
    }
79
80
    public static function validateSignatureAccess($userId, $trackInfo)
81
    {
82
        $userId = (int) $userId;
83
        if (isset($trackInfo['exe_id']) && isset($trackInfo['exe_user_id']) &&
84
            !empty($trackInfo['exe_id']) && !empty($trackInfo['exe_user_id']) &&
85
            'incomplete' !== $trackInfo['status']
86
        ) {
87
            if ($userId === (int) $trackInfo['exe_user_id']) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    public static function getSignature($userId, $trackInfo)
96
    {
97
        if (false === self::validateSignatureAccess($userId, $trackInfo)) {
98
            return false;
99
        }
100
101
        $extraFieldValue = new ExtraFieldValue('track_exercise');
102
        $result = $extraFieldValue->get_values_by_handler_and_field_variable($trackInfo['exe_id'], 'signature');
103
104
        if ($result && isset($result['value']) && !empty($result['value'])) {
105
            return $result['value'];
106
        }
107
108
        return false;
109
    }
110
111
    /**
112
     * Creates this plugin's related tables in the internal database.
113
     * Installs course fields in all courses.
114
     */
115
    public function install()
116
    {
117
        $extraField = new ExtraField('exercise');
118
        $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature_activated');
119
        $exists = false !== $extraFieldHandler;
120
121
        if (!$exists) {
122
            $extraField->save(
123
                [
124
                    'value_type' => 13, // checkbox yes/no
125
                    'variable' => 'signature_activated',
126
                    'display_text' => get_plugin_lang('SignatureActivated', 'ExerciseSignaturePlugin'),
127
                    'default_value' => null,
128
                    'field_order' => null,
129
                    'visible_to_self' => 1,
130
                    'changeable' => 1,
131
                    'filter' => null,
132
                ]
133
            );
134
        }
135
136
        $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature_mandatory');
137
        $exists = false !== $extraFieldHandler;
138
139
        if (!$exists) {
140
            $extraField->save(
141
                [
142
                    'value_type' => 13, // checkbox yes/no
143
                    'variable' => 'signature_mandatory',
144
                    'display_text' => get_plugin_lang('SignatureMandatory', 'ExerciseSignaturePlugin'),
145
                    'default_value' => null,
146
                    'field_order' => null,
147
                    'visible_to_self' => 1,
148
                    'changeable' => 1,
149
                    'filter' => null,
150
                ]
151
            );
152
        }
153
154
        $extraField = new ExtraField('track_exercise');
155
        $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature');
156
        $exists = false !== $extraFieldHandler;
157
158
        if (!$exists) {
159
            $extraField->save(
160
                [
161
                    'value_type' => 2, // textarea
162
                    'variable' => 'signature',
163
                    'display_text' => get_plugin_lang('Signature', 'ExerciseSignaturePlugin'),
164
                    'default_value' => null,
165
                    'field_order' => null,
166
                    'visible_to_self' => 1,
167
                    'changeable' => 1,
168
                    'filter' => null,
169
                ]
170
            );
171
        }
172
173
        $table = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
174
        $sql = "ALTER TABLE $table MODIFY COLUMN field_value LONGTEXT null;";
175
        Database::query($sql);
176
    }
177
178
    /**
179
     * Drops this plugins' related tables from the internal database.
180
     * Uninstalls course fields in all courses().
181
     */
182
    public function uninstall()
183
    {
184
        $extraField = new ExtraField('track_exercise');
185
        $fieldInfo = $extraField->get_handler_field_info_by_field_variable('signature_activated');
186
187
        if ($fieldInfo) {
188
            $extraField->delete($fieldInfo['id']);
189
        }
190
191
        $extraField = new ExtraField('exercise');
192
        $fieldInfo = $extraField->get_handler_field_info_by_field_variable('signature');
193
        if ($fieldInfo) {
194
            $extraField->delete($fieldInfo['id']);
195
        }
196
    }
197
}
198