Skip to content

Commit 2727617

Browse files
committed
Extrafield: Use ExtraFieldValues and ExtraFieldOptions repository to get translated options from select fields - refs BT#22627
1 parent 41336bc commit 2727617

File tree

8 files changed

+121
-90
lines changed

8 files changed

+121
-90
lines changed

public/main/inc/lib/extra_field.lib.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,6 +3151,7 @@ public function getDataAndFormattedValues($itemId, $filter = false, $onlyShow =
31513151
);
31523152
}
31533153
break;
3154+
case self::FIELD_TYPE_SELECT:
31543155
case self::FIELD_TYPE_SELECT_MULTIPLE:
31553156
$displayedValue = $valueData['value'] ?? $valueData['field_value'];
31563157
break;

public/main/inc/lib/extra_field_option.lib.php

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
66
use Chamilo\CoreBundle\Framework\Container;
77
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
8+
use Chamilo\CoreBundle\Repository\ExtraFieldOptionsRepository;
89

910
/**
1011
* Handles the extra fields for various objects (users, sessions, courses).
@@ -25,6 +26,9 @@ class ExtraFieldOption extends Model
2526
public $extraField;
2627
public $fieldId;
2728

29+
/** @var ExtraFieldOptionsRepository */
30+
public $repo;
31+
2832
/**
2933
* Gets the table for the type of object for which we are using an extra field.
3034
*
@@ -38,6 +42,7 @@ public function __construct($type)
3842
$this->extraField = $extraField;
3943
$this->table = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
4044
$this->tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
45+
$this->repo = Database::getManager()->getRepository(ExtraFieldOptions::class);
4146
}
4247

4348
/**
@@ -128,12 +133,13 @@ public function delete_all_options_by_field_id($field_id)
128133
*/
129134
public function saveOptions($params, $showQuery = false)
130135
{
131-
$optionInfo = $this->get_field_option_by_field_and_option(
132-
$params['field_id'],
133-
$params['option_value']
136+
$optionInfo = $this->repo->getFieldOptionByFieldAndOption(
137+
(int) $params['field_id'],
138+
(string) $params['option_value'],
139+
$this->extraField->getItemType()
134140
);
135141

136-
if (false == $optionInfo) {
142+
if (!$optionInfo) {
137143
$optionValue = api_replace_dangerous_char($params['option_value']);
138144
$order = $this->get_max_order($params['field_id']);
139145
$newParams = [
@@ -327,13 +333,17 @@ public function save($params, $showQuery = false)
327333
$list = explode(';', $params['field_options']);
328334

329335
foreach ($list as $option) {
330-
$option_info = $this->get_field_option_by_field_and_option($field_id, $option);
336+
$option_info = $this->repo->getFieldOptionByFieldAndOption(
337+
$field_id,
338+
$option,
339+
$this->extraField->getItemType()
340+
);
331341

332342
// Use URLify only for new items
333343
$optionValue = api_replace_dangerous_char($option);
334344
$option = trim($option);
335345

336-
if (false != $option_info) {
346+
if ($option_info) {
337347
continue;
338348
}
339349

@@ -366,7 +376,7 @@ public function save($params, $showQuery = false)
366376
*/
367377
public function save_one_item($params, $show_query = false, $insert_repeated = true)
368378
{
369-
$field_id = intval($params['field_id']);
379+
$field_id = (int) $params['field_id'];
370380
if (empty($field_id)) {
371381
return false;
372382
}
@@ -391,49 +401,19 @@ public function save_one_item($params, $show_query = false, $insert_repeated = t
391401
if ($insert_repeated) {
392402
parent::save($params, $show_query);
393403
} else {
394-
$check = $this->get_field_option_by_field_and_option(
404+
$check = $this->repo->getFieldOptionByFieldAndOption(
395405
$field_id,
396-
$params['option_value']
406+
$params['option_value'],
407+
$this->extraField->getItemType()
397408
);
398-
if (false == $check) {
409+
if (!$check) {
399410
parent::save($params, $show_query);
400411
}
401412
}
402413

403414
return true;
404415
}
405416

406-
/**
407-
* Get the complete row of a specific option of a specific field.
408-
*
409-
* @param int $field_id
410-
* @param string $option_value Value of the option
411-
*
412-
* @return mixed The row on success or false on failure
413-
* @assert (0,'') === false
414-
*/
415-
public function get_field_option_by_field_and_option($field_id, $option_value)
416-
{
417-
$field_id = (int) $field_id;
418-
$option_value = Database::escape_string($option_value);
419-
$extraFieldType = $this->getExtraField()->getItemType();
420-
421-
$sql = "SELECT s.* FROM {$this->table} s
422-
INNER JOIN {$this->tableExtraField} sf
423-
ON (s.field_id = sf.id)
424-
WHERE
425-
field_id = $field_id AND
426-
option_value = '".$option_value."' AND
427-
sf.item_type = $extraFieldType
428-
";
429-
$result = Database::query($sql);
430-
if (Database::num_rows($result) > 0) {
431-
return Database::store_result($result, 'ASSOC');
432-
}
433-
434-
return false;
435-
}
436-
437417
/**
438418
* Get the complete row of a specific option's display text of a specific field.
439419
*

public/main/inc/lib/extra_field_value.lib.php

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
use Chamilo\CoreBundle\Entity\Asset;
66
use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
7+
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
78
use Chamilo\CoreBundle\Entity\ExtraFieldRelTag;
89
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
910
use Chamilo\CoreBundle\Entity\Tag;
1011
use Chamilo\CoreBundle\Framework\Container;
12+
use Chamilo\CoreBundle\Repository\ExtraFieldOptionsRepository;
13+
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
1114
use ChamiloSession as Session;
15+
use Doctrine\ORM\Exception\NotSupported;
1216
use Symfony\Component\HttpFoundation\File\UploadedFile;
1317

1418
/**
@@ -563,23 +567,39 @@ public function save($params, $showQuery = false)
563567
*
564568
* @return mixed A structured array with the field_id and field_value, or false on error
565569
* @assert (-1,-1) === false
570+
*
571+
* @throws NotSupported
566572
*/
567573
public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
568574
{
569-
$field_id = (int) $field_id;
570-
$item_id = Database::escape_string($item_id);
575+
/** @var ExtraFieldValuesRepository $efvRepo */
576+
$efvRepo = Database::getManager()->getRepository(ExtraFieldValues::class);
577+
/** @var ExtraFieldOptionsRepository $efoRepo */
578+
$efoRepo = Database::getManager()->getRepository(ExtraFieldOptions::class);
579+
580+
$results = $efvRepo->getByHandlerAndFieldId(
581+
(int) $item_id,
582+
(int) $field_id,
583+
$this->getExtraField()->getItemType(),
584+
$transform
585+
);
586+
587+
if ($results) {
588+
/** @var ExtraFieldValues $efv */
589+
$efv = current($results);
590+
591+
$result = [
592+
'id' => $efv->getId(),
593+
'field_id' => $efv->getField()->getId(),
594+
'asset_id' => $efv->getAsset()?->getId(),
595+
'field_value' => $efv->getFieldValue(),
596+
'item_id' => $efv->getItemId(),
597+
'comment' => $efv->getComment(),
598+
'created_at' => $efv->getCreatedAt()->format('Y-m-d H:i:s'),
599+
'updated_at' => $efv->getUpdatedAt()->format('Y-m-d H:i:s'),
600+
'value_type' => $efv->getField()->getValueType(),
601+
];
571602

572-
$sql = "SELECT s.*, value_type FROM {$this->table} s
573-
INNER JOIN {$this->table_handler_field} sf
574-
ON (s.field_id = sf.id)
575-
WHERE
576-
item_id = '$item_id' AND
577-
field_id = $field_id AND
578-
sf.item_type = ".$this->getExtraField()->getItemType()."
579-
ORDER BY id";
580-
$result = Database::query($sql);
581-
if (Database::num_rows($result)) {
582-
$result = Database::fetch_assoc($result);
583603
if ($transform) {
584604
if (!empty($result['field_value'])) {
585605
switch ($result['value_type']) {
@@ -613,14 +633,14 @@ public function get_values_by_handler_and_field_id($item_id, $field_id, $transfo
613633
$result['value'] = implode(' <br /> ', $optionValues);
614634
break;
615635
case ExtraField::FIELD_TYPE_SELECT:
616-
$field_option = new ExtraFieldOption($this->type);
617-
$extra_field_option_result = $field_option->get_field_option_by_field_and_option(
618-
$result['field_id'],
619-
$result['field_value']
636+
$extra_field_option_result = $efoRepo->getFieldOptionByFieldAndOption(
637+
(int) $result['field_id'],
638+
(string) $result['field_value'],
639+
$this->getExtraField()->getItemType()
620640
);
621641

622-
if (isset($extra_field_option_result[0])) {
623-
$result['value'] = $extra_field_option_result[0]['display_text'];
642+
if ($extra_field_option_result) {
643+
$result['value'] = $extra_field_option_result[0]->getDisplayText();
624644
}
625645

626646
break;
@@ -1166,7 +1186,7 @@ public function getAllValuesForAnItem($itemId, $visibleToSelf = null, $visibleTo
11661186
->getQuery()
11671187
->getResult();
11681188

1169-
$fieldOptionsRepo = $em->getRepository(\Chamilo\CoreBundle\Entity\ExtraFieldOptions::class);
1189+
$fieldOptionsRepo = $em->getRepository(ExtraFieldOptions::class);
11701190

11711191
$valueList = [];
11721192
/** @var ExtraFieldValues $fieldValue */

public/main/inc/lib/social.lib.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,17 +1385,17 @@ public static function getExtraFieldBlock($user_id, $isArray = false)
13851385
case ExtraField::FIELD_TYPE_RADIO:
13861386
$objEfOption = new ExtraFieldOption('user');
13871387
$value = $data['extra_'.$extraFieldInfo['variable']];
1388-
$optionInfo = $objEfOption->get_field_option_by_field_and_option(
1389-
$extraFieldInfo['id'],
1390-
$value
1388+
$optionInfo = $objEfOption->repo->getFieldOptionByFieldAndOption(
1389+
(int) $extraFieldInfo['id'],
1390+
$value,
1391+
$objEfOption->extraField->getItemType()
13911392
);
13921393

1393-
if ($optionInfo && isset($optionInfo[0])) {
1394-
$optionInfo = $optionInfo[0];
1394+
if (!empty($optionInfo[0])) {
13951395
$extraFieldItem = [
13961396
'variable' => $extraFieldInfo['variable'],
13971397
'label' => ucfirst($extraFieldInfo['display_text']),
1398-
'value' => $optionInfo['display_text'],
1398+
'value' => $optionInfo[0]->getDisplayText(),
13991399
];
14001400
} else {
14011401
$extraFieldItem = [
@@ -1420,7 +1420,11 @@ public static function getExtraFieldBlock($user_id, $isArray = false)
14201420
switch ($extraFieldInfo['value_type']) {
14211421
case ExtraField::FIELD_TYPE_RADIO:
14221422
$objEfOption = new ExtraFieldOption('user');
1423-
$optionInfo = $objEfOption->get_field_option_by_field_and_option($extraFieldInfo['id'], $extraFieldInfo['value']);
1423+
$optionInfo = $objEfOption->repo->getFieldOptionByFieldAndOption(
1424+
(int) $extraFieldInfo['id'],
1425+
$extraFieldInfo['value'],
1426+
$objEfOption->extraField->getItemType()
1427+
);
14241428
break;
14251429
case ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
14261430
case ExtraField::FIELD_TYPE_GEOLOCALIZATION:

public/main/user/user.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/* For licensing terms, see /license.txt */
44

5+
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
56
use Chamilo\CoreBundle\Entity\Session;
67
use Chamilo\CoreBundle\Framework\Container;
78
use Chamilo\CoreBundle\Component\Utils\ActionIcon;
@@ -932,13 +933,18 @@ function get_user_data($from, $number_of_items, $column, $direction)
932933
$extraField['id']
933934
);
934935
if (isset($data['value'])) {
935-
$optionList = $extraFieldOption->get_field_option_by_field_and_option(
936-
$extraField['id'],
937-
$data['value']
936+
$optionList = $extraFieldOption->repo->getFieldOptionByFieldAndOption(
937+
(int) $extraField['id'],
938+
$data['value'],
939+
$extraFieldOption->extraField->getItemType()
938940
);
939-
if (!empty($optionList)) {
940-
$options = implode(', ', array_column($optionList, 'display_text'));
941-
$temp[] = Security::remove_XSS($options);
941+
942+
if ($optionList) {
943+
$optionList = array_map(
944+
fn (ExtraFieldOptions $option) => Security::remove_XSS($option['display_text']),
945+
$optionList
946+
);
947+
$temp[] = implode(', ', $optionList);
942948
} else {
943949
$temp[] = Security::remove_XSS($data['value']);
944950
}

src/CoreBundle/Controller/SocialController.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Chamilo\CoreBundle\Controller;
88

99
use Chamilo\CoreBundle\Entity\ExtraField;
10+
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
1011
use Chamilo\CoreBundle\Entity\Legal;
1112
use Chamilo\CoreBundle\Entity\Message;
1213
use Chamilo\CoreBundle\Entity\MessageAttachment;
@@ -545,12 +546,18 @@ private function getExtraFieldBlock(
545546
switch ($extraField['type']) {
546547
case ExtraField::FIELD_TYPE_RADIO:
547548
case ExtraField::FIELD_TYPE_SELECT:
548-
$extraFieldOptions = $extraFieldOptionsRepository->getFieldOptionByFieldAndOption($extraField['id'], $fieldValue, ExtraField::USER_FIELD_TYPE);
549+
$extraFieldOptions = $extraFieldOptionsRepository->getFieldOptionByFieldAndOption(
550+
$extraField['id'],
551+
$fieldValue,
552+
ExtraField::USER_FIELD_TYPE
553+
);
549554
if (!empty($extraFieldOptions)) {
550-
$optionTexts = array_map(function ($option) {
551-
return $option['display_text'];
552-
}, $extraFieldOptions);
555+
$optionTexts = array_map(
556+
fn (ExtraFieldOptions $option) => $option['display_text'],
557+
$extraFieldOptions
558+
);
553559
$fieldValue = implode(', ', $optionTexts);
560+
$fieldValue = $extraFieldOptions->getDisplayText();
554561
}
555562

556563
break;

src/CoreBundle/Repository/ExtraFieldOptionsRepository.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function findSecondaryOptions(ExtraFieldOptions $option): array
4040
;
4141
}
4242

43+
/**
44+
* Get the complete row of a specific option for a specific field.
45+
*
46+
* @return array<int, ExtraFieldOptions>
47+
*/
4348
public function getFieldOptionByFieldAndOption(int $fieldId, string $optionValue, int $itemType): array
4449
{
4550
$qb = $this->createQueryBuilder('o');
@@ -54,17 +59,6 @@ public function getFieldOptionByFieldAndOption(int $fieldId, string $optionValue
5459
])
5560
;
5661

57-
$result = $qb->getQuery()->getResult();
58-
59-
$options = [];
60-
foreach ($result as $option) {
61-
$options[] = [
62-
'id' => $option->getId(),
63-
'value' => $option->getValue(),
64-
'display_text' => $option->getDisplayText(),
65-
];
66-
}
67-
68-
return $options;
62+
return $qb->getQuery()->getResult();
6963
}
7064
}

src/CoreBundle/Repository/ExtraFieldValuesRepository.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,23 @@ public function getLpIdWithDaysForCompletion(): array
198198

199199
return $qb->getQuery()->getResult();
200200
}
201+
202+
public function getByHandlerAndFieldId(int $itemId, int $fieldId, int $itemType, bool $transform = false): array
203+
{
204+
$qb = $this->createQueryBuilder('efv');
205+
206+
return $qb
207+
->innerJoin('efv.field', 'ef')
208+
->where($qb->expr()->eq('efv.itemId', ':item_id'))
209+
->andWhere($qb->expr()->eq('efv.field', ':field_id'))
210+
->andWhere($qb->expr()->eq('ef.itemType', ':item_type'))
211+
->setParameters([
212+
'item_id' => $itemId,
213+
'field_id' => $fieldId,
214+
'item_type' => $itemType,
215+
])
216+
->getQuery()
217+
->getResult()
218+
;
219+
}
201220
}

0 commit comments

Comments
 (0)