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'); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
38 | if ($result && isset($result['value']) && 1 === (int) $result['value']) { |
||
39 | return true; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | return false; |
||
44 | } |
||
45 | |||
46 | public static function saveSignature($userId, $trackInfo, $file) |
||
47 | { |
||
48 | if (false === self::validateSignatureAccess($userId, $trackInfo)) { |
||
49 | return false; |
||
50 | } |
||
51 | |||
52 | $signature = self::getSignature($userId, $trackInfo); |
||
53 | if (false !== $signature) { |
||
54 | return false; |
||
55 | } |
||
56 | |||
57 | if (empty($file)) { |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | $params = [ |
||
62 | 'item_id' => $trackInfo['exe_id'], |
||
63 | 'extra_signature' => $file, |
||
64 | ]; |
||
65 | $extraFieldValue = new ExtraFieldValue('track_exercise'); |
||
66 | $extraFieldValue->saveFieldValues( |
||
67 | $params, |
||
68 | true |
||
69 | ); |
||
70 | |||
71 | $signature = self::getSignature($userId, $trackInfo); |
||
72 | if (false !== $signature) { |
||
73 | return true; |
||
74 | } |
||
75 | |||
76 | return true; |
||
77 | } |
||
78 | |||
79 | public static function validateSignatureAccess($userId, $trackInfo) |
||
80 | { |
||
81 | $userId = (int) $userId; |
||
82 | if (isset($trackInfo['exe_id']) && isset($trackInfo['exe_user_id']) && |
||
83 | !empty($trackInfo['exe_id']) && !empty($trackInfo['exe_user_id']) && |
||
84 | $trackInfo['status'] !== 'incomplete' |
||
85 | ) { |
||
86 | if ($userId === (int) $trackInfo['exe_user_id']) { |
||
87 | return true; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | return false; |
||
92 | } |
||
93 | |||
94 | public static function getSignature($userId, $trackInfo) |
||
95 | { |
||
96 | if (false === self::validateSignatureAccess($userId, $trackInfo)) { |
||
97 | return false; |
||
98 | } |
||
99 | |||
100 | $extraFieldValue = new ExtraFieldValue('track_exercise'); |
||
101 | $result = $extraFieldValue->get_values_by_handler_and_field_variable($trackInfo['exe_id'], 'signature'); |
||
102 | |||
103 | if ($result && isset($result['value']) && !empty($result['value'])) { |
||
104 | return $result['value']; |
||
105 | } |
||
106 | |||
107 | return false; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Get the plugin Name. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | public function get_name() |
||
116 | { |
||
117 | return 'exercise_signature'; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Creates this plugin's related tables in the internal database. |
||
122 | * Installs course fields in all courses. |
||
123 | */ |
||
124 | public function install() |
||
125 | { |
||
126 | $extraField = new ExtraField('exercise'); |
||
127 | $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature_activated'); |
||
128 | $exists = $extraFieldHandler !== false; |
||
129 | |||
130 | if (!$exists) { |
||
131 | $extraField->save( |
||
132 | [ |
||
133 | 'field_type' => 13, // checkbox yes/no |
||
134 | 'variable' => 'signature_activated', |
||
135 | 'display_text' => get_plugin_lang('SignatureActivated', 'ExerciseSignaturePlugin'), |
||
136 | 'default_value' => null, |
||
137 | 'field_order' => null, |
||
138 | 'visible_to_self' => 1, |
||
139 | 'changeable' => 1, |
||
140 | 'filter' => null, |
||
141 | ] |
||
142 | ); |
||
143 | } |
||
144 | |||
145 | $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature_mandatory'); |
||
146 | $exists = $extraFieldHandler !== false; |
||
147 | |||
148 | if (!$exists) { |
||
149 | $extraField->save( |
||
150 | [ |
||
151 | 'field_type' => 13, // checkbox yes/no |
||
152 | 'variable' => 'signature_mandatory', |
||
153 | 'display_text' => get_plugin_lang('SignatureMandatory', 'ExerciseSignaturePlugin'), |
||
154 | 'default_value' => null, |
||
155 | 'field_order' => null, |
||
156 | 'visible_to_self' => 1, |
||
157 | 'changeable' => 1, |
||
158 | 'filter' => null, |
||
159 | ] |
||
160 | ); |
||
161 | } |
||
162 | |||
163 | $extraField = new ExtraField('track_exercise'); |
||
164 | $extraFieldHandler = $extraField->get_handler_field_info_by_field_variable('signature'); |
||
165 | $exists = $extraFieldHandler !== false; |
||
166 | |||
167 | if (!$exists) { |
||
168 | $extraField->save( |
||
169 | [ |
||
170 | 'field_type' => 2, // textarea |
||
171 | 'variable' => 'signature', |
||
172 | 'display_text' => get_plugin_lang('Signature', 'ExerciseSignaturePlugin'), |
||
173 | 'default_value' => null, |
||
174 | 'field_order' => null, |
||
175 | 'visible_to_self' => 1, |
||
176 | 'changeable' => 1, |
||
177 | 'filter' => null, |
||
178 | ] |
||
179 | ); |
||
180 | } |
||
181 | |||
182 | $table = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES); |
||
183 | $sql = "ALTER TABLE $table MODIFY COLUMN value LONGTEXT null;"; |
||
184 | Database::query($sql); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Drops this plugins' related tables from the internal database. |
||
189 | * Uninstalls course fields in all courses(). |
||
190 | */ |
||
191 | public function uninstall() |
||
192 | { |
||
193 | $extraField = new ExtraField('track_exercise'); |
||
194 | $fieldInfo = $extraField->get_handler_field_info_by_field_variable('signature_activated'); |
||
195 | |||
196 | if ($fieldInfo) { |
||
197 | $extraField->delete($fieldInfo['id']); |
||
198 | } |
||
199 | |||
200 | $extraField = new ExtraField('exercise'); |
||
201 | $fieldInfo = $extraField->get_handler_field_info_by_field_variable('signature'); |
||
202 | if ($fieldInfo) { |
||
203 | $extraField->delete($fieldInfo['id']); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 |