extra_field_option.lib.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ExtraFieldOption
  5. * Handles the extra fields for various objects (users, sessions, courses)
  6. */
  7. class ExtraFieldOption extends Model
  8. {
  9. public $columns = array(
  10. 'id',
  11. 'field_id',
  12. 'option_value',
  13. 'display_text',
  14. 'option_order',
  15. 'priority',
  16. 'priority_message',
  17. 'tms'
  18. );
  19. public $extraField;
  20. /**
  21. * Gets the table for the type of object for which we are using an extra field
  22. * @param string $type Type of object (course, user or session)
  23. */
  24. public function __construct($type)
  25. {
  26. $this->type = $type;
  27. $extraField = new ExtraField($this->type);
  28. $this->extraField = $extraField;
  29. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD_OPTIONS);
  30. $this->tableExtraField = Database::get_main_table(TABLE_EXTRA_FIELD);
  31. }
  32. /**
  33. * @return ExtraField
  34. */
  35. public function getExtraField()
  36. {
  37. return $this->extraField;
  38. }
  39. /**
  40. * Gets the number of options already available in the table for this item type
  41. * @return int Number of options available
  42. * @assert () >= 0
  43. */
  44. /*public function get_count()
  45. {
  46. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  47. return $row['count'];
  48. }*/
  49. /**
  50. * Gets the number of options available for this field
  51. * @param int $fieldId
  52. *
  53. * @return int Number of options
  54. * @assert ('') === false
  55. * @assert (-1) == 0
  56. * @assert (0) == 0
  57. */
  58. public function get_count_by_field_id($fieldId)
  59. {
  60. if (empty($fieldId)) {
  61. return false;
  62. }
  63. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  64. $fieldId = intval($fieldId);
  65. $sql = "SELECT count(*) as count
  66. FROM $this->table o INNER JOIN $this->tableExtraField e
  67. ON o.field_id = e.id
  68. WHERE
  69. o.field_id = $fieldId AND
  70. e.extra_field_type = ".$extraFieldType."
  71. ";
  72. $result = Database::query($sql);
  73. $result = Database::fetch_array($result);
  74. return $result['count'];
  75. }
  76. /**
  77. * Returns a list of options for a specific field, separated by ";"
  78. * @param int $field_id
  79. * @param bool $add_id_in_array Indicates whether we want the results to be given with their id
  80. * @param string $ordered_by Order by clause (without the "order by") to be added to the SQL query
  81. * @return string List of options separated by ;
  82. * @assert (-1, false, null) == ''
  83. */
  84. public function getFieldOptionsToString($field_id, $add_id_in_array = false, $ordered_by = null)
  85. {
  86. $options = self::get_field_options_by_field($field_id, $add_id_in_array, $ordered_by);
  87. $new_options = array();
  88. if (!empty($options)) {
  89. foreach ($options as $option) {
  90. $new_options[] = $option['option_value'].':'.$option['display_text'];
  91. }
  92. $string = implode(';', $new_options);
  93. return $string;
  94. }
  95. return '';
  96. }
  97. /**
  98. * Delete all the options of a specific field
  99. * @param int $field_id
  100. *
  101. * @result void
  102. * @assert (-1) === false
  103. */
  104. public function delete_all_options_by_field_id($field_id)
  105. {
  106. $field_id = intval($field_id);
  107. $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
  108. $r = Database::query($sql);
  109. return $r;
  110. }
  111. /**
  112. * @param array $params
  113. * @param bool $showQuery
  114. *
  115. * @return int
  116. */
  117. public function saveOptions($params, $showQuery = false)
  118. {
  119. $optionInfo = self::get_field_option_by_field_and_option(
  120. $params['field_id'],
  121. $params['option_value']
  122. );
  123. if ($optionInfo == false) {
  124. $optionValue = api_replace_dangerous_char($params['option_value']);
  125. $order = self::get_max_order($params['field_id']);
  126. $newParams = array(
  127. 'field_id' => $params['field_id'],
  128. 'value' => trim($optionValue),
  129. 'display_text' => trim($params['display_text']),
  130. 'option_order' => $order
  131. );
  132. return parent::save($newParams, $showQuery);
  133. }
  134. return false;
  135. }
  136. /**
  137. * Saves an option into the corresponding *_field_options table
  138. * @param array $params Parameters to be considered for the insertion
  139. * @param bool $showQuery Whether to show the query (sent to the parent save() method)
  140. *
  141. * @return bool True on success, false on error
  142. * @assert (array('field_id'=>0), false) === false
  143. * @assert (array('field_id'=>1), false) === true
  144. */
  145. public function save($params, $showQuery = false)
  146. {
  147. $field_id = intval($params['field_id']);
  148. if (empty($field_id)) {
  149. return false;
  150. }
  151. $time = api_get_utc_datetime();
  152. if (!empty($params['field_options']) &&
  153. in_array(
  154. $params['field_type'],
  155. array(
  156. ExtraField::FIELD_TYPE_RADIO,
  157. ExtraField::FIELD_TYPE_SELECT,
  158. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  159. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  160. )
  161. )
  162. ) {
  163. if ($params['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  164. //$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  165. $options_parsed = ExtraField::extra_field_double_select_convert_string_to_array(
  166. $params['field_options']
  167. );
  168. if (!empty($options_parsed)) {
  169. foreach ($options_parsed as $key => $option) {
  170. $sub_options = $option['options'];
  171. $new_params = array(
  172. 'field_id' => $field_id,
  173. 'option_value' => 0,
  174. 'display_text' => $option['label'],
  175. 'option_order' => 0,
  176. );
  177. // Looking if option already exists:
  178. $option_info = self::get_field_option_by_field_id_and_option_display_text(
  179. $field_id,
  180. $option['label']
  181. );
  182. if (empty($option_info)) {
  183. $sub_id = parent::save($new_params, $showQuery);
  184. } else {
  185. $sub_id = $option_info['id'];
  186. $new_params['id'] = $sub_id;
  187. parent::update($new_params, $showQuery);
  188. }
  189. foreach ($sub_options as $sub_option) {
  190. if (!empty($sub_option)) {
  191. $new_params = array(
  192. 'field_id' => $field_id,
  193. 'option_value' => $sub_id,
  194. 'display_text' => $sub_option,
  195. 'option_order' => 0,
  196. );
  197. $option_info = self::getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue(
  198. $field_id,
  199. $sub_option,
  200. $sub_id
  201. );
  202. if (empty($option_info)) {
  203. parent::save($new_params, $showQuery);
  204. } else {
  205. $new_params['id'] = $option_info['id'];
  206. parent::update($new_params, $showQuery);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. $list = array();
  213. } else {
  214. $list = explode(';', $params['field_options']);
  215. }
  216. if (!empty($list)) {
  217. foreach ($list as $option) {
  218. $option_info = self::get_field_option_by_field_and_option($field_id, $option);
  219. // Use URLify only for new items
  220. $optionValue = api_replace_dangerous_char($option);
  221. $option = trim($option);
  222. if ($option_info == false) {
  223. $order = self::get_max_order($field_id);
  224. $new_params = array(
  225. 'field_id' => $field_id,
  226. 'option_value' => trim($optionValue),
  227. 'display_text' => trim($option),
  228. 'option_order' => $order,
  229. );
  230. parent::save($new_params, $showQuery);
  231. }
  232. }
  233. }
  234. }
  235. return true;
  236. }
  237. /**
  238. * Save one option item at a time
  239. * @param array $params Parameters specific to the option
  240. * @param bool $show_query Whether to show the query (sent to parent save() method)
  241. * @param bool $insert_repeated Whether to insert even if the option already exists
  242. * @return bool True on success, false on failure
  243. * @assert (array('field_id'=>0),false) === false
  244. * @assert (array('field_id'=>0),false) === true
  245. */
  246. public function save_one_item($params, $show_query = false, $insert_repeated = true)
  247. {
  248. $field_id = intval($params['field_id']);
  249. if (empty($field_id)) {
  250. return false;
  251. }
  252. if (isset($params['option_value'])) {
  253. $params['option_value'] = trim($params['option_value']);
  254. }
  255. if (isset($params['display_text'])) {
  256. $params['display_text'] = trim($params['display_text']);
  257. }
  258. if (empty($params['option_order'])) {
  259. $order = self::get_max_order($field_id);
  260. $params['option_order'] = $order;
  261. }
  262. if ($insert_repeated) {
  263. parent::save($params, $show_query);
  264. } else {
  265. $check = self::get_field_option_by_field_and_option(
  266. $field_id,
  267. $params['option_value']
  268. );
  269. if ($check == false) {
  270. parent::save($params, $show_query);
  271. }
  272. }
  273. return true;
  274. }
  275. /**
  276. * Get the complete row of a specific option of a specific field
  277. * @param int $field_id
  278. * @param string $option_value Value of the option
  279. * @return mixed The row on success or false on failure
  280. * @assert (0,'') === false
  281. */
  282. public function get_field_option_by_field_and_option($field_id, $option_value)
  283. {
  284. $field_id = intval($field_id);
  285. $option_value = Database::escape_string($option_value);
  286. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  287. $sql = "SELECT s.* FROM {$this->table} s
  288. INNER JOIN {$this->tableExtraField} sf
  289. ON (s.field_id = sf.id)
  290. WHERE
  291. field_id = $field_id AND
  292. option_value = '".$option_value."' AND
  293. sf.extra_field_type = ".$extraFieldType."
  294. ";
  295. $result = Database::query($sql);
  296. if (Database::num_rows($result) > 0) {
  297. return Database::store_result($result, 'ASSOC');
  298. }
  299. return false;
  300. }
  301. /**
  302. * Get the complete row of a specific option's display text of a specific field
  303. * @param int $field_id
  304. * @param string $option_display_text Display value of the option
  305. * @return mixed The row on success or false on failure
  306. * @assert (0, '') === false
  307. */
  308. public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text)
  309. {
  310. $field_id = intval($field_id);
  311. $option_display_text = Database::escape_string($option_display_text);
  312. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  313. $sql = "SELECT s.* FROM {$this->table} s
  314. INNER JOIN {$this->tableExtraField} sf
  315. ON (s.field_id = sf.id)
  316. WHERE
  317. field_id = $field_id AND
  318. s.display_text = '".$option_display_text."' AND
  319. sf.extra_field_type = ".$extraFieldType."
  320. ";
  321. $result = Database::query($sql);
  322. if (Database::num_rows($result) > 0) {
  323. return Database::fetch_array($result, 'ASSOC');
  324. }
  325. return false;
  326. }
  327. /**
  328. * Get the complete row of a specific option's display text of a specific field
  329. * @param int $field_id
  330. * @param string $option_display_text Display value of the option
  331. * @param string $option_value Value of the option
  332. *
  333. * @return mixed The row on success or false on failure
  334. * @assert (0, '', '') === false
  335. */
  336. public function getFieldOptionByFieldIdAndOptionDisplayTextAndOptionValue(
  337. $field_id,
  338. $option_display_text,
  339. $option_value
  340. ) {
  341. $field_id = intval($field_id);
  342. $option_display_text = Database::escape_string($option_display_text);
  343. $option_value = Database::escape_string($option_value);
  344. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  345. $sql = "SELECT s.* FROM {$this->table} s
  346. INNER JOIN {$this->tableExtraField} sf
  347. ON (s.field_id = sf.id)
  348. WHERE
  349. field_id = $field_id AND
  350. sf.display_text = '".$option_display_text."' AND
  351. option_value = '$option_value' AND
  352. sf.extra_field_type = ".$extraFieldType."
  353. ";
  354. $result = Database::query($sql);
  355. if (Database::num_rows($result) > 0) {
  356. return Database::fetch_array($result, 'ASSOC');
  357. }
  358. return false;
  359. }
  360. /**
  361. * Gets an array of options for a specific field
  362. * @param int $field_id The field ID
  363. * @param bool $add_id_in_array Whether to add the row ID in the result
  364. * @param null $ordered_by Extra ordering query bit
  365. * @return array The options if they exists. Otherwise return false
  366. */
  367. public function get_field_options_by_field($field_id, $add_id_in_array = false, $ordered_by = null)
  368. {
  369. $field_id = intval($field_id);
  370. $orderBy = null;
  371. switch ($ordered_by) {
  372. case 'id':
  373. $orderBy = ['id' => 'ASC'];
  374. break;
  375. case 'field_id':
  376. $orderBy = ['fieldId' => 'ASC'];
  377. break;
  378. case 'option_value':
  379. $orderBy = ['optionValue' => 'ASC'];
  380. break;
  381. case 'display_text':
  382. $orderBy = ['displayText' => 'ASC'];
  383. break;
  384. case 'priority':
  385. $orderBy = ['priority' => 'ASC'];
  386. break;
  387. case 'priority_message':
  388. $orderBy = ['priorityMessage' => 'ASC'];
  389. break;
  390. case 'option_order':
  391. $orderBy = ['optionOrder' => 'ASC'];
  392. break;
  393. }
  394. $result = Database::getManager()
  395. ->getRepository('ChamiloCoreBundle:ExtraFieldOptions')
  396. ->findBy(['field' => $field_id], $orderBy);
  397. if (!$result) {
  398. return false;
  399. }
  400. $options = [];
  401. foreach ($result as $row) {
  402. $option = [
  403. 'id' => $row->getId(),
  404. 'field_id' => $row->getField()->getId(),
  405. 'option_value' => $row->getValue(),
  406. 'display_text' => $row->getDisplayText(),
  407. 'priority' => $row->getPriority(),
  408. 'priority_message' => $row->getPriorityMessage(),
  409. 'option_order' => $row->getOptionOrder()
  410. ];
  411. if ($add_id_in_array) {
  412. $options[$row->getId()] = $option;
  413. continue;
  414. }
  415. $options[] = $option;
  416. }
  417. return $options;
  418. }
  419. /**
  420. * Get options for a specific field as array or in JSON format suited for the double-select format
  421. * @param int $field_id
  422. * @param int $option_value_id Option value ID
  423. * @param bool $to_json Return format (whether it should be formatted to JSON or not)
  424. * @return mixed Row/JSON on success
  425. */
  426. public function get_second_select_field_options_by_field($field_id, $option_value_id, $to_json = false)
  427. {
  428. $field_id = intval($field_id);
  429. $option_value_id = intval($option_value_id);
  430. $options = array();
  431. $sql = "SELECT * FROM {$this->table}
  432. WHERE field_id = $field_id AND option_value = $option_value_id
  433. ORDER BY display_text";
  434. $result = Database::query($sql);
  435. if (Database::num_rows($result) > 0) {
  436. $options = Database::store_result($result, 'ASSOC');
  437. }
  438. if ($to_json) {
  439. $string = null;
  440. if (!empty($options)) {
  441. $array = array();
  442. foreach ($options as $option) {
  443. $array[$option['id']] = $option['display_text'];
  444. }
  445. $string = json_encode($array);
  446. }
  447. return $string;
  448. }
  449. return $options;
  450. }
  451. /**
  452. * Get options for a specific field as string split by ;
  453. * @param int $field_id
  454. * @param string $ordered_by Extra query bit for reordering
  455. * @return string HTML string of options
  456. * @assert (0, '') === null
  457. */
  458. public function get_field_options_by_field_to_string($field_id, $ordered_by = null)
  459. {
  460. $field = new ExtraField($this->type);
  461. $field_info = $field->get($field_id);
  462. $options = self::get_field_options_by_field($field_id, false, $ordered_by);
  463. $elements = array();
  464. if (!empty($options)) {
  465. switch ($field_info['field_type']) {
  466. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  467. $html = ExtraField::extra_field_double_select_convert_array_to_string($options);
  468. break;
  469. default:
  470. foreach ($options as $option) {
  471. $elements[] = $option['option_value'];
  472. }
  473. $html = implode(';', $elements);
  474. break;
  475. }
  476. return $html;
  477. }
  478. return null;
  479. }
  480. /**
  481. * Get the maximum order value for a specific field
  482. * @param int $field_id
  483. * @return int Current max ID + 1 (we start from 0)
  484. * @assert (0, '') === 1
  485. */
  486. public function get_max_order($field_id)
  487. {
  488. $field_id = intval($field_id);
  489. $sql = "SELECT MAX(option_order)
  490. FROM {$this->table}
  491. WHERE field_id = $field_id";
  492. $res = Database::query($sql);
  493. $max = 1;
  494. if (Database::num_rows($res) > 0) {
  495. $row = Database::fetch_array($res);
  496. $max = $row[0] + 1;
  497. }
  498. return $max;
  499. }
  500. /**
  501. * Update the option using the given params
  502. * @param array $params data to be saved
  503. */
  504. public function update($params)
  505. {
  506. parent::update($params);
  507. }
  508. /**
  509. * Display a form with the options for the field_id given in REQUEST
  510. * @return void Prints output
  511. */
  512. public function display()
  513. {
  514. // action links
  515. echo '<div class="actions">';
  516. $field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null;
  517. echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.
  518. Display::return_icon('add_user_fields.png', get_lang('Add'), '', ICON_SIZE_MEDIUM ).'</a>';
  519. echo '</div>';
  520. echo Display::grid_html('extra_field_options');
  521. }
  522. /**
  523. * @return array
  524. */
  525. public function getPriorityOptions()
  526. {
  527. return array(
  528. '' => get_lang('SelectAnOption'),
  529. 1 => get_lang('Success'),
  530. 2 => get_lang('Info'),
  531. 3 => get_lang('Warning'),
  532. 4 => get_lang('Error'),
  533. );
  534. }
  535. /**
  536. * @param $priority
  537. * @return null|string
  538. */
  539. public function getPriorityMessageType($priority)
  540. {
  541. switch ($priority) {
  542. case 1:
  543. return 'success';
  544. case 2:
  545. return 'info';
  546. case 3:
  547. return 'warning';
  548. case 4:
  549. return 'error';
  550. }
  551. return null;
  552. }
  553. /**
  554. * Returns an HTML form for the current field
  555. * @param string URL to send the form to (action=...)
  556. * @param string Type of action to offer through the form (edit, usually)
  557. *
  558. * @return FormValidator
  559. */
  560. public function return_form($url, $action)
  561. {
  562. $form_name = $this->type.'_field';
  563. $form = new FormValidator($form_name, 'post', $url);
  564. // Setting the form elements
  565. $header = get_lang('Add');
  566. if ($action == 'edit') {
  567. $header = get_lang('Modify');
  568. }
  569. $form->addElement('header', $header);
  570. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  571. $form->addElement('hidden', 'id', $id);
  572. $form->addElement('hidden', 'type', $this->type);
  573. $form->addElement('hidden', 'field_id', $this->field_id);
  574. if ($action == 'edit') {
  575. $translateUrl = api_get_path(WEB_CODE_PATH) . 'extrafield/translate.php?' . http_build_query([
  576. 'extra_field_option' => $id
  577. ]);
  578. $translateButton = Display::toolbarButton(get_lang('TranslateThisTerm'), $translateUrl, 'language', 'link');
  579. $form->addText(
  580. 'display_text',
  581. [get_lang('Name'), $translateButton]
  582. );
  583. } else {
  584. $form->addElement('text', 'display_text', get_lang('Name'));
  585. }
  586. $form->addElement('text', 'option_value', get_lang('Value'));
  587. $form->addElement('text', 'option_order', get_lang('Order'));
  588. $form->addElement('select', 'priority', get_lang('Priority'), $this->getPriorityOptions());
  589. $form->addElement('textarea', 'priority_message', get_lang('PriorityOfMessage'));
  590. $defaults = array();
  591. if ($action == 'edit') {
  592. // Setting the defaults
  593. $defaults = $this->get($id, false);
  594. $form->freeze('option_value');
  595. $form->addButtonUpdate(get_lang('Modify'));
  596. } else {
  597. $form->addButtonCreate(get_lang('Add'));
  598. }
  599. $form->setDefaults($defaults);
  600. // Setting the rules
  601. $form->addRule('display_text', get_lang('ThisFieldIsRequired'), 'required');
  602. $form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required');
  603. return $form;
  604. }
  605. /**
  606. * @param string $tag
  607. * @param int $field_id
  608. * @param int $limit
  609. * @return array
  610. */
  611. public function searchByField($tag, $field_id, $limit = 10)
  612. {
  613. $field_id = intval($field_id);
  614. $limit = intval($limit);
  615. $tag = Database::escape_string($tag);
  616. $sql = "SELECT DISTINCT id, option_display_text
  617. FROM {$this->table}
  618. WHERE
  619. field_id = '".$field_id."' AND
  620. option_value LIKE '%$tag%'
  621. ORDER BY option_value
  622. LIMIT 0, $limit
  623. ";
  624. $result = Database::query($sql);
  625. $values = array();
  626. if (Database::num_rows($result)) {
  627. $values = Database::store_result($result, 'ASSOC');
  628. }
  629. return $values;
  630. }
  631. /**
  632. * @param string $tag
  633. * @param int $field_id
  634. * @param int $limit
  635. *
  636. * @return string
  637. */
  638. public function getSearchOptionsByField($tag, $field_id, $limit = 10)
  639. {
  640. $result = $this->searchByField($tag, $field_id, $limit = 10);
  641. $values = array();
  642. $json = null;
  643. if (!empty($result)) {
  644. foreach ($result as $item) {
  645. $values[] = array(
  646. 'value' => $item['id'],
  647. 'caption' => $item['option_display_text'],
  648. );
  649. }
  650. $json = json_encode($values);
  651. }
  652. return $json;
  653. }
  654. /**
  655. * Gets an element
  656. * @param int $id
  657. * @param bool $translateDisplayText Optional
  658. * @return array
  659. */
  660. public function get($id, $translateDisplayText = true)
  661. {
  662. $info = parent::get($id);
  663. if ($translateDisplayText) {
  664. $info['display_text'] = self::translateDisplayName($info['display_text']);
  665. }
  666. return $info;
  667. }
  668. /**
  669. * Translate the display text for a extra field option
  670. * @param string $defaultDisplayText
  671. * @return string
  672. */
  673. public static function translateDisplayName($defaultDisplayText)
  674. {
  675. $camelCase = api_underscore_to_camel_case($defaultDisplayText);
  676. $camelCase = ucwords($camelCase);
  677. $camelCase = str_replace(' ', '', $camelCase);
  678. return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $defaultDisplayText;
  679. }
  680. /**
  681. * Get the info from an extra field option by its id
  682. * @param int $id
  683. * @param bool $translateDisplayText
  684. * @return array
  685. * @throws \Doctrine\ORM\ORMException
  686. * @throws \Doctrine\ORM\OptimisticLockException
  687. * @throws \Doctrine\ORM\TransactionRequiredException
  688. */
  689. public static function getInfoById($id, $translateDisplayText = true)
  690. {
  691. $extraField = Database::getManager()
  692. ->find('ChamiloCoreBundle:ExtraFieldOptions', $id);
  693. $objExtraField = null;
  694. switch ($extraField->getField()->getExtraFieldType()) {
  695. case \Chamilo\CoreBundle\Entity\ExtraField::USER_FIELD_TYPE:
  696. $objExtraField = new self('user');
  697. break;
  698. case \Chamilo\CoreBundle\Entity\ExtraField::COURSE_FIELD_TYPE:
  699. $objExtraField = new self('course');
  700. break;
  701. case \Chamilo\CoreBundle\Entity\ExtraField::SESSION_FIELD_TYPE:
  702. $objExtraField = new self('session');
  703. break;
  704. }
  705. if (!$objExtraField) {
  706. return [];
  707. }
  708. return $objExtraField->get($extraField->getId(), $translateDisplayText);
  709. }
  710. /**
  711. * @param null $options
  712. * @return array
  713. */
  714. public function get_all($options = null)
  715. {
  716. $result = parent::get_all($options);
  717. foreach ($result as &$row) {
  718. $row['display_text'] = self::translateDisplayName($row['display_text']);
  719. }
  720. return $result;
  721. }
  722. }