extra_field_option.lib.php 20 KB

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