/**
  * Save values in the *_field_values table
  * @param array $params Structured array with the values to save
  * @param boolean $show_query Whether to show the insert query (passed to the parent save() method)
  * @result mixed The result sent from the parent method
  * @assert (array()) === false
  */
 public function save($params, $show_query = 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 = Database::escape_string($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:
                 if (is_array($value)) {
                     if (isset($value['extra_' . $extraFieldInfo['variable']]) && isset($value['extra_' . $extraFieldInfo['variable'] . '_second'])) {
                         $value_to_insert = $value['extra_' . $extraFieldInfo['variable']] . '::' . $value['extra_' . $extraFieldInfo['variable'] . '_second'];
                     } else {
                         $value_to_insert = null;
                     }
                 }
                 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 = array('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, $show_query);
                     }
                 } else {
                     return parent::save($params, $show_query);
                 }
             }
         } 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, $show_query);
             }
         }
     }
 }