extra_field_option.lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Declaration of the ExtraFieldOption class
  5. */
  6. /**
  7. * Handles the extra fields for various objects (users, sessions, courses)
  8. */
  9. class ExtraFieldOption extends Model
  10. {
  11. public $columns = array(
  12. 'id', 'field_id', 'option_value', 'option_display_text', 'option_order', 'priority', 'priority_message', 'tms'
  13. );
  14. /**
  15. * Gets the table for the type of object for which we are using an extra field
  16. * @param string Type of object (course, user or session)
  17. */
  18. public function __construct($type)
  19. {
  20. $this->type = $type;
  21. switch ($this->type) {
  22. case 'course':
  23. $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_OPTIONS);
  24. break;
  25. case 'user':
  26. $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
  27. break;
  28. case 'session':
  29. $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS);
  30. break;
  31. case 'question':
  32. $this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS);
  33. break;
  34. case 'lp':
  35. $this->table = Database::get_main_table(TABLE_MAIN_LP_FIELD_OPTIONS);
  36. break;
  37. }
  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 Field ID
  52. * @return int Number of options
  53. * @assert ('') === false
  54. * @assert (-1) == 0
  55. * @assert (0) == 0
  56. */
  57. public function get_count_by_field_id($field_id)
  58. {
  59. if (empty($field_id)) {
  60. return false;
  61. }
  62. $row = Database::select(
  63. 'count(*) as count',
  64. $this->table,
  65. array('where' => array('field_id = ?' => $field_id)),
  66. 'first'
  67. );
  68. return $row['count'];
  69. }
  70. /**
  71. * Returns a list of options for a specific field, separated by ";"
  72. * @param int Field ID
  73. * @param bool Indicates whether we want the results to be given with their id
  74. * @param string Order by clause (without the "order by") to be added to the SQL query
  75. * @return string List of options separated by ;
  76. * @assert (-1, false, null) == ''
  77. */
  78. public function get_field_options_to_string($field_id, $add_id_in_array = false, $ordered_by = null)
  79. {
  80. $options = self::get_field_options_by_field($field_id, $add_id_in_array, $ordered_by);
  81. $new_options = array();
  82. if (!empty($options)) {
  83. foreach ($options as $option) {
  84. $new_options[] = $option['option_value'].':'.$option['option_display_text'];
  85. }
  86. $string = implode(';', $new_options);
  87. return $string;
  88. }
  89. return '';
  90. }
  91. /**
  92. * Delete all the options of a specific field
  93. * @param int Field ID
  94. * @result void
  95. * @assert (-1) === false
  96. */
  97. public function delete_all_options_by_field_id($field_id)
  98. {
  99. $field_id = intval($field_id);
  100. $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
  101. $r = Database::query($sql);
  102. return $r;
  103. }
  104. /**
  105. * @param array $params
  106. *
  107. * @return int
  108. */
  109. public function saveOptions($params, $show_query = false)
  110. {
  111. $optionInfo = self::get_field_option_by_field_and_option($params['field_id'], $params['option_value']);
  112. // Use URLify only for new items
  113. //$optionValue = URLify::filter($params['option_value']);
  114. $optionValue = replace_dangerous_char($params['option_value']);
  115. $option = $params['option_value'];
  116. if ($optionInfo == false) {
  117. $order = self::get_max_order($params['field_id']);
  118. $new_params = array(
  119. 'field_id' => $params['field_id'],
  120. 'option_value' => trim($optionValue),
  121. 'option_display_text' => trim($option),
  122. 'option_order' => $order,
  123. 'tms' => api_get_utc_datetime(),
  124. );
  125. return parent::save($new_params, $show_query);
  126. }
  127. return false;
  128. }
  129. /**
  130. * Saves an option into the corresponding *_field_options table
  131. * @param array Parameters to be considered for the insertion
  132. * @param bool Whether to show the query (sent to the parent save() method)
  133. * @return bool True on success, false on error
  134. * @assert (array('field_id'=>0), false) === false
  135. * @assert (array('field_id'=>1), false) === true
  136. */
  137. public function save($params, $show_query = false)
  138. {
  139. $field_id = intval($params['field_id']);
  140. if (empty($field_id)) {
  141. return false;
  142. }
  143. $time = api_get_utc_datetime();
  144. if (!empty($params['field_options']) &&
  145. in_array(
  146. $params['field_type'],
  147. array(
  148. ExtraField::FIELD_TYPE_RADIO,
  149. ExtraField::FIELD_TYPE_SELECT,
  150. ExtraField::FIELD_TYPE_SELECT_MULTIPLE,
  151. ExtraField::FIELD_TYPE_DOUBLE_SELECT
  152. )
  153. )
  154. ) {
  155. if ($params['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  156. //$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
  157. $options_parsed = ExtraField::extra_field_double_select_convert_string_to_array(
  158. $params['field_options']
  159. );
  160. if (!empty($options_parsed)) {
  161. foreach ($options_parsed as $key => $option) {
  162. $sub_options = $option['options'];
  163. $new_params = array(
  164. 'field_id' => $field_id,
  165. 'option_value' => 0,
  166. 'option_display_text' => $option['label'],
  167. 'option_order' => 0,
  168. 'tms' => $time,
  169. );
  170. // Looking if option already exists:
  171. $option_info = self::get_field_option_by_field_id_and_option_display_text(
  172. $field_id,
  173. $option['label']
  174. );
  175. if (empty($option_info)) {
  176. $sub_id = parent::save($new_params, $show_query);
  177. } else {
  178. $sub_id = $option_info['id'];
  179. $new_params['id'] = $sub_id;
  180. parent::update($new_params, $show_query);
  181. }
  182. foreach ($sub_options as $sub_option) {
  183. if (!empty($sub_option)) {
  184. $new_params = array(
  185. 'field_id' => $field_id,
  186. 'option_value' => $sub_id,
  187. 'option_display_text' => $sub_option,
  188. 'option_order' => 0,
  189. 'tms' => $time,
  190. );
  191. $option_info = self::get_field_option_by_field_id_and_option_display_text_and_option_value(
  192. $field_id,
  193. $sub_option,
  194. $sub_id
  195. );
  196. if (empty($option_info)) {
  197. parent::save($new_params, $show_query);
  198. } else {
  199. $new_params['id'] = $option_info['id'];
  200. parent::update($new_params, $show_query);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. $list = array();
  207. } else {
  208. $list = explode(';', $params['field_options']);
  209. }
  210. if (!empty($list)) {
  211. foreach ($list as $option) {
  212. $option_info = self::get_field_option_by_field_and_option($field_id, $option);
  213. // Use URLify only for new items
  214. $optionValue = URLify::filter($option);
  215. $option = trim($option);
  216. if ($option_info == false) {
  217. $order = self::get_max_order($field_id);
  218. $new_params = array(
  219. 'field_id' => $field_id,
  220. 'option_value' => trim($optionValue),
  221. 'option_display_text' => trim($option),
  222. 'option_order' => $order,
  223. 'tms' => $time,
  224. );
  225. parent::save($new_params, $show_query);
  226. }
  227. }
  228. }
  229. }
  230. return true;
  231. }
  232. /**
  233. * Save one option item at a time
  234. * @param array Parameters specific to the option
  235. * @param bool Whether to show the query (sent to parent save() method)
  236. * @param bool Whether to insert even if the option already exists
  237. * @return bool True on success, false on failure
  238. * @assert (array('field_id'=>0),false) === false
  239. * @assert (array('field_id'=>0),false) === true
  240. */
  241. public function save_one_item($params, $show_query = false, $insert_repeated = true)
  242. {
  243. $field_id = intval($params['field_id']);
  244. if (empty($field_id)) {
  245. return false;
  246. }
  247. if (isset($params['option_value'])) {
  248. $params['option_value'] = trim($params['option_value']);
  249. }
  250. if (isset($params['option_display_text'])) {
  251. $params['option_display_text'] = trim($params['option_display_text']);
  252. }
  253. $params['tms'] = api_get_utc_datetime();
  254. if (empty($params['option_order'])) {
  255. $order = self::get_max_order($field_id);
  256. $params['option_order'] = $order;
  257. }
  258. if ($insert_repeated) {
  259. parent::save($params, $show_query);
  260. } else {
  261. $check = self::get_field_option_by_field_and_option($field_id, $params['option_value']);
  262. if ($check == false) {
  263. parent::save($params, $show_query);
  264. }
  265. }
  266. return true;
  267. }
  268. /**
  269. * Get the complete row of a specific option of a specific field
  270. * @param int Field ID
  271. * @param string Value of the option
  272. * @return mixed The row on success or false on failure
  273. * @assert (0,'') === false
  274. */
  275. public function get_field_option_by_field_and_option($field_id, $option_value)
  276. {
  277. $field_id = intval($field_id);
  278. $option_value = Database::escape_string($option_value);
  279. $sql = "SELECT * FROM {$this->table}
  280. WHERE field_id = $field_id AND option_value = '".$option_value."'";
  281. $result = Database::query($sql);
  282. if (Database::num_rows($result) > 0) {
  283. return Database::store_result($result, 'ASSOC');
  284. }
  285. return false;
  286. }
  287. /**
  288. * Get the complete row of a specific option's display text of a specific field
  289. * @param int Field ID
  290. * @param string Display value of the option
  291. * @return mixed The row on success or false on failure
  292. * @assert (0, '') === false
  293. */
  294. public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text)
  295. {
  296. $field_id = intval($field_id);
  297. $option_display_text = Database::escape_string($option_display_text);
  298. $sql = "SELECT * FROM {$this->table}
  299. WHERE field_id = $field_id AND option_display_text = '".$option_display_text."'";
  300. $result = Database::query($sql);
  301. if (Database::num_rows($result) > 0) {
  302. return Database::fetch_array($result, 'ASSOC');
  303. }
  304. return false;
  305. }
  306. /**
  307. * Get the complete row of a specific option's display text of a specific field
  308. * @param int Field ID
  309. * @param string Display value of the option
  310. * @param string Value of the option
  311. * @return mixed The row on success or false on failure
  312. * @assert (0, '', '') === false
  313. */
  314. public function get_field_option_by_field_id_and_option_display_text_and_option_value(
  315. $field_id,
  316. $option_display_text,
  317. $option_value
  318. ) {
  319. $field_id = intval($field_id);
  320. $option_display_text = Database::escape_string($option_display_text);
  321. $option_value = Database::escape_string($option_value);
  322. $sql = "SELECT * FROM {$this->table}
  323. WHERE
  324. field_id = $field_id AND
  325. option_display_text = '".$option_display_text."' AND
  326. option_value = '$option_value'";
  327. $result = Database::query($sql);
  328. if (Database::num_rows($result) > 0) {
  329. return Database::fetch_array($result, 'ASSOC');
  330. }
  331. return false;
  332. }
  333. /**
  334. * Gets an array of options for a specific field
  335. * @param int The field ID
  336. * @param bool Whether to add the row ID in the result
  337. * @param string Extra ordering query bit
  338. * @result mixed Row on success, false on failure
  339. * @assert (0, '') === false
  340. */
  341. public function get_field_options_by_field($field_id, $add_id_in_array = false, $ordered_by = null)
  342. {
  343. $field_id = intval($field_id);
  344. $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id ";
  345. if (!empty($ordered_by)) {
  346. $sql .= " ORDER BY $ordered_by ";
  347. }
  348. $result = Database::query($sql);
  349. if (Database::num_rows($result) > 0) {
  350. if ($add_id_in_array) {
  351. $options = array();
  352. while ($row = Database::fetch_array($result, 'ASSOC')) {
  353. $options[$row['id']] = $row;
  354. }
  355. return $options;
  356. } else {
  357. return Database::store_result($result, 'ASSOC');
  358. }
  359. }
  360. return false;
  361. }
  362. /**
  363. * Get options for a specific field as array or in JSON format suited for the double-select format
  364. * @param int Field ID
  365. * @param int Option value ID
  366. * @param bool Return format (whether it should be formatted to JSON or not)
  367. * @return mixed Row/JSON on success
  368. */
  369. public function get_second_select_field_options_by_field($field_id, $option_value_id, $to_json = false)
  370. {
  371. $field_id = intval($field_id);
  372. $option_value_id = intval($option_value_id);
  373. $options = array();
  374. $sql = "SELECT * FROM {$this->table}
  375. WHERE field_id = $field_id AND option_value = $option_value_id
  376. ORDER BY option_display_text";
  377. $result = Database::query($sql);
  378. if (Database::num_rows($result) > 0) {
  379. $options = Database::store_result($result, 'ASSOC');
  380. }
  381. if ($to_json) {
  382. $string = null;
  383. if (!empty($options)) {
  384. $array = array();
  385. foreach ($options as $option) {
  386. $array[$option['id']] = $option['option_display_text'];
  387. }
  388. $string = json_encode($array);
  389. }
  390. return $string;
  391. }
  392. return $options;
  393. }
  394. /**
  395. * Get options for a specific field as string split by ;
  396. * @param int Field ID
  397. * @param string Extra query bit for reordering
  398. * @return string HTML string of options
  399. * @assert (0, '') === null
  400. */
  401. public function get_field_options_by_field_to_string($field_id, $ordered_by = null)
  402. {
  403. $field = new ExtraField($this->type);
  404. $field_info = $field->get($field_id);
  405. $options = self::get_field_options_by_field($field_id, false, $ordered_by);
  406. $elements = array();
  407. if (!empty($options)) {
  408. switch ($field_info['field_type']) {
  409. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  410. $html = ExtraField::extra_field_double_select_convert_array_to_string($options);
  411. break;
  412. default:
  413. foreach ($options as $option) {
  414. $elements[] = $option['option_value'];
  415. }
  416. $html = implode(';', $elements);
  417. break;
  418. }
  419. return $html;
  420. }
  421. return null;
  422. }
  423. /**
  424. * Get the maximum order value for a specific field
  425. * @param int Field ID
  426. * @return int Current max ID + 1 (we start from 0)
  427. * @assert (0, '') === 1
  428. */
  429. public function get_max_order($field_id)
  430. {
  431. $field_id = intval($field_id);
  432. $sql = "SELECT MAX(option_order) FROM {$this->table} WHERE field_id = $field_id";
  433. $res = Database::query($sql);
  434. $max = 1;
  435. if (Database::num_rows($res) > 0) {
  436. $row = Database::fetch_array($res);
  437. $max = $row[0] + 1;
  438. }
  439. return $max;
  440. }
  441. /**
  442. * Update the option using the given params
  443. * @param array $params data to be saved
  444. */
  445. public function update($params)
  446. {
  447. parent::update($params);
  448. }
  449. /**
  450. * Display a form with the options for the field_id given in REQUEST
  451. * @return void Prints output
  452. */
  453. function display()
  454. {
  455. // action links
  456. echo '<div class="actions">';
  457. //echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'),'', ICON_SIZE_MEDIUM).'</a>';
  458. $field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null;
  459. echo '<a href="'.api_get_self(
  460. ).'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.Display::return_icon(
  461. 'add_user_fields.png',
  462. get_lang('Add'),
  463. '',
  464. ICON_SIZE_MEDIUM
  465. ).'</a>';
  466. echo '</div>';
  467. echo Display::grid_html('extra_field_options');
  468. }
  469. public function getPriorityOptions()
  470. {
  471. return array(
  472. '' => get_lang('SelectAnOption'),
  473. 1 => get_lang('Success'),
  474. 2 => get_lang('Info'),
  475. 3 => get_lang('Warning'),
  476. 4 => get_lang('Error'),
  477. );
  478. }
  479. public function getPriorityMessageType($priority)
  480. {
  481. switch ($priority) {
  482. case 1:
  483. return 'success';
  484. case 2:
  485. return 'info';
  486. case 3:
  487. return 'warning';
  488. case 4:
  489. return 'error';
  490. }
  491. return null;
  492. }
  493. /**
  494. * Returns an HTML form for the current field
  495. * @param string URL to send the form to (action=...)
  496. * @param string Type of action to offer through the form (edit, usually)
  497. * @return string HTML form
  498. */
  499. public function return_form($url, $action)
  500. {
  501. $form_name = $this->type.'_field';
  502. $form = new FormValidator($form_name, 'post', $url);
  503. // Settting the form elements
  504. $header = get_lang('Add');
  505. if ($action == 'edit') {
  506. $header = get_lang('Modify');
  507. }
  508. $form->addElement('header', $header);
  509. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  510. $form->addElement('hidden', 'id', $id);
  511. $form->addElement('hidden', 'type', $this->type);
  512. $form->addElement('hidden', 'field_id', $this->field_id);
  513. $form->addElement('text', 'option_display_text', get_lang('Name'), array('class' => 'span5'));
  514. $form->addElement('text', 'option_value', get_lang('Value'), array('class' => 'span5'));
  515. $form->addElement('text', 'option_order', get_lang('Order'), array('class' => 'span2'));
  516. $form->addElement('select', 'priority', get_lang('Priority'), $this->getPriorityOptions());
  517. $form->addElement('textarea', 'priority_message', get_lang('PriorityMessage'));
  518. $defaults = array();
  519. if ($action == 'edit') {
  520. // Setting the defaults
  521. $defaults = $this->get($id);
  522. $form->freeze('option_value');
  523. $form->addElement('button', 'submit', get_lang('Modify'), 'class="save"');
  524. } else {
  525. $form->addElement('button', 'submit', get_lang('Add'), 'class="save"');
  526. }
  527. $form->setDefaults($defaults);
  528. // Setting the rules
  529. $form->addRule('option_display_text', get_lang('ThisFieldIsRequired'), 'required');
  530. //$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
  531. $form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required');
  532. return $form;
  533. }
  534. /**
  535. * @param string $tag
  536. * @param int $field_id
  537. * @param int $limit
  538. * @return array
  539. */
  540. public function searchByField($tag, $field_id, $limit = 10)
  541. {
  542. $field_id = intval($field_id);
  543. $limit = intval($limit);
  544. $tag = Database::escape_string($tag);
  545. $sql = "SELECT DISTINCT id, option_display_text
  546. FROM {$this->table}
  547. WHERE
  548. field_id = '".$field_id."' AND
  549. option_value LIKE '%$tag%'
  550. ORDER BY option_value
  551. LIMIT 0, $limit
  552. ";
  553. $result = Database::query($sql);
  554. $values = array();
  555. if (Database::num_rows($result)) {
  556. $values = Database::store_result($result, 'ASSOC');
  557. }
  558. return $values;
  559. }
  560. /**
  561. * @param string $tag
  562. * @param int $field_id
  563. * @param int $limit
  564. *
  565. * @return string
  566. */
  567. public function getSearchOptionsByField($tag, $field_id, $limit = 10)
  568. {
  569. $result = $this->searchByField($tag, $field_id, $limit = 10);
  570. $values = array();
  571. $json = null;
  572. if (!empty($result)) {
  573. foreach ($result as $item) {
  574. $values[] = array(
  575. 'value' => $item['id'],
  576. 'caption' => $item['option_display_text'],
  577. );
  578. }
  579. $json = json_encode($values);
  580. }
  581. return $json;
  582. }
  583. }