extra_field_option.lib.php 19 KB

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