extra_field_value.lib.php 46 KB

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