|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
|
4
|
|
|
|
|
5
|
|
|
use Chamilo\CoreBundle\Entity\ExtraFieldOptions; |
|
6
|
|
|
use Chamilo\CoreBundle\Framework\Container; |
|
7
|
|
|
use Chamilo\CoreBundle\Component\Utils\ActionIcon; |
|
8
|
|
|
use Chamilo\CoreBundle\Repository\ExtraFieldOptionsRepository; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Handles the extra fields for various objects (users, sessions, courses). |
|
12
|
|
|
*/ |
|
13
|
|
|
class ExtraFieldOption extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
public $columns = [ |
|
16
|
|
|
'id', |
|
17
|
|
|
'field_id', |
|
18
|
|
|
'option_value', |
|
19
|
|
|
'display_text', |
|
20
|
|
|
'option_order', |
|
21
|
|
|
'priority', |
|
22
|
|
|
'priority_message', |
|
23
|
|
|
'tms', |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
public $extraField; |
|
27
|
|
|
public $fieldId; |
|
28
|
|
|
|
|
29
|
|
|
/** @var ExtraFieldOptionsRepository */ |
|
30
|
|
|
public $repo; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Gets the table for the type of object for which we are using an extra field. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $type Type of object (course, user or session) |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($type) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct(); |
|
40
|
|
|
$this->type = $type; |
|
41
|
|
|
$extraField = new ExtraField($this->type); |
|
42
|
|
|
$this->extraField = $extraField; |
|
43
|
|
|
$this->table = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS); |
|
44
|
|
|
$this->tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD); |
|
45
|
|
|
$this->repo = Database::getManager()->getRepository(ExtraFieldOptions::class); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return ExtraField |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getExtraField() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->extraField; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Gets the number of options available for this field. |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $fieldId |
|
60
|
|
|
* |
|
61
|
|
|
* @return int Number of options |
|
62
|
|
|
* @assert ('') === false |
|
63
|
|
|
* @assert (-1) == 0 |
|
64
|
|
|
* @assert (0) == 0 |
|
65
|
|
|
*/ |
|
66
|
|
|
public function get_count_by_field_id($fieldId) |
|
67
|
|
|
{ |
|
68
|
|
|
if (empty($fieldId)) { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
$extraFieldType = $this->getExtraField()->getItemType(); |
|
72
|
|
|
$fieldId = (int) $fieldId; |
|
73
|
|
|
|
|
74
|
|
|
$sql = "SELECT count(*) as count |
|
75
|
|
|
FROM $this->table o |
|
76
|
|
|
INNER JOIN $this->tableExtraField e |
|
77
|
|
|
ON o.field_id = e.id |
|
78
|
|
|
WHERE |
|
79
|
|
|
o.field_id = $fieldId AND |
|
80
|
|
|
e.item_type = $extraFieldType "; |
|
81
|
|
|
$result = Database::query($sql); |
|
82
|
|
|
$result = Database::fetch_array($result); |
|
83
|
|
|
|
|
84
|
|
|
return $result['count']; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns a list of options for a specific field, separated by ";". |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $field_id |
|
91
|
|
|
* @param bool $add_id_in_array Indicates whether we want the results to be given with their id |
|
92
|
|
|
* @param string $ordered_by Order by clause (without the "order by") to be added to the SQL query |
|
93
|
|
|
* |
|
94
|
|
|
* @return string List of options separated by ; |
|
95
|
|
|
* @assert (-1, false, null) == '' |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getFieldOptionsToString($field_id, $add_id_in_array = false, $ordered_by = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$options = self::get_field_options_by_field($field_id, $add_id_in_array, $ordered_by); |
|
|
|
|
|
|
100
|
|
|
$new_options = []; |
|
101
|
|
|
if (!empty($options)) { |
|
102
|
|
|
foreach ($options as $option) { |
|
103
|
|
|
$new_options[] = $option['option_value'].':'.$option['display_text']; |
|
104
|
|
|
} |
|
105
|
|
|
$string = implode(';', $new_options); |
|
106
|
|
|
|
|
107
|
|
|
return $string; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return ''; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Delete all the options of a specific field. |
|
115
|
|
|
* |
|
116
|
|
|
* @param int $field_id |
|
117
|
|
|
* |
|
118
|
|
|
* @assert (-1) === false |
|
119
|
|
|
*/ |
|
120
|
|
|
public function delete_all_options_by_field_id($field_id) |
|
121
|
|
|
{ |
|
122
|
|
|
$field_id = (int) $field_id; |
|
123
|
|
|
$sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; |
|
124
|
|
|
|
|
125
|
|
|
return Database::query($sql); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param array $params |
|
130
|
|
|
* @param bool $showQuery |
|
131
|
|
|
* |
|
132
|
|
|
* @return int|bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function saveOptions($params, $showQuery = false) |
|
135
|
|
|
{ |
|
136
|
|
|
$optionInfo = $this->repo->getFieldOptionByFieldAndOption( |
|
137
|
|
|
(int) $params['field_id'], |
|
138
|
|
|
(string) $params['option_value'], |
|
139
|
|
|
$this->extraField->getItemType() |
|
140
|
|
|
); |
|
141
|
|
|
|
|
142
|
|
|
if (!$optionInfo) { |
|
|
|
|
|
|
143
|
|
|
$optionValue = api_replace_dangerous_char($params['option_value']); |
|
144
|
|
|
$order = $this->get_max_order($params['field_id']); |
|
145
|
|
|
$newParams = [ |
|
146
|
|
|
'field_id' => $params['field_id'], |
|
147
|
|
|
'value' => trim($optionValue), |
|
148
|
|
|
'display_text' => trim($params['display_text']), |
|
149
|
|
|
'option_order' => $order, |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
return parent::save($newParams, $showQuery); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return false; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Saves an option into the corresponding *_field_options table. |
|
160
|
|
|
* |
|
161
|
|
|
* @param array $params Parameters to be considered for the insertion |
|
162
|
|
|
* @param bool $showQuery Whether to show the query (sent to the parent save() method) |
|
163
|
|
|
* |
|
164
|
|
|
* @return bool True on success, false on error |
|
165
|
|
|
* @assert (array('field_id'=>0), false) === false |
|
166
|
|
|
* @assert (array('field_id'=>1), false) === true |
|
167
|
|
|
*/ |
|
168
|
|
|
public function save($params, $showQuery = false) |
|
169
|
|
|
{ |
|
170
|
|
|
$field_id = (int) $params['field_id']; |
|
171
|
|
|
|
|
172
|
|
|
if (empty($field_id)) { |
|
173
|
|
|
return false; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$parseOptions = in_array( |
|
177
|
|
|
$params['value_type'], |
|
178
|
|
|
[ |
|
179
|
|
|
ExtraField::FIELD_TYPE_RADIO, |
|
180
|
|
|
ExtraField::FIELD_TYPE_SELECT, |
|
181
|
|
|
ExtraField::FIELD_TYPE_SELECT_MULTIPLE, |
|
182
|
|
|
ExtraField::FIELD_TYPE_DOUBLE_SELECT, |
|
183
|
|
|
ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD, |
|
184
|
|
|
ExtraField::FIELD_TYPE_TRIPLE_SELECT, |
|
185
|
|
|
] |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
|
|
if (empty($params['field_options']) || !$parseOptions) { |
|
189
|
|
|
return true; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
switch ($params['value_type']) { |
|
193
|
|
|
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: |
|
194
|
|
|
//$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; |
|
195
|
|
|
case ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD: |
|
196
|
|
|
//$params['field_options'] = Option 1|Option 2|Option 3 |
|
197
|
|
|
$options_parsed = ExtraField::extra_field_double_select_convert_string_to_array( |
|
198
|
|
|
$params['field_options'] |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
if (empty($options_parsed)) { |
|
202
|
|
|
break; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
foreach ($options_parsed as $key => $option) { |
|
206
|
|
|
$new_params = [ |
|
207
|
|
|
'field_id' => $field_id, |
|
208
|
|
|
'option_value' => 0, |
|
209
|
|
|
'display_text' => $option['label'], |
|
210
|
|
|
'option_order' => 0, |
|
211
|
|
|
]; |
|
212
|
|
|
// Looking if option already exists: |
|
213
|
|
|
$option_info = $this->get_field_option_by_field_id_and_option_display_text( |
|
214
|
|
|
$field_id, |
|
215
|
|
|
$option['label'] |
|
216
|
|
|
); |
|
217
|
|
|
|
|
218
|
|
|
if (empty($option_info)) { |
|
219
|
|
|
$sub_id = parent::save($new_params, $showQuery); |
|
220
|
|
|
} else { |
|
221
|
|
|
$sub_id = $option_info['id']; |
|
222
|
|
|
$new_params['id'] = $sub_id; |
|
223
|
|
|
parent::update($new_params, $showQuery); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
if (ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD == $params['value_type']) { |
|
227
|
|
|
continue; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
foreach ($option['options'] as $sub_option) { |
|
231
|
|
|
if (empty($sub_option)) { |
|
232
|
|
|
continue; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$new_params = [ |
|
236
|
|
|
'field_id' => $field_id, |
|
237
|
|
|
'option_value' => $sub_id, |
|
238
|
|
|
'display_text' => $sub_option, |
|
239
|
|
|
'option_order' => 0, |
|
240
|
|
|
]; |
|
241
|
|
|
$option_info = $this->getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue( |
|
242
|
|
|
$field_id, |
|
243
|
|
|
$sub_option, |
|
244
|
|
|
$sub_id |
|
245
|
|
|
); |
|
246
|
|
|
|
|
247
|
|
|
if (empty($option_info)) { |
|
248
|
|
|
parent::save($new_params, $showQuery); |
|
249
|
|
|
|
|
250
|
|
|
continue; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
$new_params['id'] = $option_info['id']; |
|
254
|
|
|
parent::update($new_params, $showQuery); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
break; |
|
258
|
|
|
case ExtraField::FIELD_TYPE_TRIPLE_SELECT: |
|
259
|
|
|
//Format: Option1\Option11:Option111;Option112\Option12:Option121|Option2\Option21:Option211 |
|
260
|
|
|
$options = ExtraField::tripleSelectConvertStringToArray($params['field_options']); |
|
261
|
|
|
|
|
262
|
|
|
if (!$options) { |
|
|
|
|
|
|
263
|
|
|
break; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
foreach ($options as $level1) { |
|
267
|
|
|
$level1Params = [ |
|
268
|
|
|
'field_id' => $field_id, |
|
269
|
|
|
'option_value' => 0, |
|
270
|
|
|
'display_text' => $level1['label'], |
|
271
|
|
|
'option_order' => 0, |
|
272
|
|
|
]; |
|
273
|
|
|
$optionInfo = $this->get_field_option_by_field_id_and_option_display_text( |
|
274
|
|
|
$field_id, |
|
275
|
|
|
$level1['label'] |
|
276
|
|
|
); |
|
277
|
|
|
|
|
278
|
|
|
if (empty($optionInfo)) { |
|
279
|
|
|
$level1Id = parent::save($level1Params); |
|
280
|
|
|
} else { |
|
281
|
|
|
$level1Id = $optionInfo['id']; |
|
282
|
|
|
$level1Params['id'] = $level1Id; |
|
283
|
|
|
parent::update($level1Params); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
foreach ($level1['options'] as $level2) { |
|
287
|
|
|
$level2Params = [ |
|
288
|
|
|
'field_id' => $field_id, |
|
289
|
|
|
'option_value' => $level1Id, |
|
290
|
|
|
'display_text' => $level2['label'], |
|
291
|
|
|
'display_order' => 0, |
|
292
|
|
|
]; |
|
293
|
|
|
$optionInfo = $this->getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue( |
|
294
|
|
|
$field_id, |
|
295
|
|
|
$level2['label'], |
|
296
|
|
|
$level1Id |
|
297
|
|
|
); |
|
298
|
|
|
|
|
299
|
|
|
if (empty($optionInfo)) { |
|
300
|
|
|
$level2Id = parent::save($level2Params); |
|
301
|
|
|
} else { |
|
302
|
|
|
$level2Id = $optionInfo['id']; |
|
303
|
|
|
$level2Params['id'] = $level2Id; |
|
304
|
|
|
parent::update($level2Params); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
foreach ($level2['options'] as $level3) { |
|
308
|
|
|
foreach ($level3 as $item) { |
|
309
|
|
|
$level3Params = [ |
|
310
|
|
|
'field_id' => $field_id, |
|
311
|
|
|
'option_value' => $level2Id, |
|
312
|
|
|
'display_text' => $item, |
|
313
|
|
|
'display_order' => 0, |
|
314
|
|
|
]; |
|
315
|
|
|
$optionInfo = $this->getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue( |
|
316
|
|
|
$field_id, |
|
317
|
|
|
$item, |
|
318
|
|
|
$level2Id |
|
319
|
|
|
); |
|
320
|
|
|
|
|
321
|
|
|
if (empty($optionInfo)) { |
|
322
|
|
|
parent::save($level3Params); |
|
323
|
|
|
} else { |
|
324
|
|
|
$level3Params['id'] = $optionInfo['id']; |
|
325
|
|
|
parent::update($level3Params); |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
break; |
|
332
|
|
|
default: |
|
333
|
|
|
$list = explode(';', $params['field_options']); |
|
334
|
|
|
|
|
335
|
|
|
foreach ($list as $option) { |
|
336
|
|
|
$option_info = $this->repo->getFieldOptionByFieldAndOption( |
|
337
|
|
|
$field_id, |
|
338
|
|
|
$option, |
|
339
|
|
|
$this->extraField->getItemType() |
|
340
|
|
|
); |
|
341
|
|
|
|
|
342
|
|
|
// Use URLify only for new items |
|
343
|
|
|
$optionValue = api_replace_dangerous_char($option); |
|
344
|
|
|
$option = trim($option); |
|
345
|
|
|
|
|
346
|
|
|
if ($option_info) { |
|
|
|
|
|
|
347
|
|
|
continue; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
$order = $this->get_max_order($field_id); |
|
351
|
|
|
|
|
352
|
|
|
$new_params = [ |
|
353
|
|
|
'field_id' => $field_id, |
|
354
|
|
|
'option_value' => trim($optionValue), |
|
355
|
|
|
'display_text' => trim($option), |
|
356
|
|
|
'option_order' => $order, |
|
357
|
|
|
]; |
|
358
|
|
|
parent::save($new_params, $showQuery); |
|
359
|
|
|
} |
|
360
|
|
|
break; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
return true; |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
/** |
|
367
|
|
|
* Save one option item at a time. |
|
368
|
|
|
* |
|
369
|
|
|
* @param array $params Parameters specific to the option |
|
370
|
|
|
* @param bool $show_query Whether to show the query (sent to parent save() method) |
|
371
|
|
|
* @param bool $insert_repeated Whether to insert even if the option already exists |
|
372
|
|
|
* |
|
373
|
|
|
* @return bool True on success, false on failure |
|
374
|
|
|
* @assert (array('field_id'=>0),false) === false |
|
375
|
|
|
* @assert (array('field_id'=>0),false) === true |
|
376
|
|
|
*/ |
|
377
|
|
|
public function save_one_item($params, $show_query = false, $insert_repeated = true) |
|
378
|
|
|
{ |
|
379
|
|
|
$field_id = (int) $params['field_id']; |
|
380
|
|
|
if (empty($field_id)) { |
|
381
|
|
|
return false; |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
if (isset($params['option_value'])) { |
|
385
|
|
|
$params['option_value'] = trim($params['option_value']); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
if (isset($params['display_text'])) { |
|
389
|
|
|
$params['display_text'] = trim($params['display_text']); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
if (empty($params['option_order'])) { |
|
393
|
|
|
$order = $this->get_max_order($field_id); |
|
394
|
|
|
$params['option_order'] = $order; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
if (isset($params['id']) && empty($params['id'])) { |
|
398
|
|
|
unset($params['id']); |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
if ($insert_repeated) { |
|
402
|
|
|
parent::save($params, $show_query); |
|
403
|
|
|
} else { |
|
404
|
|
|
$check = $this->repo->getFieldOptionByFieldAndOption( |
|
405
|
|
|
$field_id, |
|
406
|
|
|
$params['option_value'], |
|
407
|
|
|
$this->extraField->getItemType() |
|
408
|
|
|
); |
|
409
|
|
|
if (!$check) { |
|
|
|
|
|
|
410
|
|
|
parent::save($params, $show_query); |
|
411
|
|
|
} |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
return true; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Get the complete row of a specific option's display text of a specific field. |
|
419
|
|
|
* |
|
420
|
|
|
* @param int $field_id |
|
421
|
|
|
* @param string $option_display_text Display value of the option |
|
422
|
|
|
* |
|
423
|
|
|
* @return mixed The row on success or false on failure |
|
424
|
|
|
* @assert (0, '') === false |
|
425
|
|
|
*/ |
|
426
|
|
|
public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text) |
|
427
|
|
|
{ |
|
428
|
|
|
$field_id = (int) $field_id; |
|
429
|
|
|
$option_display_text = Database::escape_string($option_display_text); |
|
430
|
|
|
$extraFieldType = $this->getExtraField()->getItemType(); |
|
431
|
|
|
|
|
432
|
|
|
$sql = "SELECT s.* FROM {$this->table} s |
|
433
|
|
|
INNER JOIN {$this->tableExtraField} sf |
|
434
|
|
|
ON (s.field_id = sf.id) |
|
435
|
|
|
WHERE |
|
436
|
|
|
field_id = $field_id AND |
|
437
|
|
|
s.display_text = '".$option_display_text."' AND |
|
438
|
|
|
sf.item_type = $extraFieldType |
|
439
|
|
|
"; |
|
440
|
|
|
$result = Database::query($sql); |
|
441
|
|
|
if (Database::num_rows($result) > 0) { |
|
442
|
|
|
return Database::fetch_assoc($result); |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
return false; |
|
446
|
|
|
} |
|
447
|
|
|
|
|
448
|
|
|
/** |
|
449
|
|
|
* Get the complete row of a specific option's display text of a specific field. |
|
450
|
|
|
* |
|
451
|
|
|
* @param int $field_id |
|
452
|
|
|
* @param string $option_display_text Display value of the option |
|
453
|
|
|
* @param string $option_value Value of the option |
|
454
|
|
|
* |
|
455
|
|
|
* @return mixed The row on success or false on failure |
|
456
|
|
|
* @assert (0, '', '') === false |
|
457
|
|
|
*/ |
|
458
|
|
|
public function getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue( |
|
459
|
|
|
$field_id, |
|
460
|
|
|
$option_display_text, |
|
461
|
|
|
$option_value |
|
462
|
|
|
) { |
|
463
|
|
|
$field_id = (int) $field_id; |
|
464
|
|
|
$option_display_text = Database::escape_string($option_display_text); |
|
465
|
|
|
$option_value = Database::escape_string($option_value); |
|
466
|
|
|
$extraFieldType = $this->getExtraField()->getItemType(); |
|
467
|
|
|
|
|
468
|
|
|
$sql = "SELECT s.* FROM {$this->table} s |
|
469
|
|
|
INNER JOIN {$this->tableExtraField} sf |
|
470
|
|
|
ON (s.field_id = sf.id) |
|
471
|
|
|
WHERE |
|
472
|
|
|
field_id = $field_id AND |
|
473
|
|
|
sf.display_text = '".$option_display_text."' AND |
|
474
|
|
|
option_value = '$option_value' AND |
|
475
|
|
|
sf.item_type = ".$extraFieldType." |
|
476
|
|
|
"; |
|
477
|
|
|
$result = Database::query($sql); |
|
478
|
|
|
if (Database::num_rows($result) > 0) { |
|
479
|
|
|
return Database::fetch_assoc($result); |
|
480
|
|
|
} |
|
481
|
|
|
|
|
482
|
|
|
return false; |
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* Gets an array of options for a specific field. |
|
487
|
|
|
* |
|
488
|
|
|
* @param int $field_id The field ID |
|
489
|
|
|
* @param bool $add_id_in_array Whether to add the row ID in the result |
|
490
|
|
|
* @param null $ordered_by Extra ordering query bit |
|
|
|
|
|
|
491
|
|
|
* |
|
492
|
|
|
* @return array The options if they exists. Otherwise return false |
|
493
|
|
|
*/ |
|
494
|
|
|
public function get_field_options_by_field($field_id, $add_id_in_array = false, $ordered_by = null) |
|
495
|
|
|
{ |
|
496
|
|
|
$field_id = (int) $field_id; |
|
497
|
|
|
|
|
498
|
|
|
$orderBy = null; |
|
499
|
|
|
switch ($ordered_by) { |
|
500
|
|
|
case 'id': |
|
501
|
|
|
$orderBy = ['id' => 'ASC']; |
|
502
|
|
|
break; |
|
503
|
|
|
case 'field_id': |
|
504
|
|
|
$orderBy = ['field' => 'ASC']; |
|
505
|
|
|
break; |
|
506
|
|
|
case 'option_value': |
|
507
|
|
|
$orderBy = ['optionValue' => 'ASC']; |
|
508
|
|
|
break; |
|
509
|
|
|
case 'display_text': |
|
510
|
|
|
$orderBy = ['displayText' => 'ASC']; |
|
511
|
|
|
break; |
|
512
|
|
|
case 'priority': |
|
513
|
|
|
$orderBy = ['priority' => 'ASC']; |
|
514
|
|
|
break; |
|
515
|
|
|
case 'priority_message': |
|
516
|
|
|
$orderBy = ['priorityMessage' => 'ASC']; |
|
517
|
|
|
break; |
|
518
|
|
|
case 'option_order': |
|
519
|
|
|
$orderBy = ['optionOrder' => 'ASC']; |
|
520
|
|
|
break; |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
$result = Container::getExtraFieldOptionsRepository()->findBy(['field' => $field_id], $orderBy); |
|
524
|
|
|
|
|
525
|
|
|
if (!$result) { |
|
526
|
|
|
return false; |
|
527
|
|
|
} |
|
528
|
|
|
|
|
529
|
|
|
$options = []; |
|
530
|
|
|
/** @var ExtraFieldOptions $row */ |
|
531
|
|
|
foreach ($result as $row) { |
|
532
|
|
|
$option = [ |
|
533
|
|
|
'id' => $row->getId(), |
|
534
|
|
|
'field_id' => $row->getField()->getId(), |
|
535
|
|
|
'option_value' => $row->getValue(), |
|
536
|
|
|
'display_text' => $row->getDisplayText(), |
|
537
|
|
|
'priority' => $row->getPriority(), |
|
538
|
|
|
'priority_message' => $row->getPriorityMessage(), |
|
539
|
|
|
'option_order' => $row->getOptionOrder(), |
|
540
|
|
|
]; |
|
541
|
|
|
|
|
542
|
|
|
if ($add_id_in_array) { |
|
543
|
|
|
$options[$row->getId()] = $option; |
|
544
|
|
|
continue; |
|
545
|
|
|
} |
|
546
|
|
|
$options[] = $option; |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
return $options; |
|
550
|
|
|
} |
|
551
|
|
|
|
|
552
|
|
|
/** |
|
553
|
|
|
* Get options for a specific field as array or in JSON format suited for the double-select format. |
|
554
|
|
|
* |
|
555
|
|
|
* @param int $option_value_id Option value ID |
|
556
|
|
|
* @param bool $to_json Return format (whether it should be formatted to JSON or not) |
|
557
|
|
|
* |
|
558
|
|
|
* @return mixed Row/JSON on success |
|
559
|
|
|
*/ |
|
560
|
|
|
public function get_second_select_field_options_by_field($option_value_id, $to_json = false) |
|
561
|
|
|
{ |
|
562
|
|
|
$extraFieldOptionsRepo = Container::getExtraFieldOptionsRepository(); |
|
563
|
|
|
$option = $extraFieldOptionsRepo->find($option_value_id); |
|
564
|
|
|
|
|
565
|
|
|
if (!$option) { |
|
566
|
|
|
return !$to_json ? [] : '{}'; |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
$subOptions = $extraFieldOptionsRepo->findSecondaryOptions($option); |
|
570
|
|
|
$optionsInfo = []; |
|
571
|
|
|
|
|
572
|
|
|
/** @var ExtraFieldOptions $subOption */ |
|
573
|
|
|
foreach ($subOptions as $subOption) { |
|
574
|
|
|
$optionsInfo[] = [ |
|
575
|
|
|
'id' => $subOption->getId(), |
|
576
|
|
|
'field_id' => $subOption->getField()->getId(), |
|
577
|
|
|
'option_value' => $subOption->getValue(), |
|
578
|
|
|
'display_text' => $subOption->getDisplayText(), |
|
579
|
|
|
'priority' => $subOption->getPriority(), |
|
580
|
|
|
'priority_message' => $subOption->getPriorityMessage(), |
|
581
|
|
|
'option_order' => $subOption->getOptionOrder(), |
|
582
|
|
|
]; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
if (!$to_json) { |
|
586
|
|
|
return $optionsInfo; |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
$json = []; |
|
590
|
|
|
|
|
591
|
|
|
foreach ($optionsInfo as $optionInfo) { |
|
592
|
|
|
$json[$optionInfo['id']] = $optionInfo['display_text']; |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
return json_encode($json); |
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
/** |
|
599
|
|
|
* Get options for a specific field as string split by ; |
|
600
|
|
|
*/ |
|
601
|
|
|
public function get_field_options_by_field_to_string(int $field_id, string $ordered_by = null): string |
|
602
|
|
|
{ |
|
603
|
|
|
$field = new ExtraField($this->type); |
|
604
|
|
|
$field_info = $field->get($field_id); |
|
605
|
|
|
$options = self::get_field_options_by_field($field_id, false, $ordered_by); |
|
|
|
|
|
|
606
|
|
|
$elements = []; |
|
607
|
|
|
if (!empty($options)) { |
|
608
|
|
|
switch ($field_info['value_type']) { |
|
609
|
|
|
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: |
|
610
|
|
|
$html = ExtraField::extra_field_double_select_convert_array_to_string($options); |
|
611
|
|
|
break; |
|
612
|
|
|
case ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD: |
|
613
|
|
|
$html = ExtraField::extraFieldSelectWithTextConvertArrayToString($options); |
|
614
|
|
|
break; |
|
615
|
|
|
case ExtraField::FIELD_TYPE_TRIPLE_SELECT: |
|
616
|
|
|
$html = ExtraField::tripleSelectConvertArrayToString($options); |
|
617
|
|
|
break; |
|
618
|
|
|
default: |
|
619
|
|
|
foreach ($options as $option) { |
|
620
|
|
|
// If option_value is empty, use display_text |
|
621
|
|
|
$value = !empty($option['option_value']) ? trim($option['option_value']) : trim($option['display_text']); |
|
622
|
|
|
if (!empty($value)) { |
|
623
|
|
|
$elements[] = $value; |
|
624
|
|
|
} |
|
625
|
|
|
} |
|
626
|
|
|
$html = !empty($elements) ? implode(';', $elements) : get_lang("No options available"); |
|
627
|
|
|
break; |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
return $html; |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
return get_lang("No options available"); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
/** |
|
637
|
|
|
* Get the maximum order value for a specific field. |
|
638
|
|
|
* |
|
639
|
|
|
* @param int $field_id |
|
640
|
|
|
* |
|
641
|
|
|
* @return int Current max ID + 1 (we start from 0) |
|
642
|
|
|
* @assert (0, '') === 1 |
|
643
|
|
|
*/ |
|
644
|
|
|
public function get_max_order($field_id) |
|
645
|
|
|
{ |
|
646
|
|
|
$field_id = (int) $field_id; |
|
647
|
|
|
$sql = "SELECT MAX(option_order) |
|
648
|
|
|
FROM {$this->table} |
|
649
|
|
|
WHERE field_id = $field_id"; |
|
650
|
|
|
$res = Database::query($sql); |
|
651
|
|
|
$max = 1; |
|
652
|
|
|
if (Database::num_rows($res) > 0) { |
|
653
|
|
|
$row = Database::fetch_array($res); |
|
654
|
|
|
$max = $row[0] + 1; |
|
655
|
|
|
} |
|
656
|
|
|
|
|
657
|
|
|
return $max; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
/** |
|
661
|
|
|
* Display a form with the options for the field_id given in REQUEST. |
|
662
|
|
|
*/ |
|
663
|
|
|
public function display() |
|
664
|
|
|
{ |
|
665
|
|
|
// action links |
|
666
|
|
|
echo '<div class="actions">'; |
|
667
|
|
|
$field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null; |
|
668
|
|
|
echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'&field_id='.$field_id.'">'. |
|
669
|
|
|
Display::getMdiIcon(ActionIcon::ADD, 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Add')).'</a>'; |
|
670
|
|
|
echo '</div>'; |
|
671
|
|
|
echo Display::grid_html('extra_field_options'); |
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
/** |
|
675
|
|
|
* @return array |
|
676
|
|
|
*/ |
|
677
|
|
|
public function getPriorityOptions() |
|
678
|
|
|
{ |
|
679
|
|
|
return [ |
|
680
|
|
|
'' => get_lang('Please select an option'), |
|
681
|
|
|
1 => get_lang('Success'), |
|
682
|
|
|
2 => get_lang('Information'), |
|
683
|
|
|
3 => get_lang('Warning !'), |
|
684
|
|
|
4 => get_lang('Error'), |
|
685
|
|
|
]; |
|
686
|
|
|
} |
|
687
|
|
|
|
|
688
|
|
|
/** |
|
689
|
|
|
* @param $priority |
|
690
|
|
|
* |
|
691
|
|
|
* @return string|null |
|
692
|
|
|
*/ |
|
693
|
|
|
public function getPriorityMessageType($priority) |
|
694
|
|
|
{ |
|
695
|
|
|
switch ($priority) { |
|
696
|
|
|
case 1: |
|
697
|
|
|
return 'success'; |
|
698
|
|
|
case 2: |
|
699
|
|
|
return 'info'; |
|
700
|
|
|
case 3: |
|
701
|
|
|
return 'warning'; |
|
702
|
|
|
case 4: |
|
703
|
|
|
return 'error'; |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
|
|
return null; |
|
707
|
|
|
} |
|
708
|
|
|
|
|
709
|
|
|
/** |
|
710
|
|
|
* Returns an HTML form for the current field. |
|
711
|
|
|
* |
|
712
|
|
|
* @param string URL to send the form to (action=...) |
|
713
|
|
|
* @param string Type of action to offer through the form (edit, usually) |
|
714
|
|
|
* |
|
715
|
|
|
* @return FormValidator |
|
716
|
|
|
*/ |
|
717
|
|
|
public function return_form($url, $action) |
|
718
|
|
|
{ |
|
719
|
|
|
$form_name = $this->type.'_field'; |
|
720
|
|
|
$form = new FormValidator($form_name, 'post', $url); |
|
721
|
|
|
// Setting the form elements |
|
722
|
|
|
$header = get_lang('Add'); |
|
723
|
|
|
if ('edit' == $action) { |
|
724
|
|
|
$header = get_lang('Edit'); |
|
725
|
|
|
} |
|
726
|
|
|
|
|
727
|
|
|
$form->addElement('header', $header); |
|
728
|
|
|
$id = isset($_GET['id']) ? (int) $_GET['id'] : ''; |
|
729
|
|
|
|
|
730
|
|
|
$form->addElement('hidden', 'id', $id); |
|
731
|
|
|
$form->addElement('hidden', 'type', $this->type); |
|
732
|
|
|
$form->addElement('hidden', 'field_id', $this->fieldId); |
|
733
|
|
|
|
|
734
|
|
|
if ('edit' === $action) { |
|
735
|
|
|
$translateUrl = api_get_path(WEB_CODE_PATH).'extrafield/translate_option.php?'.http_build_query( |
|
736
|
|
|
['id' => $id] |
|
737
|
|
|
); |
|
738
|
|
|
$translateButton = Display::toolbarButton( |
|
739
|
|
|
get_lang('Translate this term'), |
|
740
|
|
|
$translateUrl, |
|
741
|
|
|
'language', |
|
742
|
|
|
'link' |
|
743
|
|
|
); |
|
744
|
|
|
|
|
745
|
|
|
$form->addText( |
|
746
|
|
|
'display_text', |
|
747
|
|
|
[get_lang('Name'), $translateButton] |
|
748
|
|
|
); |
|
749
|
|
|
} else { |
|
750
|
|
|
$form->addElement('text', 'display_text', get_lang('Name')); |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
$form->addElement('text', 'option_value', get_lang('Value')); |
|
754
|
|
|
$form->addElement('text', 'option_order', get_lang('Order')); |
|
755
|
|
|
$form->addSelect('priority', get_lang('Priority'), $this->getPriorityOptions()); |
|
756
|
|
|
$form->addElement('textarea', 'priority_message', get_lang('Message type')); |
|
757
|
|
|
|
|
758
|
|
|
$defaults = []; |
|
759
|
|
|
|
|
760
|
|
|
if ('edit' === $action) { |
|
761
|
|
|
// Setting the defaults |
|
762
|
|
|
$defaults = $this->get($id, false); |
|
763
|
|
|
$form->freeze('option_value'); |
|
764
|
|
|
$form->addButtonUpdate(get_lang('Edit')); |
|
765
|
|
|
} else { |
|
766
|
|
|
$form->addButtonCreate(get_lang('Add')); |
|
767
|
|
|
} |
|
768
|
|
|
|
|
769
|
|
|
$form->setDefaults($defaults); |
|
770
|
|
|
|
|
771
|
|
|
$form->addRule('display_text', get_lang('Required field'), 'required'); |
|
772
|
|
|
$form->addRule('option_value', get_lang('Required field'), 'required'); |
|
773
|
|
|
|
|
774
|
|
|
return $form; |
|
775
|
|
|
} |
|
776
|
|
|
|
|
777
|
|
|
/** |
|
778
|
|
|
* @param string $tag |
|
779
|
|
|
* @param int $field_id |
|
780
|
|
|
* @param int $limit |
|
781
|
|
|
* |
|
782
|
|
|
* @return array |
|
783
|
|
|
*/ |
|
784
|
|
|
public function searchByField($tag, $field_id, $limit = 10) |
|
785
|
|
|
{ |
|
786
|
|
|
$field_id = (int) $field_id; |
|
787
|
|
|
$limit = (int) $limit; |
|
788
|
|
|
$tag = Database::escape_string($tag); |
|
789
|
|
|
|
|
790
|
|
|
$sql = "SELECT DISTINCT id, option_display_text |
|
791
|
|
|
FROM {$this->table} |
|
792
|
|
|
WHERE |
|
793
|
|
|
field_id = '".$field_id."' AND |
|
794
|
|
|
option_value LIKE '%$tag%' |
|
795
|
|
|
ORDER BY option_value |
|
796
|
|
|
LIMIT 0, $limit |
|
797
|
|
|
"; |
|
798
|
|
|
$result = Database::query($sql); |
|
799
|
|
|
$values = []; |
|
800
|
|
|
if (Database::num_rows($result)) { |
|
801
|
|
|
$values = Database::store_result($result, 'ASSOC'); |
|
802
|
|
|
} |
|
803
|
|
|
|
|
804
|
|
|
return $values; |
|
805
|
|
|
} |
|
806
|
|
|
|
|
807
|
|
|
/** |
|
808
|
|
|
* @param string $tag |
|
809
|
|
|
* @param int $field_id |
|
810
|
|
|
* @param int $limit |
|
811
|
|
|
* |
|
812
|
|
|
* @return string |
|
813
|
|
|
*/ |
|
814
|
|
|
public function getSearchOptionsByField($tag, $field_id, $limit = 10) |
|
815
|
|
|
{ |
|
816
|
|
|
$result = $this->searchByField($tag, $field_id, $limit = 10); |
|
817
|
|
|
$values = []; |
|
818
|
|
|
$json = null; |
|
819
|
|
|
if (!empty($result)) { |
|
820
|
|
|
foreach ($result as $item) { |
|
821
|
|
|
$values[] = [ |
|
822
|
|
|
'value' => $item['id'], |
|
823
|
|
|
'caption' => $item['option_display_text'], |
|
824
|
|
|
]; |
|
825
|
|
|
} |
|
826
|
|
|
$json = json_encode($values); |
|
827
|
|
|
} |
|
828
|
|
|
|
|
829
|
|
|
return $json; |
|
830
|
|
|
} |
|
831
|
|
|
|
|
832
|
|
|
/** |
|
833
|
|
|
* Gets an element. |
|
834
|
|
|
* |
|
835
|
|
|
* @param int $id |
|
836
|
|
|
* @param bool $translateDisplayText Optional |
|
837
|
|
|
* |
|
838
|
|
|
* @return array |
|
839
|
|
|
*/ |
|
840
|
|
|
public function get($id, $translateDisplayText = true) |
|
841
|
|
|
{ |
|
842
|
|
|
$info = parent::get($id); |
|
843
|
|
|
|
|
844
|
|
|
if ($info) { |
|
845
|
|
|
$option = Container::getExtraFieldOptionsRepository()->find($id); |
|
846
|
|
|
if (!$translateDisplayText) { |
|
847
|
|
|
$option->setLocale(Container::getParameter('locale')); |
|
848
|
|
|
Database::getManager()->refresh($option); |
|
849
|
|
|
} |
|
850
|
|
|
$info['display_text'] = $option->getDisplayText(); |
|
851
|
|
|
} |
|
852
|
|
|
|
|
853
|
|
|
return $info; |
|
|
|
|
|
|
854
|
|
|
} |
|
855
|
|
|
|
|
856
|
|
|
public function get_all(array $options = []): array |
|
857
|
|
|
{ |
|
858
|
|
|
$result = parent::get_all($options); |
|
859
|
|
|
|
|
860
|
|
|
foreach ($result as &$row) { |
|
861
|
|
|
$option = Container::getExtraFieldOptionsRepository()->find($row['id']); |
|
862
|
|
|
$row['display_text'] = $option->getDisplayText(); |
|
863
|
|
|
} |
|
864
|
|
|
|
|
865
|
|
|
return $result; |
|
866
|
|
|
} |
|
867
|
|
|
|
|
868
|
|
|
/** |
|
869
|
|
|
* @param string $variable |
|
870
|
|
|
* |
|
871
|
|
|
* @return array|ExtraFieldOptions[] |
|
872
|
|
|
*/ |
|
873
|
|
|
public function getOptionsByFieldVariable($variable) |
|
874
|
|
|
{ |
|
875
|
|
|
$extraFieldType = $this->getExtraField()->getItemType(); |
|
876
|
|
|
|
|
877
|
|
|
$dql = "SELECT o FROM ChamiloCoreBundle:ExtraFieldOptions o |
|
878
|
|
|
INNER JOIN ChamiloCoreBundle:ExtraField f WITH o.field = f.id |
|
879
|
|
|
WHERE f.variable = :variable AND f.itemType = :item_type |
|
880
|
|
|
ORDER BY o.value ASC"; |
|
881
|
|
|
|
|
882
|
|
|
$result = Database::getManager() |
|
883
|
|
|
->createQuery($dql) |
|
884
|
|
|
->setParameters(['variable' => $variable, 'item_type' => $extraFieldType]) |
|
885
|
|
|
->getResult(); |
|
886
|
|
|
|
|
887
|
|
|
return $result; |
|
888
|
|
|
} |
|
889
|
|
|
} |
|
890
|
|
|
|