extra_field_value.lib.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Class ExtraFieldValue
  5. * Declaration for the ExtraFieldValue class, managing the values in extra
  6. * fields for any data type
  7. * @package chamilo.library
  8. *
  9. */
  10. class ExtraFieldValue extends Model
  11. {
  12. public $type = null;
  13. public $columns = array('id', 'field_id', 'field_value', 'tms', 'comment', 'c_id');
  14. /** @var string session_id, course_code, user_id, question id */
  15. public $handler_id = null;
  16. public $entityName;
  17. /**
  18. * Formats the necessary elements for the given datatype
  19. * @param string $type The type of data to which this extra field
  20. * applies (user, course, session, ...)
  21. * @return void (or false if unmanaged datatype)
  22. * @assert (-1) === false
  23. */
  24. public function __construct($type)
  25. {
  26. $this->type = $type;
  27. $extra_field = new ExtraField($this->type);
  28. $this->handler_id = $extra_field->handler_id;
  29. switch ($this->type) {
  30. case 'calendar_event':
  31. $this->table = Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_VALUES);
  32. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD);
  33. $this->author_id = 'user_id';
  34. break;
  35. case 'course':
  36. $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  37. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
  38. $this->author_id = 'user_id';
  39. $this->entityName = 'ChamiloLMS\Entity\CourseFieldValues';
  40. break;
  41. case 'user':
  42. $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  43. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_USER_FIELD);
  44. $this->author_id = 'author_id';
  45. $this->entityName = 'ChamiloLMS\Entity\UserFieldValues';
  46. break;
  47. case 'session':
  48. $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  49. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
  50. $this->author_id = 'user_id';
  51. $this->entityName = 'ChamiloLMS\Entity\SessionFieldValues';
  52. break;
  53. case 'question':
  54. $this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_VALUES);
  55. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD);
  56. $this->author_id = 'user_id';
  57. $this->entityName = 'ChamiloLMS\Entity\QuestionFieldValues';
  58. break;
  59. case 'lp':
  60. $this->table = Database::get_main_table(TABLE_MAIN_LP_FIELD_VALUES);
  61. $this->table_handler_field = Database::get_main_table(TABLE_MAIN_LP_FIELD);
  62. $this->author_id = 'lp_id';
  63. //$this->entityName = 'ChamiloLMS\Entity\QuestionFieldValues';
  64. break;
  65. default:
  66. //unmanaged datatype, return false to let the caller know it
  67. // didn't work
  68. return false;
  69. }
  70. $this->columns[] = $this->handler_id;
  71. $this->columns[] = $this->author_id;
  72. }
  73. /**
  74. * Gets the number of values stored in the table (all fields together)
  75. * for this type of resource
  76. * @return integer Number of rows in the table
  77. * @assert () !== false
  78. */
  79. public function get_count()
  80. {
  81. $row = Database::select('count(*) as count', $this->table, array(), 'first');
  82. return $row['count'];
  83. }
  84. /**
  85. * Saves a series of records given as parameter into the corresponding table
  86. * @param array $params array for the insertion into the *_field_values table
  87. *
  88. * @return mixed false on empty params, void otherwise
  89. * @assert (array()) === false
  90. */
  91. public function save_field_values($params)
  92. {
  93. $extra_field = new ExtraField($this->type);
  94. if (empty($params[$this->handler_id])) {
  95. return false;
  96. }
  97. foreach ($params as $key => $value) {
  98. $found = strpos($key, '__persist__');
  99. if ($found) {
  100. $tempKey = str_replace('__persist__', '', $key);
  101. if (!isset($params[$tempKey])) {
  102. $params[$tempKey] = array();
  103. }
  104. }
  105. }
  106. // Parse params.
  107. foreach ($params as $key => $value) {
  108. if (substr($key, 0, 6) == 'extra_') {
  109. // An extra field.
  110. $field_variable = substr($key, 6);
  111. $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable);
  112. if ($extra_field_info) {
  113. $commentVariable = 'extra_'.$field_variable.'_comment';
  114. $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null;
  115. switch ($extra_field_info['field_type']) {
  116. case ExtraField::FIELD_TYPE_TAG :
  117. $old = self::getAllValuesByItemAndField(
  118. $params[$this->handler_id],
  119. $extra_field_info['id']
  120. );
  121. $deleteItems = array();
  122. if (!empty($old)) {
  123. $oldIds = array();
  124. foreach ($old as $oldItem) {
  125. $oldIds[] = $oldItem['field_value'];
  126. }
  127. $deleteItems = array_diff($oldIds, $value);
  128. }
  129. foreach ($value as $optionId) {
  130. $newParams = array(
  131. $this->handler_id => $params[$this->handler_id],
  132. 'field_id' => $extra_field_info['id'],
  133. 'field_value' => $optionId,
  134. 'comment' => $comment
  135. );
  136. self::save($newParams);
  137. }
  138. if (!empty($deleteItems)) {
  139. foreach ($deleteItems as $deleteFieldValue) {
  140. self::deleteValuesByHandlerAndFieldAndValue(
  141. $params[$this->handler_id],
  142. $extra_field_info['id'],
  143. $deleteFieldValue
  144. );
  145. }
  146. }
  147. break;
  148. default;
  149. $newParams = array(
  150. $this->handler_id => $params[$this->handler_id],
  151. 'field_id' => $extra_field_info['id'],
  152. 'field_value' => $value,
  153. 'comment' => $comment
  154. );
  155. self::save($newParams);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Save values in the *_field_values table
  163. * @param array $params Structured array with the values to save
  164. * @param boolean $show_query Whether to show the insert query (passed to the parent save() method)
  165. * @result mixed The result sent from the parent method
  166. * @assert (array()) === false
  167. */
  168. public function save($params, $show_query = false)
  169. {
  170. $extra_field = new ExtraField($this->type);
  171. // Setting value to insert.
  172. $value = $params['field_value'];
  173. $value_to_insert = null;
  174. if (is_array($value)) {
  175. $value_to_insert = implode(';', $value);
  176. } else {
  177. $value_to_insert = Database::escape_string($value);
  178. }
  179. $params['field_value'] = $value_to_insert;
  180. //If field id exists
  181. $extra_field_info = $extra_field->get($params['field_id']);
  182. if ($extra_field_info) {
  183. switch ($extra_field_info['field_type']) {
  184. case ExtraField::FIELD_TYPE_RADIO:
  185. case ExtraField::FIELD_TYPE_SELECT:
  186. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  187. //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
  188. //$params['field_value'] = split(';', $value_to_insert);
  189. /*
  190. if ($field_options) {
  191. $check = false;
  192. foreach ($field_options as $option) {
  193. if (in_array($option['option_value'], $values)) {
  194. $check = true;
  195. break;
  196. }
  197. }
  198. if (!$check) {
  199. return false; //option value not found
  200. }
  201. } else {
  202. return false; //enumerated type but no option found
  203. }*/
  204. break;
  205. case ExtraField::FIELD_TYPE_TEXT:
  206. case ExtraField::FIELD_TYPE_TEXTAREA:
  207. break;
  208. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  209. if (is_array($value)) {
  210. if (isset($value['extra_'.$extra_field_info['field_variable']]) &&
  211. isset($value['extra_'.$extra_field_info['field_variable'].'_second'])
  212. ) {
  213. $value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second'];
  214. } else {
  215. $value_to_insert = null;
  216. }
  217. }
  218. break;
  219. default:
  220. break;
  221. }
  222. if ($extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  223. $field_values = self::getAllValuesByItemAndFieldAndValue(
  224. $params[$this->handler_id],
  225. $params['field_id'],
  226. $value
  227. );
  228. } else {
  229. $field_values = self::get_values_by_handler_and_field_id(
  230. $params[$this->handler_id],
  231. $params['field_id']
  232. );
  233. if ($this->is_course_model) {
  234. if (isset($field_values['c_id']) && isset($params['c_id'])) {
  235. if ($field_values['c_id'] != $params['c_id']) {
  236. $field_values = null;
  237. }
  238. }
  239. }
  240. }
  241. $params['field_value'] = $value_to_insert;
  242. $params['tms'] = api_get_utc_datetime();
  243. $params[$this->author_id] = api_get_user_id();
  244. // Insert
  245. if (empty($field_values)) {
  246. if ($extra_field_info['field_loggeable'] == 1) {
  247. global $app;
  248. switch($this->type) {
  249. case 'question':
  250. $extraFieldValue = new ChamiloLMS\Entity\QuestionFieldValues();
  251. $extraFieldValue->setUserId(api_get_user_id());
  252. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  253. break;
  254. case 'course':
  255. $extraFieldValue = new ChamiloLMS\Entity\CourseFieldValues();
  256. $extraFieldValue->setUserId(api_get_user_id());
  257. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  258. break;
  259. case 'user':
  260. $extraFieldValue = new ChamiloLMS\Entity\UserFieldValues();
  261. $extraFieldValue->setUserId($params[$this->handler_id]);
  262. $extraFieldValue->setAuthorId(api_get_user_id());
  263. break;
  264. case 'session':
  265. $extraFieldValue = new ChamiloLMS\Entity\SessionFieldValues();
  266. $extraFieldValue->setUserId(api_get_user_id());
  267. $extraFieldValue->setSessionId($params[$this->handler_id]);
  268. break;
  269. }
  270. if (isset($extraFieldValue)) {
  271. if (!empty($params['field_value'])) {
  272. $extraFieldValue->setComment($params['comment']);
  273. $extraFieldValue->setFieldValue($params['field_value']);
  274. $extraFieldValue->setFieldId($params['field_id']);
  275. $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
  276. $app['orm.ems']['db_write']->persist($extraFieldValue);
  277. $app['orm.ems']['db_write']->flush();
  278. }
  279. }
  280. } else {
  281. if ($extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  282. $option = new ExtraFieldOption($this->type);
  283. $optionExists = $option->get($params['field_value']);
  284. if (empty($optionExists)) {
  285. $optionParams = array(
  286. 'field_id' => $params['field_id'],
  287. 'option_value' => $params['field_value']
  288. );
  289. $optionId = $option->saveOptions($optionParams);
  290. } else {
  291. $optionId = $optionExists['id'];
  292. }
  293. $params['field_value'] = $optionId;
  294. if ($optionId) {
  295. return parent::save($params, $show_query);
  296. }
  297. } else {
  298. return parent::save($params, $show_query);
  299. }
  300. }
  301. } else {
  302. // Update
  303. if ($extra_field_info['field_loggeable'] == 1) {
  304. global $app;
  305. switch($this->type) {
  306. case 'question':
  307. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\QuestionFieldValues')->find($field_values['id']);
  308. $extraFieldValue->setUserId(api_get_user_id());
  309. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  310. break;
  311. case 'course':
  312. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\CourseFieldValues')->find($field_values['id']);
  313. $extraFieldValue->setUserId(api_get_user_id());
  314. $extraFieldValue->setCourseCode($params[$this->handler_id]);
  315. break;
  316. case 'user':
  317. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\UserFieldValues')->find($field_values['id']);
  318. $extraFieldValue->setUserId(api_get_user_id());
  319. $extraFieldValue->setAuthorId(api_get_user_id());
  320. break;
  321. case 'session':
  322. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\SessionFieldValues')->find($field_values['id']);
  323. $extraFieldValue->setUserId(api_get_user_id());
  324. $extraFieldValue->setSessionId($params[$this->handler_id]);
  325. break;
  326. }
  327. if (isset($extraFieldValue)) {
  328. if (!empty($params['field_value'])) {
  329. /*
  330. * If the field value is similar to the previous value then the comment will be the same
  331. in order to no save in the log an empty record
  332. */
  333. if ($extraFieldValue->getFieldValue() == $params['field_value']) {
  334. if (empty($params['comment'])) {
  335. $params['comment'] = $extraFieldValue->getComment();
  336. }
  337. }
  338. $extraFieldValue->setComment($params['comment']);
  339. $extraFieldValue->setFieldValue($params['field_value']);
  340. $extraFieldValue->setFieldId($params['field_id']);
  341. $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
  342. $app['orm.ems']['db_write']->persist($extraFieldValue);
  343. $app['orm.ems']['db_write']->flush();
  344. }
  345. }
  346. } else {
  347. $params['id'] = $field_values['id'];
  348. return parent::update($params, $show_query);
  349. }
  350. }
  351. }
  352. }
  353. /**
  354. * Returns the value of the given extra field on the given resource
  355. * @param int $item_id Item ID (It could be a session_id, course_id or user_id)
  356. * @param int $field_id Field ID (the ID from the *_field table)
  357. * @param bool $transform Whether to transform the result to a human readable strings
  358. * @return mixed A structured array with the field_id and field_value, or false on error
  359. * @assert (-1,-1) === false
  360. */
  361. public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
  362. {
  363. $field_id = intval($field_id);
  364. $item_id = intval($item_id);
  365. $sql = "SELECT s.*, field_type FROM {$this->table} s
  366. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  367. WHERE {$this->handler_id} = '$item_id' AND
  368. field_id = '".$field_id."'
  369. ORDER BY id";
  370. $result = Database::query($sql);
  371. if (Database::num_rows($result)) {
  372. $result = Database::fetch_array($result, 'ASSOC');
  373. if ($transform) {
  374. if (!empty($result['field_value'])) {
  375. switch ($result['field_type']) {
  376. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  377. $field_option = new ExtraFieldOption($this->type);
  378. $options = explode('::', $result['field_value']);
  379. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  380. $result = $field_option->get($options[0]);
  381. $result_second = $field_option->get($options[1]);
  382. if (!empty($result)) {
  383. $result['field_value'] = $result['option_display_text'].' -> ';
  384. $result['field_value'] .= $result_second['option_display_text'];
  385. }
  386. break;
  387. case ExtraField::FIELD_TYPE_SELECT:
  388. $field_option = new ExtraFieldOption($this->type);
  389. $extra_field_option_result = $field_option->get_field_option_by_field_and_option(
  390. $result['field_id'],
  391. $result['field_value']
  392. );
  393. if (isset($extra_field_option_result[0])) {
  394. $result['field_value'] = $extra_field_option_result[0]['option_display_text'];
  395. }
  396. break;
  397. }
  398. }
  399. }
  400. return $result;
  401. } else {
  402. return false;
  403. }
  404. }
  405. /**
  406. * @param string $tag
  407. * @param int $field_id
  408. * @param int $limit
  409. * @return array
  410. */
  411. public function searchValuesByField($tag, $field_id, $limit = 10)
  412. {
  413. $field_id = intval($field_id);
  414. $limit = intval($limit);
  415. $tag = Database::escape_string($tag);
  416. $sql = "SELECT DISTINCT s.field_value, s.field_id
  417. FROM {$this->table} s
  418. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  419. WHERE
  420. field_id = '".$field_id."' AND
  421. field_value LIKE '%$tag%'
  422. ORDER BY field_value
  423. LIMIT 0, $limit
  424. ";
  425. $result = Database::query($sql);
  426. $values = array();
  427. if (Database::num_rows($result)) {
  428. $values = Database::store_result($result, 'ASSOC');
  429. }
  430. return $values;
  431. }
  432. /**
  433. * Gets a structured array of the original item and its extra values, using
  434. * a specific original item and a field name (like "branch", or "birthdate")
  435. * @param int $item_id Item ID from the original table
  436. * @param string $field_variable The name of the field we are looking for
  437. * @param bool $transform
  438. * @return mixed Array of results, or false on error or not found
  439. * @assert (-1,'') === false
  440. */
  441. public function get_values_by_handler_and_field_variable($item_id, $field_variable, $transform = false)
  442. {
  443. $item_id = intval($item_id);
  444. $field_variable = Database::escape_string($field_variable);
  445. $sql = "SELECT s.*, field_type FROM {$this->table} s
  446. INNER JOIN {$this->table_handler_field} sf
  447. ON (s.field_id = sf.id)
  448. WHERE
  449. {$this->handler_id} = '$item_id' AND
  450. field_variable = '".$field_variable."'
  451. ORDER BY id";
  452. $result = Database::query($sql);
  453. if (Database::num_rows($result)) {
  454. $result = Database::fetch_array($result, 'ASSOC');
  455. if ($transform) {
  456. if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  457. if (!empty($result['field_value'])) {
  458. $field_option = new ExtraFieldOption($this->type);
  459. $options = explode('::', $result['field_value']);
  460. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  461. $result = $field_option->get($options[0]);
  462. $result_second = $field_option->get($options[1]);
  463. if (!empty($result)) {
  464. $result['field_value'] = $result['option_display_text'].' -> ';
  465. $result['field_value'] .= $result_second['option_display_text'];
  466. }
  467. }
  468. }
  469. }
  470. return $result;
  471. } else {
  472. return false;
  473. }
  474. }
  475. /**
  476. * Gets the ID from the item (course, session, etc) for which
  477. * the given field is defined with the given value
  478. * @param string $field_variable Field (type of data) we want to check
  479. * @param string $field_value Data we are looking for in the given field
  480. * @param bool $transform Whether to transform the result to a human readable strings
  481. * @param bool $last Whether to return the last element or simply the first one we get
  482. * @param bool $all return all items
  483. * @return mixed Give the ID if found, or false on failure or not found
  484. * @assert (-1,-1) === false
  485. */
  486. public function get_item_id_from_field_variable_and_field_value(
  487. $field_variable,
  488. $field_value,
  489. $transform = false,
  490. $last = false,
  491. $all = false
  492. ) {
  493. $field_value = Database::escape_string($field_value);
  494. $field_variable = Database::escape_string($field_variable);
  495. $sql = "SELECT {$this->handler_id} ";
  496. if ($all) {
  497. $sql = "SELECT s.* ";
  498. }
  499. $sql .= "
  500. FROM {$this->table} s
  501. INNER JOIN {$this->table_handler_field} sf
  502. ON (s.field_id = sf.id)
  503. WHERE
  504. field_value = '$field_value' AND
  505. field_variable = '".$field_variable."'
  506. ORDER BY {$this->handler_id}
  507. ";
  508. if ($last) {
  509. // If we want the last element instead of the first
  510. // This is useful in special cases where there might
  511. // (erroneously) be more than one row for an item
  512. $sql .= ' DESC';
  513. }
  514. $result = Database::query($sql);
  515. if ($result !== false && Database::num_rows($result)) {
  516. if ($all) {
  517. $result = Database::store_result($result, 'ASSOC');
  518. } else {
  519. $result = Database::fetch_array($result, 'ASSOC');
  520. }
  521. return $result;
  522. } else {
  523. return false;
  524. }
  525. }
  526. /**
  527. * Get all values for a specific field id
  528. * @param int $field_id
  529. * @return mixed Array of values on success, false on failure or not found
  530. * @assert (-1) === false
  531. */
  532. public function get_values_by_field_id($field_id)
  533. {
  534. $field_id = intval($field_id);
  535. $sql = "SELECT s.*, field_type FROM {$this->table} s
  536. INNER JOIN {$this->table_handler_field} sf
  537. ON (s.field_id = sf.id)
  538. WHERE field_id = '".$field_id."' ORDER BY id";
  539. $result = Database::query($sql);
  540. if (Database::num_rows($result)) {
  541. return Database::store_result($result, 'ASSOC');
  542. }
  543. return false;
  544. }
  545. /**
  546. * @param int $itemId
  547. * @param int $fieldId
  548. * @return array
  549. */
  550. public function getAllValuesByItemAndField($itemId, $fieldId)
  551. {
  552. $fieldId = intval($fieldId);
  553. $itemId = intval($itemId);
  554. $sql = "SELECT s.* FROM {$this->table} s
  555. INNER JOIN {$this->table_handler_field} sf
  556. ON (s.field_id = sf.id)
  557. WHERE
  558. field_id = '".$fieldId."' AND
  559. {$this->handler_id} = '$itemId'
  560. ORDER BY field_value";
  561. $result = Database::query($sql);
  562. if (Database::num_rows($result)) {
  563. return Database::store_result($result, 'ASSOC');
  564. }
  565. return false;
  566. }
  567. /**
  568. * @param int $itemId
  569. * @param int $fieldId
  570. * @param string $fieldValue
  571. * @return array|bool
  572. */
  573. public function getAllValuesByItemAndFieldAndValue($itemId, $fieldId, $fieldValue)
  574. {
  575. $fieldId = intval($fieldId);
  576. $itemId = intval($itemId);
  577. $fieldValue = Database::escape_string($fieldValue);
  578. $sql = "SELECT s.* FROM {$this->table} s
  579. INNER JOIN {$this->table_handler_field} sf
  580. ON (s.field_id = sf.id)
  581. WHERE
  582. field_id = '".$fieldId."' AND
  583. {$this->handler_id} = '$itemId' AND
  584. field_value = $fieldValue
  585. ORDER BY field_value";
  586. $result = Database::query($sql);
  587. if (Database::num_rows($result)) {
  588. return Database::store_result($result, 'ASSOC');
  589. }
  590. return false;
  591. }
  592. /**
  593. * Deletes all the values related to a specific field ID
  594. * @param int $field_id
  595. * @return void
  596. * @assert ('a') == null
  597. */
  598. public function delete_all_values_by_field_id($field_id)
  599. {
  600. $field_id = intval($field_id);
  601. $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
  602. Database::query($sql);
  603. }
  604. /**
  605. * Deletes values of a specific field for a specific item
  606. * @param int $item_id (session id, course id, etc)
  607. * @param int $field_id
  608. * @return void
  609. * @assert (-1,-1) == null
  610. */
  611. public function delete_values_by_handler_and_field_id($item_id, $field_id)
  612. {
  613. $field_id = intval($field_id);
  614. $item_id = intval($item_id);
  615. $sql = "DELETE FROM {$this->table}
  616. WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' ";
  617. Database::query($sql);
  618. }
  619. /**
  620. * @param int $itemId
  621. * @param int $fieldId
  622. * @param int $fieldValue
  623. */
  624. public function deleteValuesByHandlerAndFieldAndValue($itemId, $fieldId, $fieldValue)
  625. {
  626. $itemId = intval($itemId);
  627. $fieldId = intval($fieldId);
  628. $fieldValue = Database::escape_string($fieldValue);
  629. $sql = "DELETE FROM {$this->table}
  630. WHERE
  631. {$this->handler_id} = '$itemId' AND
  632. field_id = '".$fieldId."' AND
  633. field_value = '$fieldValue'";
  634. Database::query($sql);
  635. }
  636. /**
  637. * Not yet implemented - Compares the field values of two items
  638. * @param int $item_id Item 1
  639. * @param int $item_to_compare Item 2
  640. * @return mixed Differential array generated from the comparison
  641. */
  642. public function compare_item_values($item_id, $item_to_compare)
  643. {
  644. }
  645. }