extra_field_option.lib.php 23 KB

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