extra_field_value.lib.php 45 KB

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