extra_field_option.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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' => $optionValue,
  193. 'option_display_text' => $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. $params['tms'] = api_get_utc_datetime();
  220. if (empty($params['option_order'])) {
  221. $order = self::get_max_order($field_id);
  222. $params['option_order'] = $order;
  223. }
  224. if ($insert_repeated) {
  225. parent::save($params, $show_query);
  226. } else {
  227. $check = self::get_field_option_by_field_and_option($field_id, $params['option_value']);
  228. if ($check == false) {
  229. parent::save($params, $show_query);
  230. }
  231. }
  232. return true;
  233. }
  234. /**
  235. * Get the complete row of a specific option of a specific field
  236. * @param int Field ID
  237. * @param string Value of the option
  238. * @return mixed The row on success or false on failure
  239. * @assert (0,'') === false
  240. */
  241. public function get_field_option_by_field_and_option($field_id, $option_value)
  242. {
  243. $field_id = intval($field_id);
  244. $option_value = Database::escape_string($option_value);
  245. $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_value = '".$option_value."'";
  246. $result = Database::query($sql);
  247. if (Database::num_rows($result) > 0) {
  248. return Database::store_result($result, 'ASSOC');
  249. }
  250. return false;
  251. }
  252. /**
  253. * Get the complete row of a specific option's display text of a specific field
  254. * @param int Field ID
  255. * @param string Display value of the option
  256. * @return mixed The row on success or false on failure
  257. * @assert (0, '') === false
  258. */
  259. public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text)
  260. {
  261. $field_id = intval($field_id);
  262. $option_display_text = Database::escape_string($option_display_text);
  263. $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."'";
  264. $result = Database::query($sql);
  265. if (Database::num_rows($result) > 0) {
  266. return Database::fetch_array($result, 'ASSOC');
  267. }
  268. return false;
  269. }
  270. /**
  271. * Get the complete row of a specific option's display text of a specific field
  272. * @param int Field ID
  273. * @param string Display value of the option
  274. * @param string Value of the option
  275. * @return mixed The row on success or false on failure
  276. * @assert (0, '', '') === false
  277. */
  278. public function get_field_option_by_field_id_and_option_display_text_and_option_value(
  279. $field_id,
  280. $option_display_text,
  281. $option_value
  282. ) {
  283. $field_id = intval($field_id);
  284. $option_display_text = Database::escape_string($option_display_text);
  285. $option_value = Database::escape_string($option_value);
  286. $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."' AND option_value = '$option_value'";
  287. $result = Database::query($sql);
  288. if (Database::num_rows($result) > 0) {
  289. return Database::fetch_array($result, 'ASSOC');
  290. }
  291. return false;
  292. }
  293. /**
  294. * Gets an array of options for a specific field
  295. * @param int The field ID
  296. * @param bool Whether to add the row ID in the result
  297. * @param string Extra ordering query bit
  298. * @result mixed Row on success, false on failure
  299. * @assert (0, '') === false
  300. */
  301. public function get_field_options_by_field($field_id, $add_id_in_array = false, $ordered_by = null)
  302. {
  303. $field_id = intval($field_id);
  304. $sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id ";
  305. if (!empty($ordered_by)) {
  306. $sql .= " ORDER BY $ordered_by ";
  307. }
  308. $result = Database::query($sql);
  309. if (Database::num_rows($result) > 0) {
  310. if ($add_id_in_array) {
  311. $options = array();
  312. while ($row = Database::fetch_array($result, 'ASSOC')) {
  313. $options[$row['id']] = $row;
  314. }
  315. return $options;
  316. } else {
  317. return Database::store_result($result, 'ASSOC');
  318. }
  319. }
  320. return false;
  321. }
  322. /**
  323. * Get options for a specific field as array or in JSON format suited for the double-select format
  324. * @param int Field ID
  325. * @param int Option value ID
  326. * @param bool Return format (whether it should be formatted to JSON or not)
  327. * @return mixed Row/JSON on success
  328. */
  329. public function get_second_select_field_options_by_field($field_id, $option_value_id, $to_json = false)
  330. {
  331. $field_id = intval($field_id);
  332. $option_value_id = intval($option_value_id);
  333. $options = array();
  334. $sql = "SELECT * FROM {$this->table}
  335. WHERE field_id = $field_id AND option_value = $option_value_id
  336. ORDER BY option_display_text";
  337. $result = Database::query($sql);
  338. if (Database::num_rows($result) > 0) {
  339. $options = Database::store_result($result, 'ASSOC');
  340. }
  341. if ($to_json) {
  342. $string = null;
  343. if (!empty($options)) {
  344. $array = array();
  345. foreach ($options as $option) {
  346. $array[$option['id']] = $option['option_display_text'];
  347. }
  348. $string = json_encode($array);
  349. }
  350. return $string;
  351. }
  352. return $options;
  353. }
  354. /**
  355. * Get options for a specific field as string split by ;
  356. * @param int Field ID
  357. * @param string Extra query bit for reordering
  358. * @return string HTML string of options
  359. * @assert (0, '') === null
  360. */
  361. public function get_field_options_by_field_to_string($field_id, $ordered_by = null)
  362. {
  363. $field = new ExtraField($this->type);
  364. $field_info = $field->get($field_id);
  365. $options = self::get_field_options_by_field($field_id, false, $ordered_by);
  366. $elements = array();
  367. if (!empty($options)) {
  368. switch ($field_info['field_type']) {
  369. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  370. $html = ExtraField::extra_field_double_select_convert_array_to_string($options);
  371. break;
  372. default:
  373. foreach ($options as $option) {
  374. $elements[] = $option['option_value'];
  375. }
  376. $html = implode(';', $elements);
  377. break;
  378. }
  379. return $html;
  380. }
  381. return null;
  382. }
  383. /**
  384. * Get the maximum order value for a specific field
  385. * @param int Field ID
  386. * @return int Current max ID + 1 (we start from 0)
  387. * @assert (0, '') === 1
  388. */
  389. public function get_max_order($field_id)
  390. {
  391. $field_id = intval($field_id);
  392. $sql = "SELECT MAX(option_order) FROM {$this->table} WHERE field_id = $field_id";
  393. $res = Database::query($sql);
  394. $max = 1;
  395. if (Database::num_rows($res) > 0) {
  396. $row = Database::fetch_array($res);
  397. $max = $row[0] + 1;
  398. }
  399. return $max;
  400. }
  401. /**
  402. * Update the option using the given params
  403. * @param array Parameters with the data to be saved
  404. */
  405. public function update($params)
  406. {
  407. parent::update($params);
  408. }
  409. /**
  410. * Display a form with the options for the field_id given in REQUEST
  411. * @return void Prints output
  412. */
  413. function display()
  414. {
  415. // action links
  416. echo '<div class="actions">';
  417. //echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'),'', ICON_SIZE_MEDIUM).'</a>';
  418. $field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null;
  419. echo '<a href="'.api_get_self(
  420. ).'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.Display::return_icon(
  421. 'add_user_fields.png',
  422. get_lang('Add'),
  423. '',
  424. ICON_SIZE_MEDIUM
  425. ).'</a>';
  426. echo '</div>';
  427. echo Display::grid_html('extra_field_options');
  428. }
  429. public function getPriorityOptions()
  430. {
  431. return array(
  432. '' => get_lang('SelectAnOption'),
  433. 1 => get_lang('Success'),
  434. 2 => get_lang('Info'),
  435. 3 => get_lang('Warning'),
  436. 4 => get_lang('Error'),
  437. );
  438. }
  439. public function getPriorityMessageType($priority)
  440. {
  441. switch ($priority) {
  442. case 1:
  443. return 'success';
  444. case 2:
  445. return 'info';
  446. case 3:
  447. return 'warning';
  448. case 4:
  449. return 'error';
  450. }
  451. return null;
  452. }
  453. /**
  454. * Returns an HTML form for the current field
  455. * @param string URL to send the form to (action=...)
  456. * @param string Type of action to offer through the form (edit, usually)
  457. * @return string HTML form
  458. */
  459. public function return_form($url, $action)
  460. {
  461. $form_name = $this->type.'_field';
  462. $form = new FormValidator($form_name, 'post', $url);
  463. // Settting the form elements
  464. $header = get_lang('Add');
  465. if ($action == 'edit') {
  466. $header = get_lang('Modify');
  467. }
  468. $form->addElement('header', $header);
  469. $id = isset($_GET['id']) ? intval($_GET['id']) : '';
  470. $form->addElement('hidden', 'id', $id);
  471. $form->addElement('hidden', 'type', $this->type);
  472. $form->addElement('hidden', 'field_id', $this->field_id);
  473. $form->addElement('text', 'option_display_text', get_lang('Name'), array('class' => 'span5'));
  474. $form->addElement('text', 'option_value', get_lang('Value'), array('class' => 'span5'));
  475. $form->addElement('text', 'option_order', get_lang('Order'), array('class' => 'span2'));
  476. $form->addElement('select', 'priority', get_lang('Priority'), $this->getPriorityOptions());
  477. $form->addElement('textarea', 'priority_message', get_lang('PriorityMessage'));
  478. $defaults = array();
  479. if ($action == 'edit') {
  480. // Setting the defaults
  481. $defaults = $this->get($id);
  482. $form->freeze('option_value');
  483. $form->addElement('button', 'submit', get_lang('Modify'), 'class="save"');
  484. } else {
  485. $form->addElement('button', 'submit', get_lang('Add'), 'class="save"');
  486. }
  487. $form->setDefaults($defaults);
  488. // Setting the rules
  489. $form->addRule('option_display_text', get_lang('ThisFieldIsRequired'), 'required');
  490. //$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
  491. $form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required');
  492. return $form;
  493. }
  494. }