extra_field_value.lib.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 values of a specific field for a specific item.
  932. *
  933. * @param int $item_id (session id, course id, etc)
  934. * @param int $field_id
  935. * @assert (-1,-1) == null
  936. */
  937. public function delete_values_by_handler_and_field_id($item_id, $field_id)
  938. {
  939. $field_id = (int) $field_id;
  940. $item_id = (int) $item_id;
  941. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  942. $sql = "DELETE FROM {$this->table}
  943. WHERE
  944. item_id = '$item_id' AND
  945. field_id = '$field_id'
  946. ";
  947. Database::query($sql);
  948. }
  949. /**
  950. * Deletes all values from an item.
  951. *
  952. * @param int $itemId (session id, course id, etc)
  953. * @assert (-1,-1) == null
  954. */
  955. public function deleteValuesByItem($itemId)
  956. {
  957. $itemId = (int) $itemId;
  958. $extraFieldType = $this->getExtraField()->getExtraFieldType();
  959. $sql = "DELETE FROM {$this->table}
  960. WHERE
  961. item_id = '$itemId' AND
  962. field_id IN (
  963. SELECT id FROM {$this->table_handler_field}
  964. WHERE extra_field_type = ".$extraFieldType."
  965. )
  966. ";
  967. Database::query($sql);
  968. }
  969. /**
  970. * @param int $itemId
  971. * @param int $fieldId
  972. * @param int $fieldValue
  973. *
  974. * @return bool
  975. */
  976. public function deleteValuesByHandlerAndFieldAndValue($itemId, $fieldId, $fieldValue)
  977. {
  978. $itemId = (int) $itemId;
  979. $fieldId = (int) $fieldId;
  980. $fieldData = $this->getExtraField()->get($fieldId);
  981. if ($fieldData) {
  982. $fieldValue = Database::escape_string($fieldValue);
  983. $sql = "DELETE FROM {$this->table}
  984. WHERE
  985. item_id = '$itemId' AND
  986. field_id = '$fieldId' AND
  987. value = '$fieldValue'
  988. ";
  989. Database::query($sql);
  990. // Delete file from uploads
  991. if ($fieldData['field_type'] == ExtraField::FIELD_TYPE_FILE) {
  992. api_remove_uploaded_file($this->type, basename($fieldValue));
  993. }
  994. return true;
  995. }
  996. return false;
  997. }
  998. /**
  999. * Not yet implemented - Compares the field values of two items.
  1000. *
  1001. * @param int $item_id Item 1
  1002. * @param int $item_to_compare Item 2
  1003. *
  1004. * @todo
  1005. *
  1006. * @return mixed Differential array generated from the comparison
  1007. */
  1008. public function compareItemValues($item_id, $item_to_compare)
  1009. {
  1010. }
  1011. /**
  1012. * Get all values for an item.
  1013. *
  1014. * @param int $itemId The item ID
  1015. * @param bool $visibleToSelf Get the visible extra field only
  1016. * @param bool $visibleToOthers
  1017. *
  1018. * @return array
  1019. */
  1020. public function getAllValuesForAnItem($itemId, $visibleToSelf = null, $visibleToOthers = null)
  1021. {
  1022. $em = Database::getManager();
  1023. $qb = $em->createQueryBuilder();
  1024. $qb = $qb->select('fv')
  1025. ->from('ChamiloCoreBundle:ExtraFieldValues', 'fv')
  1026. ->join('fv.field', 'f')
  1027. ->where(
  1028. $qb->expr()->eq('fv.itemId', ':item')
  1029. )
  1030. ->andWhere(
  1031. $qb->expr()->eq('f.extraFieldType', ':extra_field_type')
  1032. );
  1033. if (is_bool($visibleToSelf)) {
  1034. $qb
  1035. ->andWhere($qb->expr()->eq('f.visibleToSelf', ':visibleToSelf'))
  1036. ->setParameter('visibleToSelf', $visibleToSelf);
  1037. }
  1038. if (is_bool($visibleToOthers)) {
  1039. $qb
  1040. ->andWhere($qb->expr()->eq('f.visibleToOthers', ':visibleToOthers'))
  1041. ->setParameter('visibleToOthers', $visibleToOthers);
  1042. }
  1043. $fieldValues = $qb
  1044. ->setParameter('item', $itemId)
  1045. ->setParameter('extra_field_type', $this->getExtraField()->getExtraFieldType())
  1046. ->getQuery()
  1047. ->getResult();
  1048. $fieldOptionsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldOptions');
  1049. $valueList = [];
  1050. /** @var ExtraFieldValues $fieldValue */
  1051. foreach ($fieldValues as $fieldValue) {
  1052. $item = ['value' => $fieldValue];
  1053. switch ($fieldValue->getField()->getFieldType()) {
  1054. case ExtraField::FIELD_TYPE_SELECT:
  1055. $item['option'] = $fieldOptionsRepo->findOneBy([
  1056. 'field' => $fieldValue->getField(),
  1057. 'value' => $fieldValue->getValue(),
  1058. ]);
  1059. break;
  1060. }
  1061. $valueList[] = $item;
  1062. }
  1063. return $valueList;
  1064. }
  1065. }