Passed
Push — 1.11.x ( eb7043...58b7ae )
by Julito
15:49
created

exerciseHasSignatureActivated()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 8
c 1
b 1
f 0
nc 4
nop 1
dl 0
loc 15
rs 9.2222
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
class ExerciseSignaturePlugin extends Plugin
6
{
7
    public function __construct()
8
    {
9
        parent::__construct(
10
            '0.1',
11
            'Julio Montoya',
12
            [
13
                'tool_enable' => 'boolean',
14
            ]
15
        );
16
        $this->isAdminPlugin = true;
17
    }
18
19
    /**
20
     * @return $this
21
     */
22
    public static function create()
23
    {
24
        static $instance = null;
25
26
        return $instance ? $instance : $instance = new self();
27
    }
28
29
    public static function exerciseHasSignatureActivated(Exercise $exercise)
30
    {
31
        if (empty($exercise->iId)) {
32
            return false;
33
        }
34
35
        if ('true' === api_get_plugin_setting('exercise_signature', 'tool_enable')) {
36
            $extraFieldValue = new ExtraFieldValue('exercise');
37
            $result = $extraFieldValue->get_values_by_handler_and_field_variable($exercise->iId, 'signature_activated');
38
            if ($result && isset($result['value']) && 1 === (int) $result['value']) {
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
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
            $trackInfo['status'] !== 'incomplete'
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
     * Get the plugin Name.
113
     *
114
     * @return string
115
     */
116
    public function get_name()
117
    {
118
        return 'exercise_signature';
119
    }
120
121
    /**
122
     * Creates this plugin's related tables in the internal database.
123
     * Installs course fields in all courses.
124
     */
125
    public function install()
126
    {
127
        $extraField = new ExtraField('exercise');
128
        $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature_activated');
129
        $exists = $extraFieldHandler !== false;
130
131
        if (!$exists) {
132
            $extraField->save(
133
                [
134
                    'field_type' => 13, // checkbox yes/no
135
                    'variable' => 'signature_activated',
136
                    'display_text' => get_plugin_lang('SignatureActivated', 'ExerciseSignaturePlugin'),
137
                    'default_value' => null,
138
                    'field_order' => null,
139
                    'visible_to_self' => 1,
140
                    'changeable' => 1,
141
                    'filter' => null,
142
                ]
143
            );
144
        }
145
146
        $extraField = new ExtraField('track_exercise');
147
        $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature');
148
        $exists = $extraFieldHandler !== false;
149
150
        if (!$exists) {
151
            $extraField->save(
152
                [
153
                    'field_type' => 2, // textarea
154
                    'variable' => 'signature',
155
                    'display_text' => get_plugin_lang('Signature', 'ExerciseSignaturePlugin'),
156
                    'default_value' => null,
157
                    'field_order' => null,
158
                    'visible_to_self' => 1,
159
                    'changeable' => 1,
160
                    'filter' => null,
161
                ]
162
            );
163
        }
164
165
        $table = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
166
        $sql = "ALTER TABLE $table MODIFY COLUMN value LONGTEXT null;";
167
        Database::query($sql);
168
    }
169
170
    /**
171
     * Drops this plugins' related tables from the internal database.
172
     * Uninstalls course fields in all courses().
173
     */
174
    public function uninstall()
175
    {
176
        $extraField = new ExtraField('track_exercise');
177
        $fieldInfo = $extraField->get_handler_field_info_by_field_variable('signature_activated');
178
179
        if ($fieldInfo) {
180
            $extraField->delete($fieldInfo['id']);
181
        }
182
183
        $extraField = new ExtraField('exercise');
184
        $fieldInfo = $extraField->get_handler_field_info_by_field_variable('signature');
185
        if ($fieldInfo) {
186
            $extraField->delete($fieldInfo['id']);
187
        }
188
    }
189
}
190