extra_field_value.lib.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /**
  3. * Declaration for the ExtraFieldValue class, managing the values in extra
  4. * fields for any datatype
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Class managing the values in extra fields for any datatype
  9. * @package chamilo.library.extrafields
  10. */
  11. class ExtraFieldValue extends Model {
  12. public $type = null;
  13. public $columns = array('id', 'field_id', 'field_value', 'tms');
  14. public $handler_id = null;//session_id, course_code, user_id
  15. /**
  16. * Formats the necessary elements for the given datatype
  17. * @param string The type of data to which this extra field applies (user, course, session, ...)
  18. * @return void (or false if unmanaged datatype)
  19. * @assert (-1) === false
  20. */
  21. public function __construct($type) {
  22. $this->type = $type;
  23. $extra_field = new ExtraField($this->type);
  24. $this->handler_id = $extra_field->handler_id;
  25. switch ($this->type) {
  26. case 'course':
  27. $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  28. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  29. break;
  30. case 'user':
  31. $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  32. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  33. break;
  34. case 'session':
  35. $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  36. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  37. break;
  38. default:
  39. //unmanaged datatype, return false to let the caller know it
  40. // didn't work
  41. return false;
  42. }
  43. $this->columns[] = $this->handler_id;
  44. }
  45. /**
  46. * Gets the number of values stored in the table (all fields together)
  47. * for this type of resource
  48. * @return integer Number of rows in the table
  49. * @assert () !== false
  50. */
  51. public function get_count() {
  52. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  53. return $row['count'];
  54. }
  55. /**
  56. * Saves a series of records given as parameter into the coresponding table
  57. * @param array Structured parameter for the insertion into the *_field_values table
  58. * @return mixed false on empty params, void otherwise
  59. * @assert (array()) === false
  60. */
  61. public function save_field_values($params) {
  62. $extra_field = new ExtraField($this->type);
  63. if (empty($params[$this->handler_id])) {
  64. return false;
  65. }
  66. //Parse params
  67. foreach ($params as $key => $value) {
  68. if (substr($key, 0, 6) == 'extra_') { //an extra field
  69. $field_variable = substr($key, 6);
  70. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable);
  71. if ($extra_field_info) {
  72. $new_params = array(
  73. $this->handler_id => $params[$this->handler_id],
  74. 'field_id' => $extra_field_info['id'],
  75. 'field_value' => $value
  76. );
  77. self::save($new_params);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * Save values in the *_field_values table
  84. * @param array Structured array with the values to save
  85. * @param boolean Whether to show the insert query (passed to the parent save() method)
  86. * @result mixed The result sent from the parent method
  87. * @assert (array()) === false
  88. */
  89. public function save($params, $show_query = false) {
  90. $extra_field = new ExtraField($this->type);
  91. //Setting value to insert
  92. $value = $params['field_value'];
  93. $value_to_insert = null;
  94. if (is_array($value)) {
  95. $value_to_insert = implode(';', $value);
  96. } else {
  97. $value_to_insert = Database::escape_string($value);
  98. }
  99. $params['field_value'] = $value_to_insert;
  100. //If field id exists
  101. $extra_field_info = $extra_field->get($params['field_id']);
  102. if ($extra_field_info) {
  103. switch ($extra_field_info['field_type']) {
  104. case ExtraField::FIELD_TYPE_TAG :
  105. break;
  106. case ExtraField::FIELD_TYPE_RADIO:
  107. case ExtraField::FIELD_TYPE_SELECT:
  108. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  109. //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
  110. //$params['field_value'] = split(';', $value_to_insert);
  111. /*
  112. if ($field_options) {
  113. $check = false;
  114. foreach ($field_options as $option) {
  115. if (in_array($option['option_value'], $values)) {
  116. $check = true;
  117. break;
  118. }
  119. }
  120. if (!$check) {
  121. return false; //option value not found
  122. }
  123. } else {
  124. return false; //enumerated type but no option found
  125. }*/
  126. break;
  127. case ExtraField::FIELD_TYPE_TEXT:
  128. case ExtraField::FIELD_TYPE_TEXTAREA:
  129. break;
  130. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  131. if (is_array($value)) {
  132. if (isset($value['extra_'.$extra_field_info['field_variable']]) &&
  133. isset($value['extra_'.$extra_field_info['field_variable'].'_second'])
  134. ) {
  135. $value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second'];
  136. } else {
  137. $value_to_insert = null;
  138. }
  139. }
  140. default:
  141. break;
  142. }
  143. $field_values = self::get_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
  144. if ($field_values) {
  145. self::delete_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
  146. }
  147. $params['field_value'] = $value_to_insert;
  148. $params['tms'] = api_get_utc_datetime();
  149. return parent::save($params, $show_query);
  150. }
  151. }
  152. /**
  153. * Returns the value of the given extra field on the given resource
  154. * @param int Item ID (It could be a session_id, course_id or user_id)
  155. * @param int Field ID (the ID from the *_field table)
  156. * @param bool Whether to transform the result to a human readable strings
  157. * @return mixed A structured array with the field_id and field_value, or fals on error
  158. * @assert (-1,-1) === false
  159. */
  160. public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false) {
  161. $field_id = intval($field_id);
  162. $item_id = Database::escape_string($item_id);
  163. $sql = "SELECT s.*, field_type FROM {$this->table} s
  164. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  165. WHERE {$this->handler_id} = '$item_id' AND
  166. field_id = '".$field_id."'
  167. ORDER BY id";
  168. $result = Database::query($sql);
  169. if (Database::num_rows($result)) {
  170. $result = Database::fetch_array($result, 'ASSOC');
  171. if ($transform) {
  172. if (!empty($result['field_value'])) {
  173. switch ($result['field_type']) {
  174. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  175. $field_option = new ExtraFieldOption($this->type);
  176. $options = explode('::', $result['field_value']);
  177. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  178. $result = $field_option->get($options[0]);
  179. $result_second = $field_option->get($options[1]);
  180. if (!empty($result)) {
  181. $result['field_value'] = $result['option_display_text'].' -> ';
  182. $result['field_value'] .= $result_second['option_display_text'];
  183. }
  184. break;
  185. case ExtraField::FIELD_TYPE_SELECT:
  186. $field_option = new ExtraFieldOption($this->type);
  187. $extra_field_option_result = $field_option->get_field_option_by_field_and_option($result['field_id'], $result['field_value']);
  188. if (isset($extra_field_option_result[0])) {
  189. $result['field_value'] = $extra_field_option_result[0]['option_display_text'];
  190. }
  191. break;
  192. }
  193. }
  194. }
  195. return $result;
  196. } else {
  197. return false;
  198. }
  199. }
  200. /**
  201. * Gets a structured array of the original item and its extra values, using
  202. * a specific original item and a field name (like "branch", or "birthdate")
  203. * @param int Item ID from the original table
  204. * @param string The name of the field we are looking for
  205. * @return mixed Array of results, or false on error or not found
  206. * @assert (-1,'') === false
  207. */
  208. public function get_values_by_handler_and_field_variable($item_id, $field_variable, $transform = false) {
  209. $field_id = intval($field_id);
  210. $item_id = Database::escape_string($item_id);
  211. $field_variable = Database::escape_string($field_variable);
  212. $sql = "SELECT s.*, field_type FROM {$this->table} s
  213. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  214. WHERE {$this->handler_id} = '$item_id' AND
  215. field_variable = '".$field_variable."'
  216. ORDER BY id";
  217. $result = Database::query($sql);
  218. if (Database::num_rows($result)) {
  219. $result = Database::fetch_array($result, 'ASSOC');
  220. if ($transform) {
  221. if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  222. if (!empty($result['field_value'])) {
  223. $field_option = new ExtraFieldOption($this->type);
  224. $options = explode('::', $result['field_value']);
  225. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  226. $result = $field_option->get($options[0]);
  227. $result_second = $field_option->get($options[1]);
  228. if (!empty($result)) {
  229. $result['field_value'] = $result['option_display_text'].' -> ';
  230. $result['field_value'] .= $result_second['option_display_text'];
  231. }
  232. }
  233. }
  234. }
  235. return $result;
  236. } else {
  237. return false;
  238. }
  239. }
  240. /**
  241. * Gets the ID from the item (course, session, etc) for which
  242. * the given field is defined with the given value
  243. * @param string Field (type of data) we want to check
  244. * @param string Data we are looking for in the given field
  245. * @return mixed Give the ID if found, or false on failure or not found
  246. * @assert (-1,-1) === false
  247. */
  248. public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) {
  249. $field_value = Database::escape_string($field_value);
  250. $field_variable = Database::escape_string($field_variable);
  251. $sql = "SELECT {$this->handler_id} FROM {$this->table} s
  252. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  253. WHERE field_value = '$field_value' AND
  254. field_variable = '".$field_variable."'
  255. ";
  256. $result = Database::query($sql);
  257. if ($result !== false && Database::num_rows($result)) {
  258. $result = Database::fetch_array($result, 'ASSOC');
  259. return $result;
  260. } else {
  261. return false;
  262. }
  263. }
  264. /**
  265. * Get all values for a specific field id
  266. * @param int Field ID
  267. * @return mixed Array of values on success, false on failure or not found
  268. * @assert (-1) === false
  269. */
  270. public function get_values_by_field_id($field_id) {
  271. $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  272. WHERE field_id = '".$field_id."' ORDER BY id";
  273. $result = Database::query($sql);
  274. if (Database::num_rows($result)) {
  275. return Database::store_result($result, 'ASSOC');
  276. }
  277. return false;
  278. }
  279. /**
  280. * Deletes all the values related to a specific field ID
  281. * @param int Field ID
  282. * @return void
  283. * @assert ('a') == null
  284. */
  285. public function delete_all_values_by_field_id($field_id) {
  286. $field_id = intval($field_id);
  287. $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
  288. Database::query($sql);
  289. }
  290. /**
  291. * Deletes values of a specific field for a specific item
  292. * @param int Item ID (session id, course id, etc)
  293. * @param int Field ID
  294. * @return void
  295. * @assert (-1,-1) == null
  296. */
  297. public function delete_values_by_handler_and_field_id($item_id, $field_id) {
  298. $field_id = intval($field_id);
  299. $item_id = Database::escape_string($item_id);
  300. $sql = "DELETE FROM {$this->table} WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' ";
  301. Database::query($sql);
  302. }
  303. /**
  304. * Not yet implemented - Compares the field values of two items
  305. * @param int Item 1
  306. * @param int Item 2
  307. * @return mixed Differential array generated from the comparison
  308. */
  309. public function compare_item_values($item_id, $item_to_compare) {
  310. }
  311. }