extra_field_value.lib.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
  4. use Chamilo\CoreBundle\Entity\ExtraFieldRelTag;
  5. use Chamilo\CoreBundle\Entity\Tag;
  6. use ChamiloSession as Session;
  7. use Chamilo\CoreBundle\Entity\ExtraFieldValues;
  8. /**
  9. * Class ExtraFieldValue
  10. * Declaration for the ExtraFieldValue class, managing the values in extra
  11. * fields for any data type
  12. *
  13. * @package chamilo.library
  14. *
  15. */
  16. class ExtraFieldValue extends Model
  17. {
  18. public $type = '';
  19. public $columns = array(
  20. 'id',
  21. 'field_id',
  22. 'value',
  23. 'comment',
  24. 'item_id',
  25. 'created_at',
  26. 'updated_at',
  27. );
  28. /** @var ExtraField */
  29. public $extraField;
  30. /**
  31. * Formats the necessary elements for the given datatype
  32. * @param string $type The type of data to which this extra field
  33. * applies (user, course, session, ...)
  34. *
  35. * @assert (-1) === false
  36. */
  37. public function __construct($type)
  38. {
  39. parent::__construct();
  40. $this->type = $type;
  41. $extraField = new ExtraField($this->type);
  42. $this->extraField = $extraField;
  43. $this->table = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
  44. $this->table_handler_field = Database::get_main_table(TABLE_EXTRA_FIELD);
  45. }
  46. /**
  47. * @return ExtraField
  48. */
  49. public function getExtraField()
  50. {
  51. return $this->extraField;
  52. }
  53. /**
  54. * Gets the number of values stored in the table (all fields together)
  55. * for this type of resource
  56. * @return integer Number of rows in the table
  57. * @assert () !== false
  58. */
  59. public function get_count()
  60. {
  61. $em = Database::getManager();
  62. $query = $em->getRepository('ChamiloCoreBundle:ExtraFieldValues')->createQueryBuilder('e');
  63. $query->select('count(e.id)');
  64. $query->where('e.extraFieldType = :type');
  65. $query->setParameter('type', $this->getExtraField()->getExtraFieldType());
  66. return $query->getQuery()->getScalarResult();
  67. }
  68. /**
  69. * Save the extra fields values
  70. * In order to save this function needs a item_id (user id, course id, etc)
  71. * This function is used with $extraField->addElements()
  72. * @param array $params array for the insertion into the *_field_values table
  73. * @param bool $forceSave
  74. * @param bool $showQuery
  75. * @param array $saveOnlyThisFields
  76. * @param array $avoidFields do not insert/modify this field
  77. * @return mixed false on empty params, void otherwise
  78. * @assert (array()) === false
  79. */
  80. public function saveFieldValues(
  81. $params,
  82. $forceSave = false,
  83. $showQuery = false,
  84. $saveOnlyThisFields = [],
  85. $avoidFields = []
  86. ) {
  87. foreach ($params as $key => $value) {
  88. $found = strpos($key, '__persist__');
  89. if ($found === false) {
  90. continue;
  91. }
  92. $tempKey = str_replace('__persist__', '', $key);
  93. if (!isset($params[$tempKey])) {
  94. $params[$tempKey] = array();
  95. }
  96. }
  97. if (empty($params['item_id'])) {
  98. return false;
  99. }
  100. $type = $this->getExtraField()->getExtraFieldType();
  101. $extraField = new ExtraField($this->type);
  102. $extraFields = $extraField->get_all(null, 'option_order');
  103. $resultsExist = [];
  104. // Parse params
  105. foreach ($extraFields as $fieldDetails) {
  106. if ($forceSave === false) {
  107. if ($fieldDetails['visible_to_self'] != 1) {
  108. continue;
  109. }
  110. }
  111. $field_variable = $fieldDetails['variable'];
  112. if (!empty($avoidFields)) {
  113. if (in_array($field_variable, $avoidFields)) {
  114. continue;
  115. }
  116. }
  117. if (!empty($saveOnlyThisFields)) {
  118. if (!in_array($field_variable, $saveOnlyThisFields)) {
  119. continue;
  120. }
  121. }
  122. if (isset($params['extra_'.$field_variable])) {
  123. $value = $params['extra_'.$field_variable];
  124. } else {
  125. $value = '';
  126. }
  127. $resultsExist[$field_variable] = isset($value) && $value !== '' ? true : false;
  128. $extraFieldInfo = $this->getExtraField()->get_handler_field_info_by_field_variable($field_variable);
  129. if (!$extraFieldInfo) {
  130. continue;
  131. }
  132. $commentVariable = 'extra_'.$field_variable.'_comment';
  133. $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null;
  134. switch ($extraFieldInfo['field_type']) {
  135. case ExtraField::FIELD_TYPE_TAG:
  136. if ($type == EntityExtraField::USER_FIELD_TYPE) {
  137. UserManager::delete_user_tags(
  138. $params['item_id'],
  139. $extraFieldInfo['id']
  140. );
  141. UserManager::process_tags(
  142. $value,
  143. $params['item_id'],
  144. $extraFieldInfo['id']
  145. );
  146. break;
  147. }
  148. $em = Database::getManager();
  149. $currentTags = $em
  150. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  151. ->findBy([
  152. 'fieldId' => $extraFieldInfo['id'],
  153. 'itemId' => $params['item_id']
  154. ]);
  155. foreach ($currentTags as $extraFieldtag) {
  156. $em->remove($extraFieldtag);
  157. }
  158. $em->flush();
  159. $tagValues = is_array($value) ? $value : [$value];
  160. $tags = [];
  161. foreach ($tagValues as $tagValue) {
  162. if (empty($tagValue)) {
  163. continue;
  164. }
  165. $tagsResult = $em
  166. ->getRepository('ChamiloCoreBundle:Tag')
  167. ->findBy([
  168. 'tag' => $tagValue,
  169. 'fieldId' => $extraFieldInfo['id'],
  170. ]);
  171. if (empty($tagsResult)) {
  172. $tag = new Tag();
  173. $tag->setFieldId($extraFieldInfo['id']);
  174. $tag->setTag($tagValue);
  175. $tags[] = $tag;
  176. } else {
  177. $tags = array_merge($tags, $tagsResult);
  178. }
  179. }
  180. foreach ($tags as $tag) {
  181. $tagUses = $em
  182. ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  183. ->findBy([
  184. 'tagId' => $tag->getId()
  185. ]);
  186. $tag->setCount(count($tagUses) + 1);
  187. $em->persist($tag);
  188. }
  189. $em->flush();
  190. foreach ($tags as $tag) {
  191. $fieldRelTag = new ExtraFieldRelTag();
  192. $fieldRelTag->setFieldId($extraFieldInfo['id']);
  193. $fieldRelTag->setItemId($params['item_id']);
  194. $fieldRelTag->setTagId($tag->getId());
  195. $em->persist($fieldRelTag);
  196. }
  197. $em->flush();
  198. break;
  199. case ExtraField::FIELD_TYPE_FILE_IMAGE:
  200. $dirPermissions = api_get_permissions_for_new_directories();
  201. switch ($this->type) {
  202. case 'course':
  203. $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/";
  204. $fileDirStored = "courses/";
  205. break;
  206. case 'session':
  207. $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/";
  208. $fileDirStored = "sessions/";
  209. break;
  210. case 'user':
  211. $fileDir = UserManager::getUserPathById($params['item_id'], 'system');
  212. $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last');
  213. break;
  214. }
  215. $fileName = ExtraField::FIELD_TYPE_FILE_IMAGE."_{$params['item_id']}.png";
  216. if (!file_exists($fileDir)) {
  217. mkdir($fileDir, $dirPermissions, true);
  218. }
  219. if ($value['error'] == 0) {
  220. //Crop the image to adjust 16:9 ratio
  221. $crop = new Image($value['tmp_name']);
  222. $crop->crop($params['extra_'.$field_variable.'_crop_result']);
  223. $imageExtraField = new Image($value['tmp_name']);
  224. $imageExtraField->resize(400);
  225. $imageExtraField->send_image($fileDir.$fileName, -1, 'png');
  226. $newParams = array(
  227. 'item_id' => $params['item_id'],
  228. 'field_id' => $extraFieldInfo['id'],
  229. 'value' => $fileDirStored.$fileName,
  230. 'comment' => $comment
  231. );
  232. self::save($newParams);
  233. }
  234. break;
  235. case ExtraField::FIELD_TYPE_FILE:
  236. $dirPermissions = api_get_permissions_for_new_directories();
  237. switch ($this->type) {
  238. case 'course':
  239. $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/";
  240. $fileDirStored = "courses/";
  241. break;
  242. case 'session':
  243. $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/";
  244. $fileDirStored = "sessions/";
  245. break;
  246. case 'user':
  247. $fileDir = UserManager::getUserPathById($params['item_id'], 'system');
  248. $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last');
  249. break;
  250. }
  251. $cleanedName = api_replace_dangerous_char($value['name']);
  252. $fileName = ExtraField::FIELD_TYPE_FILE."_{$params['item_id']}_$cleanedName";
  253. if (!file_exists($fileDir)) {
  254. mkdir($fileDir, $dirPermissions, true);
  255. }
  256. if ($value['error'] == 0) {
  257. moveUploadedFile($value, $fileDir.$fileName);
  258. $new_params = array(
  259. 'item_id' => $params['item_id'],
  260. 'field_id' => $extraFieldInfo['id'],
  261. 'value' => $fileDirStored.$fileName
  262. );
  263. if ($this->type !== 'session' && $this->type !== 'course') {
  264. $new_params['comment'] = $comment;
  265. }
  266. self::save($new_params);
  267. }
  268. break;
  269. case ExtraField::FIELD_TYPE_CHECKBOX:
  270. $fieldToSave = 0;
  271. if (is_array($value)) {
  272. if (isset($value['extra_'.$field_variable])) {
  273. $fieldToSave = 1;
  274. }
  275. }
  276. $newParams = array(
  277. 'item_id' => $params['item_id'],
  278. 'field_id' => $extraFieldInfo['id'],
  279. 'value' => $fieldToSave,
  280. 'comment' => $comment
  281. );
  282. self::save($newParams);
  283. break;
  284. default:
  285. $newParams = array(
  286. 'item_id' => $params['item_id'],
  287. 'field_id' => $extraFieldInfo['id'],
  288. 'value' => $value,
  289. 'comment' => $comment
  290. );
  291. self::save($newParams, $showQuery);
  292. }
  293. }
  294. // Set user.profile_completed = 1
  295. if ($this->type === 'user') {
  296. if (api_get_setting('show_terms_if_profile_completed') === 'true') {
  297. $justTermResults = [];
  298. foreach ($resultsExist as $term => $value) {
  299. if (strpos($term, 'terms_') !== false) {
  300. $justTermResults[$term] = $value;
  301. }
  302. }
  303. $profileCompleted = 0;
  304. if (!in_array(false, $justTermResults)) {
  305. $profileCompleted = 1;
  306. }
  307. $userId = $params['item_id'];
  308. $table = Database::get_main_table(TABLE_MAIN_USER);
  309. $sql = "UPDATE $table SET profile_completed = $profileCompleted WHERE user_id = $userId";
  310. Database::query($sql);
  311. Session::write('profile_completed_result', $justTermResults);
  312. }
  313. }
  314. }
  315. /**
  316. * Save values in the *_field_values table
  317. * @param array $params Structured array with the values to save
  318. * @param boolean $show_query Whether to show the insert query (passed to the parent save() method)
  319. * @return mixed The result sent from the parent method
  320. * @assert (array()) === false
  321. */
  322. public function save($params, $show_query = false)
  323. {
  324. $extra_field = $this->getExtraField();
  325. // Setting value to insert.
  326. $value = $params['value'];
  327. $value_to_insert = null;
  328. if (is_array($value)) {
  329. $value_to_insert = implode(';', $value);
  330. } else {
  331. $value_to_insert = $value;
  332. }
  333. $params['value'] = $value_to_insert;
  334. // If field id exists
  335. if (isset($params['field_id'])) {
  336. $extraFieldInfo = $extra_field->get($params['field_id']);
  337. } else {
  338. // Try the variable
  339. $extraFieldInfo = $extra_field->get_handler_field_info_by_field_variable(
  340. $params['variable']
  341. );
  342. $params['field_id'] = $extraFieldInfo['id'];
  343. }
  344. if ($extraFieldInfo) {
  345. switch ($extraFieldInfo['field_type']) {
  346. case ExtraField::FIELD_TYPE_RADIO:
  347. case ExtraField::FIELD_TYPE_SELECT:
  348. break;
  349. case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
  350. //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
  351. //$params['field_value'] = split(';', $value_to_insert);
  352. /*
  353. if ($field_options) {
  354. $check = false;
  355. foreach ($field_options as $option) {
  356. if (in_array($option['option_value'], $values)) {
  357. $check = true;
  358. break;
  359. }
  360. }
  361. if (!$check) {
  362. return false; //option value not found
  363. }
  364. } else {
  365. return false; //enumerated type but no option found
  366. }*/
  367. break;
  368. case ExtraField::FIELD_TYPE_TEXT:
  369. case ExtraField::FIELD_TYPE_TEXTAREA:
  370. break;
  371. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  372. if (is_array($value)) {
  373. if (isset($value['extra_'.$extraFieldInfo['variable']]) &&
  374. isset($value['extra_'.$extraFieldInfo['variable'].'_second'])
  375. ) {
  376. $value_to_insert = $value['extra_'.$extraFieldInfo['variable']].'::'.$value['extra_'.$extraFieldInfo['variable'].'_second'];
  377. } else {
  378. $value_to_insert = null;
  379. }
  380. }
  381. break;
  382. default:
  383. break;
  384. }
  385. if ($extraFieldInfo['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  386. $field_values = self::getAllValuesByItemAndFieldAndValue(
  387. $params['item_id'],
  388. $params['field_id'],
  389. $value
  390. );
  391. } else {
  392. $field_values = self::get_values_by_handler_and_field_id(
  393. $params['item_id'],
  394. $params['field_id']
  395. );
  396. }
  397. $params['value'] = $value_to_insert;
  398. $params['author_id'] = api_get_user_id();
  399. // Insert
  400. if (empty($field_values)) {
  401. /* Enable this when field_loggeable is introduced as a table field (2.0)
  402. if ($extraFieldInfo['field_loggeable'] == 1) {
  403. */
  404. if (false) {
  405. global $app;
  406. switch ($this->type) {
  407. case 'question':
  408. $extraFieldValue = new ChamiloLMS\Entity\QuestionFieldValues();
  409. $extraFieldValue->setUserId(api_get_user_id());
  410. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  411. break;
  412. case 'course':
  413. $extraFieldValue = new ChamiloLMS\Entity\CourseFieldValues();
  414. $extraFieldValue->setUserId(api_get_user_id());
  415. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  416. break;
  417. case 'user':
  418. $extraFieldValue = new ChamiloLMS\Entity\UserFieldValues();
  419. $extraFieldValue->setUserId($params[$this->handler_id]);
  420. $extraFieldValue->setAuthorId(api_get_user_id());
  421. break;
  422. case 'session':
  423. $extraFieldValue = new ChamiloLMS\Entity\SessionFieldValues();
  424. $extraFieldValue->setUserId(api_get_user_id());
  425. $extraFieldValue->setSessionId($params[$this->handler_id]);
  426. break;
  427. }
  428. if (isset($extraFieldValue)) {
  429. if (!empty($params['value'])) {
  430. $extraFieldValue->setComment($params['comment']);
  431. $extraFieldValue->setFieldValue($params['value']);
  432. $extraFieldValue->setFieldId($params['field_id']);
  433. $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
  434. $app['orm.ems']['db_write']->persist($extraFieldValue);
  435. $app['orm.ems']['db_write']->flush();
  436. }
  437. }
  438. } else {
  439. if ($extraFieldInfo['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  440. $option = new ExtraFieldOption($this->type);
  441. $optionExists = $option->get($params['value']);
  442. if (empty($optionExists)) {
  443. $optionParams = array(
  444. 'field_id' => $params['field_id'],
  445. 'option_value' => $params['value']
  446. );
  447. $optionId = $option->saveOptions($optionParams);
  448. } else {
  449. $optionId = $optionExists['id'];
  450. }
  451. $params['value'] = $optionId;
  452. if ($optionId) {
  453. return parent::save($params, $show_query);
  454. }
  455. } else {
  456. return parent::save($params, $show_query);
  457. }
  458. }
  459. } else {
  460. // Update
  461. /* Enable this when field_loggeable is introduced as a table field (2.0)
  462. if ($extraFieldInfo['field_loggeable'] == 1) {
  463. */
  464. if (false) {
  465. global $app;
  466. switch ($this->type) {
  467. case 'question':
  468. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\QuestionFieldValues')->find($field_values['id']);
  469. $extraFieldValue->setUserId(api_get_user_id());
  470. $extraFieldValue->setQuestionId($params[$this->handler_id]);
  471. break;
  472. case 'course':
  473. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\CourseFieldValues')->find($field_values['id']);
  474. $extraFieldValue->setUserId(api_get_user_id());
  475. $extraFieldValue->setCourseCode($params[$this->handler_id]);
  476. break;
  477. case 'user':
  478. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\UserFieldValues')->find($field_values['id']);
  479. $extraFieldValue->setUserId(api_get_user_id());
  480. $extraFieldValue->setAuthorId(api_get_user_id());
  481. break;
  482. case 'session':
  483. $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\SessionFieldValues')->find($field_values['id']);
  484. $extraFieldValue->setUserId(api_get_user_id());
  485. $extraFieldValue->setSessionId($params[$this->handler_id]);
  486. break;
  487. }
  488. if (isset($extraFieldValue)) {
  489. if (!empty($params['value'])) {
  490. /*
  491. * If the field value is similar to the previous value then the comment will be the same
  492. in order to no save in the log an empty record
  493. */
  494. if ($extraFieldValue->getFieldValue() == $params['value']) {
  495. if (empty($params['comment'])) {
  496. $params['comment'] = $extraFieldValue->getComment();
  497. }
  498. }
  499. $extraFieldValue->setComment($params['comment']);
  500. $extraFieldValue->setFieldValue($params['value']);
  501. $extraFieldValue->setFieldId($params['field_id']);
  502. $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
  503. $app['orm.ems']['db_write']->persist($extraFieldValue);
  504. $app['orm.ems']['db_write']->flush();
  505. }
  506. }
  507. } else {
  508. $params['id'] = $field_values['id'];
  509. return parent::update($params, $show_query);
  510. }
  511. }
  512. }
  513. }
  514. /**
  515. * Returns the value of the given extra field on the given resource
  516. * @param int $item_id Item ID (It could be a session_id, course_id or user_id)
  517. * @param int $field_id Field ID (the ID from the *_field table)
  518. * @param bool $transform Whether to transform the result to a human readable strings
  519. * @return mixed A structured array with the field_id and field_value, or false on error
  520. * @assert (-1,-1) === false
  521. */
  522. public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
  523. {
  524. $field_id = intval($field_id);
  525. $item_id = Database::escape_string($item_id);
  526. $sql = "SELECT s.*, field_type FROM {$this->table} s
  527. INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
  528. WHERE
  529. item_id = '$item_id' AND
  530. field_id = '".$field_id."' AND
  531. sf.extra_field_type = ".$this->getExtraField()->getExtraFieldType()."
  532. ORDER BY id";
  533. $result = Database::query($sql);
  534. if (Database::num_rows($result)) {
  535. $result = Database::fetch_array($result, 'ASSOC');
  536. if ($transform) {
  537. if (!empty($result['value'])) {
  538. switch ($result['field_type']) {
  539. case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
  540. $field_option = new ExtraFieldOption($this->type);
  541. $options = explode('::', $result['value']);
  542. // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
  543. $result = $field_option->get($options[0]);
  544. $result_second = $field_option->get($options[1]);
  545. if (!empty($result)) {
  546. $result['value'] = $result['display_text'].' -> ';
  547. $result['value'] .= $result_second['display_text'];
  548. }
  549. break;
  550. case ExtraField::FIELD_TYPE_SELECT:
  551. $field_option = new ExtraFieldOption($this->type);
  552. $extra_field_option_result = $field_option->get_field_option_by_field_and_option(
  553. $result['field_id'],
  554. $result['value']
  555. );
  556. if (isset($extra_field_option_result[0])) {
  557. $result['value'] = $extra_field_option_result[0]['display_text'];
  558. }
  559. break;
  560. }
  561. }
  562. }
  563. return $result;
  564. } else {
  565. return false;
  566. }
  567. }
  568. /**
  569. * @param string $tag
  570. * @param int $field_id
  571. * @param int $limit
  572. *
  573. * @return array
  574. */
  575. public function searchValuesByField($tag, $field_id, $limit = 10)
  576. {
  577. $field_id = intval($field_id);
  578. $limit = intval($limit);
  579. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  580. $tag = Database::escape_string($tag);
  581. $sql = "SELECT DISTINCT s.value, s.field_id
  582. FROM {$this->table} s
  583. INNER JOIN {$this->table_handler_field} sf
  584. ON (s.field_id = sf.id)
  585. WHERE
  586. field_id = '".$field_id."' AND
  587. value LIKE '%$tag%' AND
  588. sf.extra_field_type = ".$extraFieldType."
  589. ORDER BY value
  590. LIMIT 0, $limit
  591. ";
  592. $result = Database::query($sql);
  593. $values = array();
  594. if (Database::num_rows($result)) {
  595. $values = Database::store_result($result, 'ASSOC');
  596. }
  597. return $values;
  598. }
  599. /**
  600. * Gets a structured array of the original item and its extra values, using
  601. * a specific original item and a field name (like "branch", or "birthdate")
  602. * @param int $item_id Item ID from the original table
  603. * @param string $field_variable The name of the field we are looking for
  604. * @param bool $transform
  605. * @param bool $filterByVisibility
  606. * @param int $visibility
  607. *
  608. * @return mixed Array of results, or false on error or not found
  609. * @assert (-1,'') === false
  610. */
  611. public function get_values_by_handler_and_field_variable(
  612. $item_id,
  613. $field_variable,
  614. $transform = false,
  615. $filterByVisibility = false,
  616. $visibility = 0
  617. ) {
  618. $item_id = intval($item_id);
  619. $field_variable = Database::escape_string($field_variable);
  620. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  621. $sql = "SELECT s.*, field_type
  622. FROM {$this->table} s
  623. INNER JOIN {$this->table_handler_field} sf
  624. ON (s.field_id = sf.id)
  625. WHERE
  626. item_id = '$item_id' AND
  627. variable = '".$field_variable."' AND
  628. sf.extra_field_type = $extraFieldType
  629. ";
  630. if ($filterByVisibility) {
  631. $visibility = intval($visibility);
  632. $sql .= " AND visible_to_self = $visibility ";
  633. }
  634. $sql .= " ORDER BY id";
  635. $result = Database::query($sql);
  636. if (Database::num_rows($result)) {
  637. $result = Database::fetch_array($result, 'ASSOC');
  638. if ($transform) {
  639. if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
  640. if (!empty($result['value'])) {
  641. $field_option = new ExtraFieldOption($this->type);
  642. $options = explode('::', $result['value']);
  643. $result = $field_option->get($options[0]);
  644. $result_second = $field_option->get($options[1]);
  645. if (!empty($result)) {
  646. $result['value'] = $result['display_text'].' -> ';
  647. $result['value'] .= $result_second['display_text'];
  648. }
  649. }
  650. }
  651. }
  652. return $result;
  653. } else {
  654. return false;
  655. }
  656. }
  657. /**
  658. * Gets the ID from the item (course, session, etc) for which
  659. * the given field is defined with the given value
  660. * @param string $field_variable Field (type of data) we want to check
  661. * @param string $field_value Data we are looking for in the given field
  662. * @param bool $transform Whether to transform the result to a human readable strings
  663. * @param bool $last Whether to return the last element or simply the first one we get
  664. * @return mixed Give the ID if found, or false on failure or not found
  665. * @assert (-1,-1) === false
  666. */
  667. public function get_item_id_from_field_variable_and_field_value(
  668. $field_variable,
  669. $field_value,
  670. $transform = false,
  671. $last = false,
  672. $all = false
  673. ) {
  674. $field_value = Database::escape_string($field_value);
  675. $field_variable = Database::escape_string($field_variable);
  676. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  677. $sql = "SELECT item_id FROM {$this->table} s
  678. INNER JOIN {$this->table_handler_field} sf
  679. ON (s.field_id = sf.id)
  680. WHERE
  681. value = '$field_value' AND
  682. variable = '".$field_variable."' AND
  683. sf.extra_field_type = $extraFieldType
  684. ORDER BY item_id
  685. ";
  686. if ($last) {
  687. // If we want the last element instead of the first
  688. // This is useful in special cases where there might
  689. // (erroneously) be more than one row for an item
  690. $sql .= ' DESC';
  691. }
  692. $result = Database::query($sql);
  693. if ($result !== false && Database::num_rows($result)) {
  694. if ($all) {
  695. $result = Database::store_result($result, 'ASSOC');
  696. } else {
  697. $result = Database::fetch_array($result, 'ASSOC');
  698. }
  699. return $result;
  700. } else {
  701. return false;
  702. }
  703. }
  704. /**
  705. * Get all the values stored for one specific field
  706. * @param int $fieldId
  707. *
  708. * @return array|bool
  709. */
  710. public function getValuesByFieldId($fieldId)
  711. {
  712. $fieldId = intval($fieldId);
  713. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  714. $sql = "SELECT s.* FROM {$this->table} s
  715. INNER JOIN {$this->table_handler_field} sf
  716. ON (s.field_id = sf.id)
  717. WHERE
  718. field_id = '".$fieldId."' AND
  719. sf.extra_field_type = $extraFieldType
  720. ORDER BY s.value";
  721. $result = Database::query($sql);
  722. if (Database::num_rows($result)) {
  723. return Database::store_result($result, 'ASSOC');
  724. }
  725. return false;
  726. }
  727. /**
  728. * @param int $itemId
  729. * @param int $fieldId
  730. * @return array
  731. */
  732. public function getAllValuesByItemAndField($itemId, $fieldId)
  733. {
  734. $fieldId = intval($fieldId);
  735. $itemId = intval($itemId);
  736. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  737. $sql = "SELECT s.* FROM {$this->table} s
  738. INNER JOIN {$this->table_handler_field} sf
  739. ON (s.field_id = sf.id)
  740. WHERE
  741. field_id = '".$fieldId."' AND
  742. item_id = '$itemId' AND
  743. sf.extra_field_type = $extraFieldType
  744. ORDER BY s.value";
  745. $result = Database::query($sql);
  746. if (Database::num_rows($result)) {
  747. return Database::store_result($result, 'ASSOC');
  748. }
  749. return false;
  750. }
  751. /**
  752. * @param int $itemId
  753. *
  754. * @return array
  755. */
  756. public function getAllValuesByItem($itemId)
  757. {
  758. $itemId = intval($itemId);
  759. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  760. $sql = "SELECT s.value, sf.variable, sf.field_type, sf.id
  761. FROM {$this->table} s
  762. INNER JOIN {$this->table_handler_field} sf
  763. ON (s.field_id = sf.id)
  764. WHERE
  765. item_id = '$itemId' AND
  766. sf.extra_field_type = $extraFieldType
  767. ORDER BY s.value";
  768. $result = Database::query($sql);
  769. $idList = [];
  770. if (Database::num_rows($result)) {
  771. $result = Database::store_result($result, 'ASSOC');
  772. $finalResult = [];
  773. foreach ($result as $item) {
  774. $finalResult[$item['id']] = $item;
  775. }
  776. $idList = array_column($result, 'id');
  777. }
  778. $em = Database::getManager();
  779. $extraField = new ExtraField($this->type);
  780. $allData = $extraField->get_all(['filter = ?' => 1]);
  781. $allResults = [];
  782. foreach ($allData as $field) {
  783. if (in_array($field['id'], $idList)) {
  784. $allResults[] = $finalResult[$field['id']];
  785. } else {
  786. if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
  787. $tagResult = [];
  788. $tags = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
  789. ->findBy(
  790. [
  791. 'fieldId' => $field['id'],
  792. 'itemId' => $itemId,
  793. ]
  794. );
  795. if ($tags) {
  796. /** @var ExtraFieldRelTag $extraFieldTag */
  797. foreach ($tags as $extraFieldTag) {
  798. /** @var \Chamilo\CoreBundle\Entity\Tag $tag */
  799. $tag = $em->find('ChamiloCoreBundle:Tag', $extraFieldTag->getTagId());
  800. $tagResult[] = [
  801. 'id' => $extraFieldTag->getTagId(),
  802. 'value' => $tag->getTag()
  803. ];
  804. }
  805. }
  806. $allResults[] = [
  807. 'value' => $tagResult,
  808. 'variable' => $field['variable'],
  809. 'field_type' => $field['field_type'],
  810. 'id' => $field['id'] ,
  811. ];
  812. }
  813. }
  814. }
  815. return $allResults;
  816. }
  817. /**
  818. * @param int $itemId
  819. * @param int $fieldId
  820. * @param string $fieldValue
  821. *
  822. * @return array|bool
  823. */
  824. public function getAllValuesByItemAndFieldAndValue($itemId, $fieldId, $fieldValue)
  825. {
  826. $fieldId = intval($fieldId);
  827. $itemId = intval($itemId);
  828. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  829. $fieldValue = Database::escape_string($fieldValue);
  830. $sql = "SELECT s.* FROM {$this->table} s
  831. INNER JOIN {$this->table_handler_field} sf
  832. ON (s.field_id = sf.id)
  833. WHERE
  834. field_id = '$fieldId' AND
  835. item_id = '$itemId' AND
  836. value = '$fieldValue' AND
  837. sf.extra_field_type = $extraFieldType
  838. ORDER BY value";
  839. $result = Database::query($sql);
  840. if (Database::num_rows($result)) {
  841. return Database::store_result($result, 'ASSOC');
  842. }
  843. return false;
  844. }
  845. /**
  846. * Deletes all the values related to a specific field ID
  847. * @param int $field_id
  848. *
  849. * @return void
  850. * @assert ('a') == null
  851. */
  852. public function delete_all_values_by_field_id($field_id)
  853. {
  854. $field_id = intval($field_id);
  855. $sql = "DELETE FROM {$this->table}
  856. WHERE
  857. field_id = $field_id ";
  858. Database::query($sql);
  859. }
  860. /**
  861. * Deletes values of a specific field for a specific item
  862. * @param int $item_id (session id, course id, etc)
  863. * @param int $field_id
  864. * @return void
  865. * @assert (-1,-1) == null
  866. */
  867. public function delete_values_by_handler_and_field_id($item_id, $field_id)
  868. {
  869. $field_id = intval($field_id);
  870. $item_id = intval($item_id);
  871. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  872. $sql = "DELETE FROM {$this->table}
  873. WHERE
  874. item_id = '$item_id' AND
  875. field_id = '$field_id' AND
  876. extra_field_type = $extraFieldType
  877. ";
  878. Database::query($sql);
  879. }
  880. /**
  881. * Deletes all values from an item
  882. * @param int $itemId (session id, course id, etc)
  883. * @assert (-1,-1) == null
  884. */
  885. public function deleteValuesByItem($itemId)
  886. {
  887. $itemId = intval($itemId);
  888. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  889. $sql = "DELETE FROM {$this->table}
  890. WHERE
  891. item_id = '$itemId' AND
  892. field_id IN (
  893. SELECT id FROM {$this->table_handler_field}
  894. WHERE extra_field_type = ".$extraFieldType."
  895. )
  896. ";
  897. Database::query($sql);
  898. }
  899. /**
  900. * @param int $itemId
  901. * @param int $fieldId
  902. * @param int $fieldValue
  903. */
  904. public function deleteValuesByHandlerAndFieldAndValue($itemId, $fieldId, $fieldValue)
  905. {
  906. $itemId = intval($itemId);
  907. $fieldId = intval($fieldId);
  908. $fieldValue = Database::escape_string($fieldValue);
  909. $sql = "DELETE FROM {$this->table}
  910. WHERE
  911. item_id = '$itemId' AND
  912. field_id = '$fieldId' AND
  913. value = '$fieldValue'
  914. ";
  915. Database::query($sql);
  916. }
  917. /**
  918. * Not yet implemented - Compares the field values of two items
  919. * @param int $item_id Item 1
  920. * @param int $item_to_compare Item 2
  921. * @todo
  922. * @return mixed Differential array generated from the comparison
  923. */
  924. public function compareItemValues($item_id, $item_to_compare)
  925. {
  926. }
  927. /**
  928. * Get all values for an item
  929. * @param int $itemId The item ID
  930. * @param boolean $visibleToSelf Get the visible extra field only
  931. * @param boolean $visibleToOthers
  932. *
  933. * @return array
  934. */
  935. public function getAllValuesForAnItem($itemId, $visibleToSelf = null, $visibleToOthers = null)
  936. {
  937. $em = Database::getManager();
  938. /** @var \Doctrine\DBAL\Query\QueryBuilder $qb */
  939. $qb = $em->createQueryBuilder();
  940. $qb = $qb->select('fv')
  941. ->from('ChamiloCoreBundle:ExtraFieldValues', 'fv')
  942. ->join('fv.field', 'f')
  943. ->where(
  944. $qb->expr()->eq('fv.itemId', ':item')
  945. );
  946. if (is_bool($visibleToSelf)) {
  947. $qb
  948. ->andWhere($qb->expr()->eq('f.visibleToSelf', ':visibleToSelf'))
  949. ->setParameter('visibleToSelf', $visibleToSelf);
  950. }
  951. if (is_bool($visibleToOthers)) {
  952. $qb
  953. ->andWhere($qb->expr()->eq('f.visibleToOthers', ':visibleToOthers'))
  954. ->setParameter('visibleToOthers', $visibleToOthers);
  955. }
  956. $fieldValues = $qb
  957. ->setParameter('item', $itemId)
  958. ->getQuery()
  959. ->getResult();
  960. $fieldOptionsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldOptions');
  961. $valueList = [];
  962. /** @var ExtraFieldValues $fieldValue */
  963. foreach ($fieldValues as $fieldValue) {
  964. $item = [
  965. 'value' => $fieldValue
  966. ];
  967. switch ($fieldValue->getField()->getFieldType()) {
  968. case ExtraField::FIELD_TYPE_SELECT:
  969. $item['option'] = $fieldOptionsRepo->findOneBy([
  970. 'field' => $fieldValue->getField(),
  971. 'value' => $fieldValue->getValue()
  972. ]);
  973. break;
  974. }
  975. $valueList[] = $item;
  976. }
  977. return $valueList;
  978. }
  979. }