123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152 |
- <?php
- /* For licensing terms, see /license.txt */
- use Chamilo\CoreBundle\Entity\ExtraField as EntityExtraField;
- use Chamilo\CoreBundle\Entity\ExtraFieldRelTag;
- use Chamilo\CoreBundle\Entity\ExtraFieldValues;
- use Chamilo\CoreBundle\Entity\Tag;
- use ChamiloSession as Session;
- /**
- * Class ExtraFieldValue
- * Declaration for the ExtraFieldValue class, managing the values in extra
- * fields for any data type.
- *
- * @package chamilo.library
- */
- class ExtraFieldValue extends Model
- {
- public $type = '';
- public $columns = [
- 'id',
- 'field_id',
- 'value',
- 'comment',
- 'item_id',
- 'created_at',
- 'updated_at',
- ];
- /** @var ExtraField */
- public $extraField;
- /**
- * Formats the necessary elements for the given datatype.
- *
- * @param string $type The type of data to which this extra field
- * applies (user, course, session, ...)
- *
- * @assert (-1) === false
- */
- public function __construct($type)
- {
- parent::__construct();
- $this->type = $type;
- $extraField = new ExtraField($this->type);
- $this->extraField = $extraField;
- $this->table = Database::get_main_table(TABLE_EXTRA_FIELD_VALUES);
- $this->table_handler_field = Database::get_main_table(TABLE_EXTRA_FIELD);
- }
- /**
- * @return ExtraField
- */
- public function getExtraField()
- {
- return $this->extraField;
- }
- /**
- * Gets the number of values stored in the table (all fields together)
- * for this type of resource.
- *
- * @return int Number of rows in the table
- * @assert () !== false
- */
- public function get_count()
- {
- $em = Database::getManager();
- $query = $em->getRepository('ChamiloCoreBundle:ExtraFieldValues')->createQueryBuilder('e');
- $query->select('count(e.id)');
- $query->where('e.extraFieldType = :type');
- $query->setParameter('type', $this->getExtraField()->getExtraFieldType());
- return $query->getQuery()->getSingleScalarResult();
- }
- /**
- * Save the extra fields values
- * In order to save this function needs a item_id (user id, course id, etc)
- * This function is used with $extraField->addElements().
- *
- * @param array $params array for the insertion into the *_field_values table
- * @param bool $onlySubmittedFields Only save parameters in the $param array
- * @param bool $showQuery
- * @param array $saveOnlyThisFields
- * @param array $avoidFields do not insert/modify this field
- *
- * @return mixed false on empty params, void otherwise
- * @assert (array()) === false
- */
- public function saveFieldValues(
- $params,
- $onlySubmittedFields = false,
- $showQuery = false,
- $saveOnlyThisFields = [],
- $avoidFields = []
- ) {
- foreach ($params as $key => $value) {
- $found = strpos($key, '__persist__');
- if ($found === false) {
- continue;
- }
- $tempKey = str_replace('__persist__', '', $key);
- if (!isset($params[$tempKey])) {
- $params[$tempKey] = [];
- }
- }
- if (empty($params['item_id'])) {
- return false;
- }
- $type = $this->getExtraField()->getExtraFieldType();
- $extraField = new ExtraField($this->type);
- $extraFields = $extraField->get_all(null, 'option_order');
- // Parse params.
- foreach ($extraFields as $fieldDetails) {
- $field_variable = $fieldDetails['variable'];
- // if the field is not visible to the user in the end, we need to apply special rules
- if ($fieldDetails['visible_to_self'] != 1) {
- //only admins should be able to add those values
- if (!api_is_platform_admin(true, true)) {
- // although if not admin but sent through a CLI script, we should accept it as well
- if (PHP_SAPI != 'cli') {
- continue; //not a CLI script, so don't write the value to DB
- }
- }
- }
- if ($onlySubmittedFields && !isset($params['extra_'.$field_variable])) {
- continue;
- }
- if (!empty($avoidFields)) {
- if (in_array($field_variable, $avoidFields)) {
- continue;
- }
- }
- if (!empty($saveOnlyThisFields)) {
- if (!in_array($field_variable, $saveOnlyThisFields)) {
- continue;
- }
- }
- $value = '';
- if (isset($params['extra_'.$field_variable])) {
- $value = $params['extra_'.$field_variable];
- }
- $extraFieldInfo = $this->getExtraField()->get_handler_field_info_by_field_variable($field_variable);
- if (!$extraFieldInfo) {
- continue;
- }
- $commentVariable = 'extra_'.$field_variable.'_comment';
- $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null;
- $dirPermissions = api_get_permissions_for_new_directories();
- switch ($extraFieldInfo['field_type']) {
- case ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
- case ExtraField::FIELD_TYPE_GEOLOCALIZATION:
- if (!empty($value)) {
- if (isset($params['extra_'.$extraFieldInfo['variable'].'_coordinates'])) {
- $value = $value.'::'.$params['extra_'.$extraFieldInfo['variable'].'_coordinates'];
- }
- $newParams = [
- 'item_id' => $params['item_id'],
- 'field_id' => $extraFieldInfo['id'],
- 'value' => $value,
- 'comment' => $comment,
- ];
- self::save($newParams, $showQuery);
- }
- break;
- case ExtraField::FIELD_TYPE_TAG:
- if ($type == EntityExtraField::USER_FIELD_TYPE) {
- UserManager::delete_user_tags(
- $params['item_id'],
- $extraFieldInfo['id']
- );
- UserManager::process_tags(
- $value,
- $params['item_id'],
- $extraFieldInfo['id']
- );
- break;
- }
- $em = Database::getManager();
- $currentTags = $em
- ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
- ->findBy([
- 'fieldId' => $extraFieldInfo['id'],
- 'itemId' => $params['item_id'],
- ]);
- foreach ($currentTags as $extraFieldtag) {
- $em->remove($extraFieldtag);
- }
- $em->flush();
- $tagValues = is_array($value) ? $value : [$value];
- $tags = [];
- foreach ($tagValues as $tagValue) {
- if (empty($tagValue)) {
- continue;
- }
- $tagsResult = $em->getRepository('ChamiloCoreBundle:Tag')
- ->findBy([
- 'tag' => $tagValue,
- 'fieldId' => $extraFieldInfo['id'],
- ]);
- if (empty($tagsResult)) {
- $tag = new Tag();
- $tag->setFieldId($extraFieldInfo['id']);
- $tag->setTag($tagValue);
- $tags[] = $tag;
- } else {
- $tags = array_merge($tags, $tagsResult);
- }
- }
- foreach ($tags as $tag) {
- $tagUses = $em
- ->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
- ->findBy([
- 'tagId' => $tag->getId(),
- ]);
- $tag->setCount(count($tagUses) + 1);
- $em->persist($tag);
- }
- $em->flush();
- foreach ($tags as $tag) {
- $fieldRelTag = new ExtraFieldRelTag();
- $fieldRelTag->setFieldId($extraFieldInfo['id']);
- $fieldRelTag->setItemId($params['item_id']);
- $fieldRelTag->setTagId($tag->getId());
- $em->persist($fieldRelTag);
- }
- $em->flush();
- break;
- case ExtraField::FIELD_TYPE_FILE_IMAGE:
- $fileDir = $fileDirStored = '';
- switch ($this->type) {
- case 'course':
- $fileDir = api_get_path(SYS_UPLOAD_PATH)."courses/";
- $fileDirStored = "courses/";
- break;
- case 'session':
- $fileDir = api_get_path(SYS_UPLOAD_PATH)."sessions/";
- $fileDirStored = "sessions/";
- break;
- case 'user':
- $fileDir = UserManager::getUserPathById($params['item_id'], 'system');
- $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last');
- break;
- case 'work':
- $fileDir = api_get_path(SYS_UPLOAD_PATH).'work/';
- $fileDirStored = 'work/';
- break;
- }
- $fileName = ExtraField::FIELD_TYPE_FILE_IMAGE."_{$params['item_id']}.png";
- if (!file_exists($fileDir)) {
- mkdir($fileDir, $dirPermissions, true);
- }
- if (!empty($value['tmp_name']) && isset($value['error']) && $value['error'] == 0) {
- // Crop the image to adjust 16:9 ratio
- if (isset($params['extra_'.$field_variable.'_crop_result'])) {
- $crop = new Image($value['tmp_name']);
- $crop->crop($params['extra_'.$field_variable.'_crop_result']);
- }
- $imageExtraField = new Image($value['tmp_name']);
- $imageExtraField->resize(400);
- $imageExtraField->send_image($fileDir.$fileName, -1, 'png');
- $newParams = [
- 'item_id' => $params['item_id'],
- 'field_id' => $extraFieldInfo['id'],
- 'value' => $fileDirStored.$fileName,
- 'comment' => $comment,
- ];
- $this->save($newParams);
- }
- break;
- case ExtraField::FIELD_TYPE_FILE:
- $fileDir = $fileDirStored = '';
- switch ($this->type) {
- case 'course':
- $fileDir = api_get_path(SYS_UPLOAD_PATH).'courses/';
- $fileDirStored = "courses/";
- break;
- case 'session':
- $fileDir = api_get_path(SYS_UPLOAD_PATH).'sessions/';
- $fileDirStored = "sessions/";
- break;
- case 'user':
- $fileDir = UserManager::getUserPathById($params['item_id'], 'system');
- $fileDirStored = UserManager::getUserPathById($params['item_id'], 'last');
- break;
- case 'work':
- $fileDir = api_get_path(SYS_UPLOAD_PATH).'work/';
- $fileDirStored = "work/";
- break;
- case 'scheduled_announcement':
- $fileDir = api_get_path(SYS_UPLOAD_PATH).'scheduled_announcement/';
- $fileDirStored = 'scheduled_announcement/';
- break;
- }
- $cleanedName = api_replace_dangerous_char($value['name']);
- $fileName = ExtraField::FIELD_TYPE_FILE."_{$params['item_id']}_$cleanedName";
- if (!file_exists($fileDir)) {
- mkdir($fileDir, $dirPermissions, true);
- }
- if (!empty($value['tmp_name']) && isset($value['error']) && $value['error'] == 0) {
- $cleanedName = api_replace_dangerous_char($value['name']);
- $fileName = ExtraField::FIELD_TYPE_FILE."_{$params['item_id']}_$cleanedName";
- moveUploadedFile($value, $fileDir.$fileName);
- $new_params = [
- 'item_id' => $params['item_id'],
- 'field_id' => $extraFieldInfo['id'],
- 'value' => $fileDirStored.$fileName,
- ];
- if ($this->type !== 'session' && $this->type !== 'course') {
- $new_params['comment'] = $comment;
- }
- $this->save($new_params);
- }
- break;
- case ExtraField::FIELD_TYPE_CHECKBOX:
- $fieldToSave = 0;
- if (is_array($value)) {
- if (isset($value['extra_'.$field_variable])) {
- $fieldToSave = 1;
- }
- }
- $newParams = [
- 'item_id' => $params['item_id'],
- 'field_id' => $extraFieldInfo['id'],
- 'value' => $fieldToSave,
- 'comment' => $comment,
- ];
- $this->save($newParams);
- break;
- default:
- $newParams = [
- 'item_id' => $params['item_id'],
- 'field_id' => $extraFieldInfo['id'],
- 'value' => $value,
- 'comment' => $comment,
- ];
- $this->save($newParams, $showQuery);
- }
- }
- }
- /**
- * Save values in the *_field_values table.
- *
- * @param array $params Structured array with the values to save
- * @param bool $showQuery Whether to show the insert query (passed to the parent save() method)
- *
- * @return mixed The result sent from the parent method
- * @assert (array()) === false
- */
- public function save($params, $showQuery = false)
- {
- $extra_field = $this->getExtraField();
- // Setting value to insert.
- $value = $params['value'];
- $value_to_insert = null;
- if (is_array($value)) {
- $value_to_insert = implode(';', $value);
- } else {
- $value_to_insert = $value;
- }
- $params['value'] = $value_to_insert;
- // If field id exists
- if (isset($params['field_id'])) {
- $extraFieldInfo = $extra_field->get($params['field_id']);
- } else {
- // Try the variable
- $extraFieldInfo = $extra_field->get_handler_field_info_by_field_variable(
- $params['variable']
- );
- $params['field_id'] = $extraFieldInfo['id'];
- }
- if ($extraFieldInfo) {
- switch ($extraFieldInfo['field_type']) {
- case ExtraField::FIELD_TYPE_RADIO:
- case ExtraField::FIELD_TYPE_SELECT:
- break;
- case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
- //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
- //$params['field_value'] = split(';', $value_to_insert);
- /*
- if ($field_options) {
- $check = false;
- foreach ($field_options as $option) {
- if (in_array($option['option_value'], $values)) {
- $check = true;
- break;
- }
- }
- if (!$check) {
- return false; //option value not found
- }
- } else {
- return false; //enumerated type but no option found
- }*/
- break;
- case ExtraField::FIELD_TYPE_TEXT:
- case ExtraField::FIELD_TYPE_TEXTAREA:
- break;
- case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
- case ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD:
- if (is_array($value)) {
- $value_to_insert = null;
- if (isset($value['extra_'.$extraFieldInfo['variable']]) &&
- isset($value['extra_'.$extraFieldInfo['variable'].'_second'])
- ) {
- $value_to_insert = $value['extra_'.$extraFieldInfo['variable']].'::'.$value['extra_'.$extraFieldInfo['variable'].'_second'];
- }
- }
- break;
- default:
- break;
- }
- if ($extraFieldInfo['field_type'] == ExtraField::FIELD_TYPE_TAG) {
- $field_values = self::getAllValuesByItemAndFieldAndValue(
- $params['item_id'],
- $params['field_id'],
- $value
- );
- } else {
- $field_values = self::get_values_by_handler_and_field_id(
- $params['item_id'],
- $params['field_id']
- );
- }
- $params['value'] = $value_to_insert;
- //$params['author_id'] = api_get_user_id();
- // Insert
- if (empty($field_values)) {
- /* Enable this when field_loggeable is introduced as a table field (2.0)
- if ($extraFieldInfo['field_loggeable'] == 1) {
- */
- if (false) {
- global $app;
- switch ($this->type) {
- case 'question':
- $extraFieldValue = new ChamiloLMS\Entity\QuestionFieldValues();
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setQuestionId($params[$this->handler_id]);
- break;
- case 'course':
- $extraFieldValue = new ChamiloLMS\Entity\CourseFieldValues();
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setQuestionId($params[$this->handler_id]);
- break;
- case 'user':
- $extraFieldValue = new ChamiloLMS\Entity\UserFieldValues();
- $extraFieldValue->setUserId($params[$this->handler_id]);
- $extraFieldValue->setAuthorId(api_get_user_id());
- break;
- case 'session':
- $extraFieldValue = new ChamiloLMS\Entity\SessionFieldValues();
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setSessionId($params[$this->handler_id]);
- break;
- }
- if (isset($extraFieldValue)) {
- if (!empty($params['value'])) {
- $extraFieldValue->setComment($params['comment']);
- $extraFieldValue->setFieldValue($params['value']);
- $extraFieldValue->setFieldId($params['field_id']);
- $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
- $app['orm.ems']['db_write']->persist($extraFieldValue);
- $app['orm.ems']['db_write']->flush();
- }
- }
- } else {
- if ($extraFieldInfo['field_type'] == ExtraField::FIELD_TYPE_TAG) {
- $option = new ExtraFieldOption($this->type);
- $optionExists = $option->get($params['value']);
- if (empty($optionExists)) {
- $optionParams = [
- 'field_id' => $params['field_id'],
- 'option_value' => $params['value'],
- ];
- $optionId = $option->saveOptions($optionParams);
- } else {
- $optionId = $optionExists['id'];
- }
- $params['value'] = $optionId;
- if ($optionId) {
- return parent::save($params, $showQuery);
- }
- } else {
- return parent::save($params, $showQuery);
- }
- }
- } else {
- // Update
- /* Enable this when field_loggeable is introduced as a table field (2.0)
- if ($extraFieldInfo['field_loggeable'] == 1) {
- */
- if (false) {
- global $app;
- switch ($this->type) {
- case 'question':
- $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\QuestionFieldValues')->find($field_values['id']);
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setQuestionId($params[$this->handler_id]);
- break;
- case 'course':
- $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\CourseFieldValues')->find($field_values['id']);
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setCourseCode($params[$this->handler_id]);
- break;
- case 'user':
- $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\UserFieldValues')->find($field_values['id']);
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setAuthorId(api_get_user_id());
- break;
- case 'session':
- $extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\SessionFieldValues')->find($field_values['id']);
- $extraFieldValue->setUserId(api_get_user_id());
- $extraFieldValue->setSessionId($params[$this->handler_id]);
- break;
- }
- if (isset($extraFieldValue)) {
- if (!empty($params['value'])) {
- /*
- * If the field value is similar to the previous value then the comment will be the same
- in order to no save in the log an empty record
- */
- if ($extraFieldValue->getFieldValue() == $params['value']) {
- if (empty($params['comment'])) {
- $params['comment'] = $extraFieldValue->getComment();
- }
- }
- $extraFieldValue->setComment($params['comment']);
- $extraFieldValue->setFieldValue($params['value']);
- $extraFieldValue->setFieldId($params['field_id']);
- $extraFieldValue->setTms(api_get_utc_datetime(null, false, true));
- $app['orm.ems']['db_write']->persist($extraFieldValue);
- $app['orm.ems']['db_write']->flush();
- }
- }
- } else {
- $params['id'] = $field_values['id'];
- return parent::update($params, $showQuery);
- }
- }
- }
- }
- /**
- * Returns the value of the given extra field on the given resource.
- *
- * @param int $item_id Item ID (It could be a session_id, course_id or user_id)
- * @param int $field_id Field ID (the ID from the *_field table)
- * @param bool $transform Whether to transform the result to a human readable strings
- *
- * @return mixed A structured array with the field_id and field_value, or false on error
- * @assert (-1,-1) === false
- */
- public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
- {
- $field_id = (int) $field_id;
- $item_id = Database::escape_string($item_id);
- $sql = "SELECT s.*, field_type FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id)
- WHERE
- item_id = '$item_id' AND
- field_id = '".$field_id."' AND
- sf.extra_field_type = ".$this->getExtraField()->getExtraFieldType()."
- ORDER BY id";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $result = Database::fetch_array($result, 'ASSOC');
- if ($transform) {
- if (!empty($result['value'])) {
- switch ($result['field_type']) {
- case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
- $field_option = new ExtraFieldOption($this->type);
- $options = explode('::', $result['value']);
- // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
- $result = $field_option->get($options[0]);
- $result_second = $field_option->get($options[1]);
- if (!empty($result)) {
- $result['value'] = $result['display_text'].' -> ';
- $result['value'] .= $result_second['display_text'];
- }
- break;
- case ExtraField::FIELD_TYPE_SELECT:
- $field_option = new ExtraFieldOption($this->type);
- $extra_field_option_result = $field_option->get_field_option_by_field_and_option(
- $result['field_id'],
- $result['value']
- );
- if (isset($extra_field_option_result[0])) {
- $result['value'] = $extra_field_option_result[0]['display_text'];
- }
- break;
- case ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD:
- $options = explode('::', $result['value']);
- $field_option = new ExtraFieldOption($this->type);
- $result = $field_option->get($options[0]);
- if (!empty($result)) {
- $result['value'] = $result['display_text']
- .'→'
- .$options[1];
- }
- break;
- case ExtraField::FIELD_TYPE_TRIPLE_SELECT:
- $optionIds = explode(';', $result['value']);
- $optionValues = [];
- foreach ($optionIds as $optionId) {
- $objEfOption = new ExtraFieldOption('user');
- $optionInfo = $objEfOption->get($optionId);
- $optionValues[] = $optionInfo['display_text'];
- }
- $result['value'] = implode(' / ', $optionValues);
- break;
- }
- }
- }
- return $result;
- } else {
- return false;
- }
- }
- /**
- * @param string $tag
- * @param int $field_id
- * @param int $limit
- *
- * @return array
- */
- public function searchValuesByField($tag, $field_id, $limit = 10)
- {
- $field_id = (int) $field_id;
- $limit = (int) $limit;
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $tag = Database::escape_string($tag);
- $sql = "SELECT DISTINCT s.value, s.field_id
- FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- field_id = '".$field_id."' AND
- value LIKE '%$tag%' AND
- sf.extra_field_type = ".$extraFieldType."
- ORDER BY value
- LIMIT 0, $limit
- ";
- $result = Database::query($sql);
- $values = [];
- if (Database::num_rows($result)) {
- $values = Database::store_result($result, 'ASSOC');
- }
- return $values;
- }
- /**
- * Gets a structured array of the original item and its extra values, using
- * a specific original item and a field name (like "branch", or "birthdate").
- *
- * @param int $item_id Item ID from the original table
- * @param string $field_variable The name of the field we are looking for
- * @param bool $transform
- * @param bool $filterByVisibility
- * @param int $visibility
- *
- * @return mixed Array of results, or false on error or not found
- * @assert (-1,'') === false
- */
- public function get_values_by_handler_and_field_variable(
- $item_id,
- $field_variable,
- $transform = false,
- $filterByVisibility = false,
- $visibility = 0
- ) {
- $item_id = (int) $item_id;
- $field_variable = Database::escape_string($field_variable);
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $sql = "SELECT s.*, field_type
- FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- item_id = '$item_id' AND
- variable = '".$field_variable."' AND
- sf.extra_field_type = $extraFieldType
- ";
- if ($filterByVisibility) {
- $visibility = (int) $visibility;
- $sql .= " AND visible_to_self = $visibility ";
- }
- $sql .= ' ORDER BY id';
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- $result = Database::fetch_array($result, 'ASSOC');
- if ($transform) {
- if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
- if (!empty($result['value'])) {
- $field_option = new ExtraFieldOption($this->type);
- $options = explode('::', $result['value']);
- $result = $field_option->get($options[0]);
- $result_second = $field_option->get($options[1]);
- if (!empty($result)) {
- $result['value'] = $result['display_text'].' -> ';
- $result['value'] .= $result_second['display_text'];
- }
- }
- }
- if ($result['field_type'] == ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD) {
- if (!empty($result['value'])) {
- $options = explode('::', $result['value']);
- $field_option = new ExtraFieldOption($this->type);
- $result = $field_option->get($options[0]);
- if (!empty($result)) {
- $result['value'] = $result['display_text']
- .'→'
- .$options[1];
- }
- }
- }
- if ($result['field_type'] == ExtraField::FIELD_TYPE_TRIPLE_SELECT) {
- if (!empty($result['value'])) {
- $optionIds = explode(';', $result['value']);
- $optionValues = [];
- foreach ($optionIds as $optionId) {
- $objEfOption = new ExtraFieldOption('user');
- $optionInfo = $objEfOption->get($optionId);
- $optionValues[] = $optionInfo['display_text'];
- }
- $result['value'] = implode(' / ', $optionValues);
- }
- }
- }
- return $result;
- } else {
- return false;
- }
- }
- /**
- * Gets the ID from the item (course, session, etc) for which
- * the given field is defined with the given value.
- *
- * @param string $field_variable Field (type of data) we want to check
- * @param string $field_value Data we are looking for in the given field
- * @param bool $transform Whether to transform the result to a human readable strings
- * @param bool $last Whether to return the last element or simply the first one we get
- *
- * @return mixed Give the ID if found, or false on failure or not found
- * @assert (-1,-1) === false
- */
- public function get_item_id_from_field_variable_and_field_value(
- $field_variable,
- $field_value,
- $transform = false,
- $last = false,
- $all = false
- ) {
- $field_value = Database::escape_string($field_value);
- $field_variable = Database::escape_string($field_variable);
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $sql = "SELECT item_id FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- value = '$field_value' AND
- variable = '".$field_variable."' AND
- sf.extra_field_type = $extraFieldType
- ORDER BY item_id
- ";
- if ($last) {
- // If we want the last element instead of the first
- // This is useful in special cases where there might
- // (erroneously) be more than one row for an item
- $sql .= ' DESC';
- }
- $result = Database::query($sql);
- if ($result !== false && Database::num_rows($result)) {
- if ($all) {
- $result = Database::store_result($result, 'ASSOC');
- } else {
- $result = Database::fetch_array($result, 'ASSOC');
- }
- return $result;
- } else {
- return false;
- }
- }
- /**
- * Get all the values stored for one specific field.
- *
- * @param int $fieldId
- *
- * @return array|bool
- */
- public function getValuesByFieldId($fieldId)
- {
- $fieldId = (int) $fieldId;
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $sql = "SELECT s.* FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- field_id = '".$fieldId."' AND
- sf.extra_field_type = $extraFieldType
- ORDER BY s.value";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- return Database::store_result($result, 'ASSOC');
- }
- return false;
- }
- /**
- * @param int $itemId
- * @param int $fieldId
- *
- * @return array
- */
- public function getAllValuesByItemAndField($itemId, $fieldId)
- {
- $fieldId = (int) $fieldId;
- $itemId = (int) $itemId;
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $sql = "SELECT s.* FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- field_id = $fieldId AND
- item_id = $itemId AND
- sf.extra_field_type = $extraFieldType
- ORDER BY s.value";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- return Database::store_result($result, 'ASSOC');
- }
- return false;
- }
- /**
- * @param int $itemId
- *
- * @return array
- */
- public function getAllValuesByItem($itemId)
- {
- $itemId = (int) $itemId;
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $sql = "SELECT s.value, sf.variable, sf.field_type, sf.id, sf.display_text
- FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- item_id = '$itemId' AND
- sf.extra_field_type = $extraFieldType
- ORDER BY s.value";
- $result = Database::query($sql);
- $idList = [];
- if (Database::num_rows($result)) {
- $result = Database::store_result($result, 'ASSOC');
- $finalResult = [];
- foreach ($result as $item) {
- $finalResult[$item['id']] = $item;
- }
- $idList = array_column($result, 'id');
- }
- $em = Database::getManager();
- $extraField = new ExtraField($this->type);
- $allData = $extraField->get_all(['filter = ?' => 1]);
- $allResults = [];
- foreach ($allData as $field) {
- if (in_array($field['id'], $idList)) {
- $allResults[] = $finalResult[$field['id']];
- } else {
- if ($field['field_type'] == ExtraField::FIELD_TYPE_TAG) {
- $tagResult = [];
- $tags = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')
- ->findBy(
- [
- 'fieldId' => $field['id'],
- 'itemId' => $itemId,
- ]
- );
- if ($tags) {
- /** @var ExtraFieldRelTag $extraFieldTag */
- foreach ($tags as $extraFieldTag) {
- /** @var \Chamilo\CoreBundle\Entity\Tag $tag */
- $tag = $em->find('ChamiloCoreBundle:Tag', $extraFieldTag->getTagId());
- $tagResult[] = [
- 'id' => $extraFieldTag->getTagId(),
- 'value' => $tag->getTag(),
- ];
- }
- }
- $allResults[] = [
- 'value' => $tagResult,
- 'variable' => $field['variable'],
- 'field_type' => $field['field_type'],
- 'id' => $field['id'],
- ];
- }
- }
- }
- return $allResults;
- }
- /**
- * @param int $itemId
- * @param int $fieldId
- * @param string $fieldValue
- *
- * @return array|bool
- */
- public function getAllValuesByItemAndFieldAndValue($itemId, $fieldId, $fieldValue)
- {
- $fieldId = (int) $fieldId;
- $itemId = (int) $itemId;
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $fieldValue = Database::escape_string($fieldValue);
- $sql = "SELECT s.* FROM {$this->table} s
- INNER JOIN {$this->table_handler_field} sf
- ON (s.field_id = sf.id)
- WHERE
- field_id = '$fieldId' AND
- item_id = '$itemId' AND
- value = '$fieldValue' AND
- sf.extra_field_type = $extraFieldType
- ORDER BY value";
- $result = Database::query($sql);
- if (Database::num_rows($result)) {
- return Database::store_result($result, 'ASSOC');
- }
- return false;
- }
- /**
- * Deletes all the values related to a specific field ID.
- *
- * @param int $field_id
- *
- * @assert ('a') == null
- */
- public function delete_all_values_by_field_id($field_id)
- {
- $field_id = (int) $field_id;
- $sql = "DELETE FROM {$this->table}
- WHERE
- field_id = $field_id ";
- Database::query($sql);
- }
- /**
- * Deletes all values from an item.
- *
- * @param int $itemId (session id, course id, etc)
- * @assert (-1,-1) == null
- */
- public function deleteValuesByItem($itemId)
- {
- $itemId = (int) $itemId;
- $extraFieldType = $this->getExtraField()->getExtraFieldType();
- $sql = "DELETE FROM {$this->table}
- WHERE
- item_id = '$itemId' AND
- field_id IN (
- SELECT id FROM {$this->table_handler_field}
- WHERE extra_field_type = ".$extraFieldType."
- )
- ";
- Database::query($sql);
- }
- /**
- * @param int $itemId
- * @param int $fieldId
- * @param int $fieldValue
- *
- * @return bool
- */
- public function deleteValuesByHandlerAndFieldAndValue($itemId, $fieldId, $fieldValue)
- {
- $itemId = (int) $itemId;
- $fieldId = (int) $fieldId;
- $fieldData = $this->getExtraField()->get($fieldId);
- if ($fieldData) {
- $fieldValue = Database::escape_string($fieldValue);
- $sql = "DELETE FROM {$this->table}
- WHERE
- item_id = '$itemId' AND
- field_id = '$fieldId' AND
- value = '$fieldValue'
- ";
- Database::query($sql);
- // Delete file from uploads
- if ($fieldData['field_type'] == ExtraField::FIELD_TYPE_FILE) {
- api_remove_uploaded_file($this->type, basename($fieldValue));
- }
- return true;
- }
- return false;
- }
- /**
- * Get all values for an item.
- *
- * @param int $itemId The item ID
- * @param bool $visibleToSelf Get the visible extra field only
- * @param bool $visibleToOthers
- *
- * @return array
- */
- public function getAllValuesForAnItem($itemId, $visibleToSelf = null, $visibleToOthers = null)
- {
- $em = Database::getManager();
- $qb = $em->createQueryBuilder();
- $qb = $qb->select('fv')
- ->from('ChamiloCoreBundle:ExtraFieldValues', 'fv')
- ->join('fv.field', 'f')
- ->where(
- $qb->expr()->eq('fv.itemId', ':item')
- )
- ->andWhere(
- $qb->expr()->eq('f.extraFieldType', ':extra_field_type')
- );
- if (is_bool($visibleToSelf)) {
- $qb
- ->andWhere($qb->expr()->eq('f.visibleToSelf', ':visibleToSelf'))
- ->setParameter('visibleToSelf', $visibleToSelf);
- }
- if (is_bool($visibleToOthers)) {
- $qb
- ->andWhere($qb->expr()->eq('f.visibleToOthers', ':visibleToOthers'))
- ->setParameter('visibleToOthers', $visibleToOthers);
- }
- $fieldValues = $qb
- ->setParameter('item', $itemId)
- ->setParameter('extra_field_type', $this->getExtraField()->getExtraFieldType())
- ->getQuery()
- ->getResult();
- $fieldOptionsRepo = $em->getRepository('ChamiloCoreBundle:ExtraFieldOptions');
- $valueList = [];
- /** @var ExtraFieldValues $fieldValue */
- foreach ($fieldValues as $fieldValue) {
- $item = ['value' => $fieldValue];
- switch ($fieldValue->getField()->getFieldType()) {
- case ExtraField::FIELD_TYPE_SELECT:
- $item['option'] = $fieldOptionsRepo->findOneBy([
- 'field' => $fieldValue->getField(),
- 'value' => $fieldValue->getValue(),
- ]);
- break;
- }
- $valueList[] = $item;
- }
- return $valueList;
- }
- }
|