extra_field_value.lib.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Declaration for the ExtraFieldValue class, managing the values in extra
  5. * fields for any datatype
  6. * @package chamilo.library
  7. */
  8. /**
  9. * Class managing the values in extra fields for any datatype
  10. * @package chamilo.library.extrafields
  11. */
  12. class ExtraFieldValue extends Model
  13. {
  14. public $type = null;
  15. public $columns = array('id', 'field_id', 'field_value', 'tms', 'comment');
  16. /** @var string session_id, course_code, user_id, question id */
  17. public $handler_id = null;
  18. public $entityName;
  19. /**
  20. * Formats the necessary elements for the given datatype
  21. * @param string The type of data to which this extra field applies (user, course, session, ...)
  22. * @return void (or false if unmanaged datatype)
  23. * @assert (-1) === false
  24. */
  25. public function __construct($type)
  26. {
  27. $this->type = $type;
  28. $extra_field = new ExtraField($this->type);
  29. $this->handler_id = $extra_field->handler_id;
  30. switch ($this->type) {
  31. case 'course':
  32. $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  33. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  34. $this->author_id = 'user_id';
  35. $this->entityName = 'Entity\CourseFieldValues';
  36. break;
  37. case 'user':
  38. $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  39. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  40. $this->author_id = 'author_id';
  41. $this->entityName = 'Entity\UserFieldValues';
  42. break;
  43. case 'session':
  44. $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  45. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  46. $this->author_id = 'user_id';
  47. $this->entityName = 'Entity\SessionFieldValues';
  48. break;
  49. case 'question':
  50. $this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_VALUES);
  51. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD);
  52. $this->author_id = 'user_id';
  53. $this->entityName = 'Entity\QuestionFieldValues';
  54. break;
  55. default:
  56. //unmanaged datatype, return false to let the caller know it
  57. // didn't work
  58. return false;
  59. }
  60. $this->columns[] = $this->handler_id;
  61. $this->columns[] = $this->author_id;
  62. }
  63. /**
  64. * Gets the number of values stored in the table (all fields together)
  65. * for this type of resource
  66. * @return integer Number of rows in the table
  67. * @assert () !== false
  68. */
  69. public function get_count()
  70. {
  71. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  72. return $row['count'];
  73. }
  74. /**
  75. * Saves a series of records given as parameter into the coresponding table
  76. * @param array Structured parameter for the insertion into the *_field_values table
  77. * @return mixed false on empty params, void otherwise
  78. * @assert (array()) === false
  79. */
  80. public function save_field_values($params)
  81. {
  82. $extra_field = new ExtraField($this->type);
  83. if (empty($params[$this->handler_id])) {
  84. return false;
  85. }
  86. // Parse params.
  87. foreach ($params as $key => $value) {
  88. if (substr($key, 0, 6) == 'extra_') {
  89. // An extra field.
  90. $field_variable = substr($key, 6);
  91. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable);
  92. if ($extra_field_info) {
  93. $commentVariable = 'extra_'.$field_variable.'_comment';
  94. $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null;
  95. $new_params = array(
  96. $this->handler_id => $params[$this->handler_id],
  97. 'field_id' => $extra_field_info['id'],
  98. 'field_value' => $value,
  99. 'comment' => $comment
  100. );
  101. self::save($new_params);
  102. }
  103. }
  104. }
  105. }
  106. /**
  107. * Save values in the *_field_values table
  108. * @param array Structured array with the values to save
  109. * @param boolean Whether to show the insert query (passed to the parent save() method)
  110. * @result mixed The result sent from the parent method
  111. * @assert (array()) === false
  112. */
  113. public function save($params, $show_query = false)
  114. {
  115. $extra_field = new ExtraField($this->type);
  116. // Setting value to insert.
  117. $value = $params['field_value'];
  118. $value_to_insert = null;
  119. if (is_array($value)) {
  120. $value_to_insert = implode(';', $value);
  121. } else {
  122. $value_to_insert = Database::escape_string($value);
  123. }
  124. $params['field_value'] = $value_to_insert;
  125. //If field id exists
  126. $extra_field_info = $extra_field->get($params['field_id']);
  127. if ($extra_field_info) {
  128. switch ($extra_field_info['field_type']) {
  129. case ExtraField::FIELD_TYPE_TAG:
  130. break;
  131. case ExtraField::FIELD_TYPE_RADIO:
  132. case ExtraField::FIELD_TYPE_SELECT:
  133. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  134. //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
  135. //$params['field_value'] = split(';', $value_to_insert);
  136. /*
  137. if ($field_options) {
  138. $check = false;
  139. foreach ($field_options as $option) {
  140. if (in_array($option['option_value'], $values)) {
  141. $check = true;
  142. break;
  143. }
  144. }
  145. if (!$check) {
  146. return false; //option value not found
  147. }
  148. } else {
  149. return false; //enumerated type but no option found
  150. }*/
  151. break;
  152. case ExtraField::FIELD_TYPE_TEXT:
  153. case ExtraField::FIELD_TYPE_TEXTAREA:
  154. break;
  155. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  156. if (is_array($value)) {
  157. if (isset($value['extra_'.$extra_field_info['field_variable']]) &&
  158. isset($value['extra_'.$extra_field_info['field_variable'].'_second'])
  159. ) {
  160. $value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second'];
  161. } else {
  162. $value_to_insert = null;
  163. }
  164. }
  165. break;
  166. default:
  167. break;
  168. }
  169. $field_values = self::get_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
  170. $params['field_value'] = $value_to_insert;
  171. $params['tms'] = api_get_utc_datetime();
  172. $params[$this->author_id] = api_get_user_id();
  173. // Insert
  174. if (empty($field_values)) {
  175. if ($extra_field_info['field_loggeable'] == 1) {
  176. global $app;
  177. switch($this->type) {
  178. case 'question':
  179. $extraFieldValue = new Entity\QuestionFieldValues();
  180. $extraFieldValue->setUserId(api_get_user_id());
  181. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  182. break;
  183. case 'course':
  184. $extraFieldValue = new Entity\CourseFieldValues();
  185. $extraFieldValue->setUserId(api_get_user_id());
  186. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  187. break;
  188. case 'user':
  189. $extraFieldValue = new Entity\UserFieldValues();
  190. $extraFieldValue->setUserId($params[$this->handler_id]);
  191. $extraFieldValue->setAuthorId(api_get_user_id());
  192. break;
  193. case 'session':
  194. $extraFieldValue = new Entity\SessionFieldValues();
  195. $extraFieldValue->setUserId(api_get_user_id());
  196. $extraFieldValue->setSessionId($params[$this->handler_id]);
  197. break;
  198. }
  199. if (isset($extraFieldValue)) {
  200. if (!empty($params['field_value'])) {
  201. $extraFieldValue->setComment($params['comment']);
  202. $extraFieldValue->setFieldValue($params['field_value']);
  203. $extraFieldValue->setFieldId($params['field_id']);
  204. $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
  205. $app['orm.ems']['db_write']->persist($extraFieldValue);
  206. $app['orm.ems']['db_write']->flush();
  207. }
  208. }
  209. } else {
  210. return parent::save($params, $show_query);
  211. }
  212. } else {
  213. //self::delete_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
  214. // Update
  215. if ($extra_field_info['field_loggeable'] == 1) {
  216. global $app;
  217. switch($this->type) {
  218. case 'question':
  219. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('Entity\QuestionFieldValues')->find($field_values['id']);
  220. $extraFieldValue->setUserId(api_get_user_id());
  221. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  222. break;
  223. case 'course':
  224. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('Entity\CourseFieldValues')->find($field_values['id']);
  225. $extraFieldValue->setUserId(api_get_user_id());
  226. $extraFieldValue->setCourseCode($params[$this->handler_id]);
  227. break;
  228. case 'user':
  229. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('Entity\UserFieldValues')->find($field_values['id']);
  230. $extraFieldValue->setUserId(api_get_user_id());
  231. $extraFieldValue->setAuthorId(api_get_user_id());
  232. break;
  233. case 'session':
  234. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('Entity\SessionFieldValues')->find($field_values['id']);
  235. $extraFieldValue->setUserId(api_get_user_id());
  236. $extraFieldValue->setSessionId($params[$this->handler_id]);
  237. break;
  238. }
  239. if (isset($extraFieldValue)) {
  240. if (!empty($params['field_value'])) {
  241. /*
  242. * If the field value is similar to the previous value then the comment will be the same
  243. in order to no save in the log an empty record
  244. */
  245. if ($extraFieldValue->getFieldValue() == $params['field_value']) {
  246. if (empty($params['comment'])) {
  247. $params['comment'] = $extraFieldValue->getComment();
  248. }
  249. }
  250. $extraFieldValue->setComment($params['comment']);
  251. $extraFieldValue->setFieldValue($params['field_value']);
  252. $extraFieldValue->setFieldId($params['field_id']);
  253. $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
  254. $app['orm.ems']['db_write']->persist($extraFieldValue);
  255. $app['orm.ems']['db_write']->flush();
  256. }
  257. }
  258. } else {
  259. $params['id'] = $field_values['id'];
  260. return parent::update($params, $show_query);
  261. }
  262. }
  263. }
  264. }
  265. /**
  266. * Returns the value of the given extra field on the given resource
  267. * @param int Item ID (It could be a session_id, course_id or user_id)
  268. * @param int Field ID (the ID from the *_field table)
  269. * @param bool Whether to transform the result to a human readable strings
  270. * @return mixed A structured array with the field_id and field_value, or fals on error
  271. * @assert (-1,-1) === false
  272. */
  273. public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
  274. {
  275. $field_id = intval($field_id);
  276. $item_id = Database::escape_string($item_id);
  277. $sql = "SELECT s.*, field_type FROM {$this->table} s
  278. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  279. WHERE {$this->handler_id} = '$item_id' AND
  280. field_id = '".$field_id."'
  281. ORDER BY id";
  282. $result = Database::query($sql);
  283. if (Database::num_rows($result)) {
  284. $result = Database::fetch_array($result, 'ASSOC');
  285. if ($transform) {
  286. if (!empty($result['field_value'])) {
  287. switch ($result['field_type']) {
  288. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  289. $field_option = new ExtraFieldOption($this->type);
  290. $options = explode('::', $result['field_value']);
  291. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  292. $result = $field_option->get($options[0]);
  293. $result_second = $field_option->get($options[1]);
  294. if (!empty($result)) {
  295. $result['field_value'] = $result['option_display_text'].' -> ';
  296. $result['field_value'] .= $result_second['option_display_text'];
  297. }
  298. break;
  299. case ExtraField::FIELD_TYPE_SELECT:
  300. $field_option = new ExtraFieldOption($this->type);
  301. $extra_field_option_result = $field_option->get_field_option_by_field_and_option($result['field_id'], $result['field_value']);
  302. if (isset($extra_field_option_result[0])) {
  303. $result['field_value'] = $extra_field_option_result[0]['option_display_text'];
  304. }
  305. break;
  306. }
  307. }
  308. }
  309. return $result;
  310. } else {
  311. return false;
  312. }
  313. }
  314. /**
  315. * Gets a structured array of the original item and its extra values, using
  316. * a specific original item and a field name (like "branch", or "birthdate")
  317. * @param int Item ID from the original table
  318. * @param string The name of the field we are looking for
  319. * @return mixed Array of results, or false on error or not found
  320. * @assert (-1,'') === false
  321. */
  322. public function get_values_by_handler_and_field_variable($item_id, $field_variable, $transform = false)
  323. {
  324. $item_id = Database::escape_string($item_id);
  325. $field_variable = Database::escape_string($field_variable);
  326. $sql = "SELECT s.*, field_type FROM {$this->table} s
  327. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  328. WHERE {$this->handler_id} = '$item_id' AND
  329. field_variable = '".$field_variable."'
  330. ORDER BY id";
  331. $result = Database::query($sql);
  332. if (Database::num_rows($result)) {
  333. $result = Database::fetch_array($result, 'ASSOC');
  334. if ($transform) {
  335. if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  336. if (!empty($result['field_value'])) {
  337. $field_option = new ExtraFieldOption($this->type);
  338. $options = explode('::', $result['field_value']);
  339. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  340. $result = $field_option->get($options[0]);
  341. $result_second = $field_option->get($options[1]);
  342. if (!empty($result)) {
  343. $result['field_value'] = $result['option_display_text'].' -> ';
  344. $result['field_value'] .= $result_second['option_display_text'];
  345. }
  346. }
  347. }
  348. }
  349. return $result;
  350. } else {
  351. return false;
  352. }
  353. }
  354. /**
  355. * Gets the ID from the item (course, session, etc) for which
  356. * the given field is defined with the given value
  357. * @param string Field (type of data) we want to check
  358. * @param string Data we are looking for in the given field
  359. * @return mixed Give the ID if found, or false on failure or not found
  360. * @assert (-1,-1) === false
  361. */
  362. public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) {
  363. $field_value = Database::escape_string($field_value);
  364. $field_variable = Database::escape_string($field_variable);
  365. $sql = "SELECT {$this->handler_id} FROM {$this->table} s
  366. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  367. WHERE field_value = '$field_value' AND
  368. field_variable = '".$field_variable."'
  369. ";
  370. $result = Database::query($sql);
  371. if ($result !== false && Database::num_rows($result)) {
  372. $result = Database::fetch_array($result, 'ASSOC');
  373. return $result;
  374. } else {
  375. return false;
  376. }
  377. }
  378. /**
  379. * Get all values for a specific field id
  380. * @param int Field ID
  381. * @return mixed Array of values on success, false on failure or not found
  382. * @assert (-1) === false
  383. */
  384. public function get_values_by_field_id($field_id)
  385. {
  386. $sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  387. WHERE field_id = '".$field_id."' ORDER BY id";
  388. $result = Database::query($sql);
  389. if (Database::num_rows($result)) {
  390. return Database::store_result($result, 'ASSOC');
  391. }
  392. return false;
  393. }
  394. /**
  395. * Deletes all the values related to a specific field ID
  396. * @param int Field ID
  397. * @return void
  398. * @assert ('a') == null
  399. */
  400. public function delete_all_values_by_field_id($field_id) {
  401. $field_id = intval($field_id);
  402. $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
  403. Database::query($sql);
  404. }
  405. /**
  406. * Deletes values of a specific field for a specific item
  407. * @param int Item ID (session id, course id, etc)
  408. * @param int Field ID
  409. * @return void
  410. * @assert (-1,-1) == null
  411. */
  412. public function delete_values_by_handler_and_field_id($item_id, $field_id)
  413. {
  414. $field_id = intval($field_id);
  415. $item_id = Database::escape_string($item_id);
  416. $sql = "DELETE FROM {$this->table} WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' ";
  417. Database::query($sql);
  418. }
  419. /**
  420. * Not yet implemented - Compares the field values of two items
  421. * @param int Item 1
  422. * @param int Item 2
  423. * @return mixed Differential array generated from the comparison
  424. */
  425. public function compare_item_values($item_id, $item_to_compare)
  426. {
  427. }
  428. }