/**
     * @param FormValidator $form
     * @param $extra_data
     * @param $form_name
     * @param bool $admin_permissions
     * @param null $user_id
     * @deprecated
     * @return array
     */
    static function set_extra_fields_in_form($form, $extra_data, $admin_permissions = false, $user_id = null)
    {
        $user_id = intval($user_id);
        // EXTRA FIELDS
        $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
        $jquery_ready_content = null;
        foreach ($extra as $field_details) {
            if (!$admin_permissions) {
                if ($field_details[6] == 0) {
                    continue;
                }
            }
            switch ($field_details[2]) {
                case ExtraField::FIELD_TYPE_TEXT:
                    $form->addElement('text', 'extra_' . $field_details[1], $field_details[3], array('size' => 40));
                    $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
                    $form->applyFilter('extra_' . $field_details[1], 'trim');
                    $form->applyFilter('extra_' . $field_details[1], 'html_filter');
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    break;
                case ExtraField::FIELD_TYPE_TEXTAREA:
                    $form->addHtmlEditor('extra_' . $field_details[1], $field_details[3], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130'));
                    $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
                    $form->applyFilter('extra_' . $field_details[1], 'trim');
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    break;
                case ExtraField::FIELD_TYPE_RADIO:
                    $group = array();
                    foreach ($field_details[9] as $option_id => $option_details) {
                        $options[$option_details[1]] = $option_details[2];
                        $group[] = $form->createElement('radio', 'extra_' . $field_details[1], $option_details[1], $option_details[2] . '<br />', $option_details[1]);
                    }
                    $form->addGroup($group, 'extra_' . $field_details[1], $field_details[3], '');
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    break;
                case ExtraField::FIELD_TYPE_SELECT:
                    $get_lang_variables = false;
                    if (in_array($field_details[1], array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message'))) {
                        $get_lang_variables = true;
                    }
                    $options = array();
                    foreach ($field_details[9] as $option_id => $option_details) {
                        if ($get_lang_variables) {
                            $options[$option_details[1]] = get_lang($option_details[2]);
                        } else {
                            $options[$option_details[1]] = $option_details[2];
                        }
                    }
                    if ($get_lang_variables) {
                        $field_details[3] = get_lang($field_details[3]);
                    }
                    $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, array('id' => 'extra_' . $field_details[1]));
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    break;
                case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
                    $options = array();
                    foreach ($field_details[9] as $option_id => $option_details) {
                        $options[$option_details[1]] = $option_details[2];
                    }
                    $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], $options, array('multiple' => 'multiple'));
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    break;
                case ExtraField::FIELD_TYPE_DATE:
                    $form->addDatePicker('extra_' . $field_details[1], $field_details[3]);
                    $defaults['extra_' . $field_details[1]] = date('Y-m-d 12:00:00');
                    $form->setDefaults($defaults);
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    $form->applyFilter('theme', 'trim');
                    break;
                case ExtraField::FIELD_TYPE_DATETIME:
                    $form->addDateTimePicker('extra_' . $field_details[1], $field_details[3]);
                    $defaults['extra_' . $field_details[1]] = date('Y-m-d 12:00:00');
                    $form->setDefaults($defaults);
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    $form->applyFilter('theme', 'trim');
                    break;
                case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
                    foreach ($field_details[9] as $key => $element) {
                        if ($element[2][0] == '*') {
                            $values['*'][$element[0]] = str_replace('*', '', $element[2]);
                        } else {
                            $values[0][$element[0]] = $element[2];
                        }
                    }
                    $group = '';
                    $group[] = $form->createElement('select', 'extra_' . $field_details[1], '', $values[0], '');
                    $group[] = $form->createElement('select', 'extra_' . $field_details[1] . '*', '', $values['*'], '');
                    $form->addGroup($group, 'extra_' . $field_details[1], $field_details[3], '&nbsp;');
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    /* Recoding the selected values for double : if the user has
                       selected certain values, we have to assign them to the
                       correct select form */
                    if (array_key_exists('extra_' . $field_details[1], $extra_data)) {
                        // exploding all the selected values (of both select forms)
                        $selected_values = explode(';', $extra_data['extra_' . $field_details[1]]);
                        $extra_data['extra_' . $field_details[1]] = array();
                        // looping through the selected values and assigning the selected values to either the first or second select form
                        foreach ($selected_values as $key => $selected_value) {
                            if (array_key_exists($selected_value, $values[0])) {
                                $extra_data['extra_' . $field_details[1]]['extra_' . $field_details[1]] = $selected_value;
                            } else {
                                $extra_data['extra_' . $field_details[1]]['extra_' . $field_details[1] . '*'] = $selected_value;
                            }
                        }
                    }
                    break;
                case ExtraField::FIELD_TYPE_DIVIDER:
                    $form->addElement('static', $field_details[1], '<br /><strong>' . $field_details[3] . '</strong>');
                    break;
                case ExtraField::FIELD_TYPE_TAG:
                    //the magic should be here
                    $user_tags = UserManager::get_user_tags($user_id, $field_details[0]);
                    $tag_list = '';
                    if (is_array($user_tags) && count($user_tags) > 0) {
                        foreach ($user_tags as $tag) {
                            $tag_list .= '<option value="' . $tag['tag'] . '" class="selected">' . $tag['tag'] . '</option>';
                        }
                    }
                    $multi_select = '<select id="extra_' . $field_details[1] . '" name="extra_' . $field_details[1] . '">
                                    ' . $tag_list . '
                                    </select>';
                    $form->addElement('label', $field_details[3], $multi_select);
                    $url = api_get_path(WEB_AJAX_PATH) . 'user_manager.ajax.php';
                    $complete_text = get_lang('StartToType');
                    //if cache is set to true the jquery will be called 1 time
                    $jquery_ready_content = <<<EOF
                    \$("#extra_{$field_details['1']}").fcbkcomplete({
                        json_url: "{$url}?a=search_tags&field_id={$field_details['0']}",
                        cache: false,
                        filter_case: true,
                        filter_hide: true,
                        complete_text:"{$complete_text}",
                        firstselected: true,
                        //onremove: "testme",
                        //onselect: "testme",
                        filter_selected: true,
                        newel: true
                    });
EOF;
                    break;
                case ExtraField::FIELD_TYPE_TIMEZONE:
                    $form->addElement('select', 'extra_' . $field_details[1], $field_details[3], api_get_timezones(), '');
                    if ($field_details[7] == 0) {
                        $form->freeze('extra_' . $field_details[1]);
                    }
                    break;
                case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
                    // get the social network's favicon
                    $icon_path = UserManager::get_favicon_from_url($extra_data['extra_' . $field_details[1]], $field_details[4]);
                    // special hack for hi5
                    $leftpad = '1.7';
                    $top = '0.4';
                    $domain = parse_url($icon_path, PHP_URL_HOST);
                    if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
                        $leftpad = '3';
                        $top = '0';
                    }
                    // print the input field
                    $form->addElement('text', 'extra_' . $field_details[1], $field_details[3], array('size' => 60, 'style' => 'background-image: url(\'' . $icon_path . '\'); background-repeat: no-repeat; background-position: 0.4em ' . $top . 'em; padding-left: ' . $leftpad . 'em; '));
                    $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
                    $form->applyFilter('extra_' . $field_details[1], 'trim');
                    if ($field_details[7] == 0) {
                        $form->freeze('extra_' . $field_details[1]);
                    }
                    break;
                case ExtraField::FIELD_TYPE_FILE:
                    $extra_field = 'extra_' . $field_details[1];
                    $form->addElement('file', $extra_field, $field_details[3], null, '');
                    if ($extra_file_list = UserManager::build_user_extra_file_list($user_id, $field_details[1], '', true)) {
                        $form->addElement('static', $extra_field . '_list', null, $extra_file_list);
                    }
                    if ($field_details[7] == 0) {
                        $form->freeze($extra_field);
                    }
                    break;
                case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
                    $form->addElement('text', 'extra_' . $field_details[1], $field_details[3] . " (" . get_lang('CountryDialCode') . ")", array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx'));
                    $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
                    $form->applyFilter('extra_' . $field_details[1], 'trim');
                    $form->applyFilter('extra_' . $field_details[1], 'mobile_phone_number_filter');
                    $form->addRule('extra_' . $field_details[1], get_lang('MobilePhoneNumberWrong'), 'mobile_phone_number');
                    if (!$admin_permissions) {
                        if ($field_details[7] == 0) {
                            $form->freeze('extra_' . $field_details[1]);
                        }
                    }
                    break;
            }
        }
        $return = array();
        $return['jquery_ready_content'] = $jquery_ready_content;
        return $return;
    }
 /**
  * @param FormValidator $form
  *
  * @return array
  */
 public static function setForm(FormValidator &$form, $sessionId = 0)
 {
     $categoriesList = SessionManager::get_all_session_category();
     $userInfo = api_get_user_info();
     $categoriesOptions = array('0' => get_lang('None'));
     if ($categoriesList != false) {
         foreach ($categoriesList as $categoryItem) {
             $categoriesOptions[$categoryItem['id']] = $categoryItem['name'];
         }
     }
     // Database Table Definitions
     $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
     $form->addElement('text', 'name', get_lang('SessionName'), array('maxlength' => 50));
     $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
     $form->addRule('name', get_lang('SessionNameAlreadyExists'), 'callback', 'check_session_name');
     if (!api_is_platform_admin() && api_is_teacher()) {
         $form->addElement('select', 'coach_username', get_lang('CoachName'), [api_get_user_id() => $userInfo['complete_name']], array('id' => 'coach_username', 'class' => 'chzn-select', 'style' => 'width:370px;'));
     } else {
         $sql = "SELECT COUNT(1) FROM {$tbl_user} WHERE status = 1";
         $rs = Database::query($sql);
         $countUsers = Database::result($rs, 0, 0);
         if (intval($countUsers) < 50) {
             $orderClause = "ORDER BY ";
             $orderClause .= api_sort_by_first_name() ? "firstname, lastname, username" : "lastname, firstname, username";
             $sql = "SELECT user_id, lastname, firstname, username\n                        FROM {$tbl_user}\n                        WHERE status = '1' " . $orderClause;
             if (api_is_multiple_url_enabled()) {
                 $userRelAccessUrlTable = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
                 $accessUrlId = api_get_current_access_url_id();
                 if ($accessUrlId != -1) {
                     $sql = "SELECT user.user_id, username, lastname, firstname\n                        FROM {$tbl_user} user\n                        INNER JOIN {$userRelAccessUrlTable} url_user\n                        ON (url_user.user_id = user.user_id)\n                        WHERE\n                            access_url_id = {$accessUrlId} AND\n                            status = 1 " . $orderClause;
                 }
             }
             $result = Database::query($sql);
             $coachesList = Database::store_result($result);
             $coachesOptions = array();
             foreach ($coachesList as $coachItem) {
                 $coachesOptions[$coachItem['user_id']] = api_get_person_name($coachItem['firstname'], $coachItem['lastname']) . ' (' . $coachItem['username'] . ')';
             }
             $form->addElement('select', 'coach_username', get_lang('CoachName'), $coachesOptions);
         } else {
             $form->addElement('select_ajax', 'coach_username', get_lang('CoachName'), null, ['url' => api_get_path(WEB_AJAX_PATH) . 'session.ajax.php?a=search_general_coach', 'width' => '100%']);
         }
     }
     $form->addRule('coach_username', get_lang('ThisFieldIsRequired'), 'required');
     $form->addHtml('<div id="ajax_list_coachs"></div>');
     $form->addButtonAdvancedSettings('advanced_params');
     $form->addElement('html', '<div id="advanced_params_options" style="display:none">');
     $form->addSelect('session_category', get_lang('SessionCategory'), $categoriesOptions, array('id' => 'session_category', 'class' => 'chzn-select', 'style' => 'width:370px;'));
     $form->addHtmlEditor('description', get_lang('Description'), false, false, array('ToolbarSet' => 'Minimal'));
     $form->addElement('checkbox', 'show_description', null, get_lang('ShowDescription'));
     $visibilityGroup = array();
     $visibilityGroup[] = $form->createElement('select', 'session_visibility', null, array(SESSION_VISIBLE_READ_ONLY => get_lang('SessionReadOnly'), SESSION_VISIBLE => get_lang('SessionAccessible'), SESSION_INVISIBLE => api_ucfirst(get_lang('SessionNotAccessible'))));
     $form->addGroup($visibilityGroup, 'visibility_group', get_lang('SessionVisibility'), null, false);
     $options = [0 => get_lang('ByDuration'), 1 => get_lang('ByDates')];
     $form->addSelect('access', get_lang('Access'), $options, array('onchange' => 'accessSwitcher()', 'id' => 'access'));
     $form->addElement('html', '<div id="duration" style="display:none">');
     $form->addElement('number', 'duration', array(get_lang('SessionDurationTitle'), get_lang('SessionDurationDescription')), array('maxlength' => 50));
     $form->addElement('html', '</div>');
     $form->addElement('html', '<div id="date_fields" style="display:none">');
     // Dates
     $form->addDateTimePicker('access_start_date', array(get_lang('SessionStartDate'), get_lang('SessionStartDateComment')), array('id' => 'access_start_date'));
     $form->addDateTimePicker('access_end_date', array(get_lang('SessionEndDate'), get_lang('SessionEndDateComment')), array('id' => 'access_end_date'));
     $form->addRule(array('access_start_date', 'access_end_date'), get_lang('StartDateMustBeBeforeTheEndDate'), 'compare_datetime_text', '< allow_empty');
     $form->addDateTimePicker('display_start_date', array(get_lang('SessionDisplayStartDate'), get_lang('SessionDisplayStartDateComment')), array('id' => 'display_start_date'));
     $form->addDateTimePicker('display_end_date', array(get_lang('SessionDisplayEndDate'), get_lang('SessionDisplayEndDateComment')), array('id' => 'display_end_date'));
     $form->addRule(array('display_start_date', 'display_end_date'), get_lang('StartDateMustBeBeforeTheEndDate'), 'compare_datetime_text', '< allow_empty');
     $form->addDateTimePicker('coach_access_start_date', array(get_lang('SessionCoachStartDate'), get_lang('SessionCoachStartDateComment')), array('id' => 'coach_access_start_date'));
     $form->addDateTimePicker('coach_access_end_date', array(get_lang('SessionCoachEndDate'), get_lang('SessionCoachEndDateComment')), array('id' => 'coach_access_end_date'));
     $form->addRule(array('coach_access_start_date', 'coach_access_end_date'), get_lang('StartDateMustBeBeforeTheEndDate'), 'compare_datetime_text', '< allow_empty');
     $form->addElement('html', '</div>');
     $form->addCheckBox('send_subscription_notification', [get_lang('SendSubscriptionNotification'), get_lang('SendAnEmailWhenAUserBeingSubscribed')]);
     // Extra fields
     $extra_field = new ExtraField('session');
     $extra = $extra_field->addElements($form, $sessionId);
     $form->addElement('html', '</div>');
     $js = $extra['jquery_ready_content'];
     return ['js' => $js];
 }
    /**
     * @param FormValidator $form
     * @param array $extraData
     * @param bool $admin_permissions
     * @param int $user_id
     * @param array $extra
     * @param int $itemId
     * @param array $exclude variables of extra field to exclude
     * @return array
     */
    public function set_extra_fields_in_form($form, $extraData, $admin_permissions = false, $extra = array(), $itemId = null, $exclude = [])
    {
        $type = $this->type;
        $jquery_ready_content = null;
        if (!empty($extra)) {
            foreach ($extra as $field_details) {
                // Getting default value id if is set
                $defaultValueId = null;
                if (isset($field_details['options']) && !empty($field_details['options'])) {
                    $valueToFind = null;
                    if (isset($field_details['field_default_value'])) {
                        $valueToFind = $field_details['field_default_value'];
                    }
                    // If a value is found we override the default value
                    if (isset($extraData['extra_' . $field_details['variable']])) {
                        $valueToFind = $extraData['extra_' . $field_details['variable']];
                    }
                    foreach ($field_details['options'] as $option) {
                        if ($option['option_value'] == $valueToFind) {
                            $defaultValueId = $option['id'];
                        }
                    }
                }
                if (!$admin_permissions) {
                    if ($field_details['visible'] == 0) {
                        continue;
                    }
                    if (in_array($field_details['variable'], $exclude)) {
                        continue;
                    }
                }
                switch ($field_details['field_type']) {
                    case ExtraField::FIELD_TYPE_TEXT:
                        $form->addElement('text', 'extra_' . $field_details['variable'], $field_details['display_text'], array());
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_TEXTAREA:
                        $form->addHtmlEditor('extra_' . $field_details['variable'], $field_details['display_text'], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130'));
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_RADIO:
                        $group = array();
                        if (isset($field_details['options']) && !empty($field_details['options'])) {
                            foreach ($field_details['options'] as $option_details) {
                                $options[$option_details['option_value']] = $option_details['display_text'];
                                $group[] = $form->createElement('radio', 'extra_' . $field_details['variable'], $option_details['option_value'], $option_details['display_text'] . '<br />', $option_details['option_value']);
                            }
                        }
                        $form->addGroup($group, 'extra_' . $field_details['variable'], $field_details['display_text'], '');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_CHECKBOX:
                        $group = array();
                        if (isset($field_details['options']) && !empty($field_details['options'])) {
                            foreach ($field_details['options'] as $option_details) {
                                $options[$option_details['option_value']] = $option_details['display_text'];
                                $group[] = $form->createElement('checkbox', 'extra_' . $field_details['variable'], $option_details['option_value'], $option_details['display_text'] . '<br />', $option_details['option_value']);
                            }
                        } else {
                            $fieldVariable = "extra_{$field_details['variable']}";
                            $checkboxAttributes = array();
                            if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
                                $checkboxAttributes['checked'] = true;
                            }
                            // We assume that is a switch on/off with 1 and 0 as values
                            $group[] = $form->createElement('checkbox', 'extra_' . $field_details['variable'], null, get_lang('Yes'), $checkboxAttributes);
                        }
                        $form->addGroup($group, 'extra_' . $field_details['variable'], $field_details['display_text'], '');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_SELECT:
                        $get_lang_variables = false;
                        if (in_array($field_details['variable'], array('mail_notify_message', 'mail_notify_invitation', 'mail_notify_group_message'))) {
                            $get_lang_variables = true;
                        }
                        // Get extra field workflow
                        $userInfo = api_get_user_info();
                        $addOptions = array();
                        $optionsExists = false;
                        global $app;
                        // Check if exist $app['orm.em'] object
                        if (isset($app['orm.em']) && is_object($app['orm.em'])) {
                            $optionsExists = $app['orm.em']->getRepository('ChamiloLMS\\Entity\\ExtraFieldOptionRelFieldOption')->findOneBy(array('fieldId' => $field_details['id']));
                        }
                        if ($optionsExists) {
                            if (isset($userInfo['status']) && !empty($userInfo['status'])) {
                                $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\\Entity\\ExtraFieldOptionRelFieldOption')->findBy(array('fieldId' => $field_details['id'], 'relatedFieldOptionId' => $defaultValueId, 'roleId' => $userInfo['status']));
                                foreach ($fieldWorkFlow as $item) {
                                    $addOptions[] = $item->getFieldOptionId();
                                }
                            }
                        }
                        $options = array();
                        if (empty($defaultValueId)) {
                            $options[''] = get_lang('SelectAnOption');
                        }
                        $optionList = array();
                        if (!empty($field_details['options'])) {
                            foreach ($field_details['options'] as $option_details) {
                                $optionList[$option_details['id']] = $option_details;
                                if ($get_lang_variables) {
                                    $options[$option_details['option_value']] = get_lang($option_details['display_text']);
                                } else {
                                    if ($optionsExists) {
                                        // Adding always the default value
                                        if ($option_details['id'] == $defaultValueId) {
                                            $options[$option_details['option_value']] = $option_details['display_text'];
                                        } else {
                                            if (isset($addOptions) && !empty($addOptions)) {
                                                // Parsing filters
                                                if (in_array($option_details['id'], $addOptions)) {
                                                    $options[$option_details['option_value']] = $option_details['display_text'];
                                                }
                                            }
                                        }
                                    } else {
                                        // Normal behaviour
                                        $options[$option_details['option_value']] = $option_details['display_text'];
                                    }
                                }
                            }
                            if (isset($optionList[$defaultValueId])) {
                                if (isset($optionList[$defaultValueId]['option_value']) && $optionList[$defaultValueId]['option_value'] == 'aprobada') {
                                    // @todo function don't exists api_is_question_manager
                                    /*if (api_is_question_manager() == false) {
                                          $form->freeze();
                                      }*/
                                }
                            }
                            // Setting priority message
                            if (isset($optionList[$defaultValueId]) && isset($optionList[$defaultValueId]['priority'])) {
                                if (!empty($optionList[$defaultValueId]['priority'])) {
                                    $priorityId = $optionList[$defaultValueId]['priority'];
                                    $option = new ExtraFieldOption($this->type);
                                    $messageType = $option->getPriorityMessageType($priorityId);
                                    $form->addElement('label', null, Display::return_message($optionList[$defaultValueId]['priority_message'], $messageType));
                                }
                            }
                        }
                        if ($get_lang_variables) {
                            $field_details['display_text'] = get_lang($field_details['display_text']);
                        }
                        // chzn-select doesn't work for sessions??
                        $form->addElement('select', 'extra_' . $field_details['variable'], $field_details['display_text'], $options, array('id' => 'extra_' . $field_details['variable']));
                        /* Enable this when field_loggeable is introduced as a table field (2.0)
                                                if ($optionsExists && $field_details['field_loggeable'] && !empty($defaultValueId)) {
                        
                                                    $form->addElement(
                                                        'textarea',
                                                        'extra_' . $field_details['variable'] . '_comment',
                                                        $field_details['display_text'] . ' ' . get_lang('Comment')
                                                    );
                        
                                                    $extraFieldValue = new ExtraFieldValue($this->type);
                                                    $repo = $app['orm.em']->getRepository($extraFieldValue->entityName);
                                                    $repoLog = $app['orm.em']->getRepository('Gedmo\Loggable\Entity\LogEntry');
                                                    $newEntity = $repo->findOneBy(
                                                        array(
                                                            $this->handlerEntityId => $itemId,
                                                            'fieldId' => $field_details['id']
                                                        )
                                                    );
                                                    // @todo move this in a function inside the class
                                                    if ($newEntity) {
                                                        $logs = $repoLog->getLogEntries($newEntity);
                                                        if (!empty($logs)) {
                                                            $html = '<b>' . get_lang('LatestChanges') . '</b><br /><br />';
                        
                                                            $table = new HTML_Table(array('class' => 'data_table'));
                                                            $table->setHeaderContents(0, 0, get_lang('Value'));
                                                            $table->setHeaderContents(0, 1, get_lang('Comment'));
                                                            $table->setHeaderContents(0, 2, get_lang('ModifyDate'));
                                                            $table->setHeaderContents(0, 3, get_lang('Username'));
                                                            $row = 1;
                                                            foreach ($logs as $log) {
                                                                $column = 0;
                                                                $data = $log->getData();
                                                                $fieldValue = isset($data['fieldValue']) ? $data['fieldValue'] : null;
                                                                $comment = isset($data['comment']) ? $data['comment'] : null;
                        
                                                                $table->setCellContents($row, $column, $fieldValue);
                                                                $column++;
                                                                $table->setCellContents($row, $column, $comment);
                                                                $column++;
                                                                $table->setCellContents($row, $column, api_get_local_time($log->getLoggedAt()->format('Y-m-d H:i:s')));
                                                                $column++;
                                                                $table->setCellContents($row, $column, $log->getUsername());
                                                                $row++;
                                                            }
                                                            $form->addElement('label', null, $html.$table->toHtml());
                                                        }
                                                    }
                                                }
                                                */
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
                        $options = array();
                        foreach ($field_details['options'] as $option_id => $option_details) {
                            $options[$option_details['option_value']] = $option_details['display_text'];
                        }
                        $form->addElement('select', 'extra_' . $field_details['variable'], $field_details['display_text'], $options, array('multiple' => 'multiple'));
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_DATE:
                        $form->addDatePicker('extra_' . $field_details['variable'], $field_details['display_text']);
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        $form->applyFilter('theme', 'trim');
                        break;
                    case ExtraField::FIELD_TYPE_DATETIME:
                        $form->addDateTimePicker('extra_' . $field_details['variable'], $field_details['display_text']);
                        $defaults['extra_' . $field_details['variable']] = api_get_local_time();
                        if (!isset($form->_defaultValues['extra_' . $field_details['variable']])) {
                            $form->setDefaults($defaults);
                        }
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        $form->applyFilter('theme', 'trim');
                        break;
                    case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
                        $first_select_id = 'first_extra_' . $field_details['variable'];
                        $url = api_get_path(WEB_AJAX_PATH) . 'extra_field.ajax.php?1=1';
                        $jquery_ready_content .= '
                        $("#' . $first_select_id . '").on("change", function() {
                            var id = $(this).val();
                            if (id) {
                                $.ajax({
                                    url: "' . $url . '&a=get_second_select_options",
                                    dataType: "json",
                                    data: "type=' . $type . '&field_id=' . $field_details['id'] . '&option_value_id="+id,
                                    success: function(data) {
                                        $("#second_extra_' . $field_details['variable'] . '").empty();
                                        $.each(data, function(index, value) {
                                            $("#second_extra_' . $field_details['variable'] . '").append($("<option/>", {
                                                value: index,
                                                text: value
                                            }));
                                        });
                                    },
                                });
                            } else {
                                $("#second_extra_' . $field_details['variable'] . '").empty();
                            }
                        });';
                        $first_id = null;
                        $second_id = null;
                        if (!empty($extraData)) {
                            $first_id = $extraData['extra_' . $field_details['variable']]['extra_' . $field_details['variable']];
                            $second_id = $extraData['extra_' . $field_details['variable']]['extra_' . $field_details['variable'] . '_second'];
                        }
                        $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array($field_details['options']);
                        $values = array('' => get_lang('Select'));
                        $second_values = array();
                        if (!empty($options)) {
                            foreach ($options as $option) {
                                foreach ($option as $sub_option) {
                                    if ($sub_option['option_value'] == '0') {
                                        $values[$sub_option['id']] = $sub_option['display_text'];
                                    } else {
                                        if ($first_id === $sub_option['option_value']) {
                                            $second_values[$sub_option['id']] = $sub_option['display_text'];
                                        }
                                    }
                                }
                            }
                        }
                        $group = array();
                        $group[] = $form->createElement('select', 'extra_' . $field_details['variable'], null, $values, array('id' => $first_select_id));
                        $group[] = $form->createElement('select', 'extra_' . $field_details['variable'] . '_second', null, $second_values, array('id' => 'second_extra_' . $field_details['variable']));
                        $form->addGroup($group, 'extra_' . $field_details['variable'], $field_details['display_text'], '&nbsp;');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_DIVIDER:
                        $form->addElement('static', $field_details['variable'], '<br /><strong>' . $field_details['display_text'] . '</strong>');
                        break;
                    case ExtraField::FIELD_TYPE_TAG:
                        $variable = $field_details['variable'];
                        $field_id = $field_details['id'];
                        //Added for correctly translate the extra_field
                        $get_lang_variables = false;
                        if (in_array($variable, ['tags'])) {
                            $get_lang_variables = true;
                        }
                        if ($get_lang_variables) {
                            $field_details['display_text'] = get_lang($field_details['display_text']);
                        }
                        $tagsSelect = $form->addSelect("extra_{$field_details['variable']}", $field_details['display_text']);
                        $tagsSelect->setAttribute('class', null);
                        $tagsSelect->setAttribute('id', "extra_{$field_details['variable']}");
                        $tagsSelect->setMultiple(true);
                        if ($this->type == 'user') {
                            /* //the magic should be here
                                                        $user_tags = UserManager::get_user_tags($user_id, $field_details[0]);
                            
                                                        $tag_list = '';
                                                        if (is_array($user_tags) && count($user_tags) > 0) {
                                                            foreach ($user_tags as $tag) {
                                                                $tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>';
                                                            }
                                                        }
                            
                                                        $multi_select = '<select id="extra_'.$field_details[1].'" name="extra_'.$field_details[1].'">
                                                                '.$tag_list.'
                                                                </select>';
                            
                                                        $form->addElement('label', $field_details[3], $multi_select);
                                                        $url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php';
                                                        $complete_text = get_lang('StartToType');
                                                        //if cache is set to true the jquery will be called 1 time
                                                        $jquery_ready_content = <<<EOF
                                                $("#extra_$field_details[1]").fcbkcomplete({
                                                    json_url: "$url?a=search_tags&field_id=$field_details[0]",
                                                    cache: false,
                                                    filter_case: true,
                                                    filter_hide: true,
                                                    complete_text:"$complete_text",
                                                    firstselected: true,
                                                    //onremove: "testme",
                                                    //onselect: "testme",
                                                    filter_selected: true,
                                                    newel: true
                                                });
                            EOF;
                                                        break;*/
                            // The magic should be here
                            $user_tags = UserManager::get_user_tags($itemId, $field_details['id']);
                            if (is_array($user_tags) && count($user_tags) > 0) {
                                foreach ($user_tags as $tag) {
                                    $tagsSelect->addOption($tag['tag'], $tag['tag'], ['selected' => 'selected', 'class' => 'selected']);
                                }
                            }
                            $url = api_get_path(WEB_AJAX_PATH) . 'user_manager.ajax.php';
                        } else {
                            $em = Database::getManager();
                            $fieldTags = $em->getRepository('ChamiloCoreBundle:ExtraFieldRelTag')->findBy(['fieldId' => $field_id, 'itemId' => $itemId]);
                            foreach ($fieldTags as $fieldTag) {
                                $tag = $em->find('ChamiloCoreBundle:Tag', $fieldTag->getTagId());
                                if (empty($tag)) {
                                    continue;
                                }
                                $tagsSelect->addOption($tag->getTag(), $tag->getTag(), ['selected' => 'selected', 'class' => 'selected']);
                            }
                            $url = api_get_path(WEB_AJAX_PATH) . 'extra_field.ajax.php';
                        }
                        $complete_text = get_lang('StartToType');
                        //if cache is set to true the jquery will be called 1 time
                        $jquery_ready_content .= <<<EOF
                    \$("#extra_{$variable}").fcbkcomplete({
                        json_url: "{$url}?a=search_tags&field_id={$field_id}&type={$this->type}",
                        cache: false,
                        filter_case: true,
                        filter_hide: true,
                        complete_text:"{$complete_text}",
                        firstselected: false,
                        filter_selected: true,
                        newel: true
                    });
EOF;
                        break;
                    case ExtraField::FIELD_TYPE_TIMEZONE:
                        $form->addElement('select', 'extra_' . $field_details['variable'], $field_details['display_text'], api_get_timezones(), '');
                        if ($field_details['visible'] == 0) {
                            $form->freeze('extra_' . $field_details['variable']);
                        }
                        break;
                    case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
                        // get the social network's favicon
                        $extra_data_variable = isset($extraData['extra_' . $field_details['variable']]) ? $extraData['extra_' . $field_details['variable']] : null;
                        $field_default_value = isset($field_details['field_default_value']) ? $field_details['field_default_value'] : null;
                        $icon_path = UserManager::get_favicon_from_url($extra_data_variable, $field_default_value);
                        // special hack for hi5
                        $leftpad = '1.7';
                        $top = '0.4';
                        $domain = parse_url($icon_path, PHP_URL_HOST);
                        if ($domain == 'www.hi5.com' or $domain == 'hi5.com') {
                            $leftpad = '3';
                            $top = '0';
                        }
                        // print the input field
                        $form->addElement('text', 'extra_' . $field_details['variable'], $field_details['display_text'], array('size' => 60, 'style' => 'background-image: url(\'' . $icon_path . '\'); background-repeat: no-repeat; background-position: 0.4em ' . $top . 'em; padding-left: ' . $leftpad . 'em; '));
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        if ($field_details['visible'] == 0) {
                            $form->freeze('extra_' . $field_details['variable']);
                        }
                        break;
                    case ExtraField::FIELD_TYPE_MOBILE_PHONE_NUMBER:
                        $form->addElement('text', 'extra_' . $field_details[1], $field_details[3] . " (" . get_lang('CountryDialCode') . ")", array('size' => 40, 'placeholder' => '(xx)xxxxxxxxx'));
                        $form->applyFilter('extra_' . $field_details[1], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details[1], 'trim');
                        $form->applyFilter('extra_' . $field_details[1], 'mobile_phone_number_filter');
                        $form->addRule('extra_' . $field_details[1], get_lang('MobilePhoneNumberWrong'), 'mobile_phone_number');
                        if ($field_details['visible'] == 0) {
                            $form->freeze('extra_' . $field_details['variable']);
                        }
                        break;
                    case ExtraField::FIELD_TYPE_INTEGER:
                        $form->addElement('number', 'extra_' . $field_details['variable'], $field_details['display_text'], array('class' => 'span1', 'step' => 1));
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        $form->applyFilter('extra_' . $field_details['variable'], 'intval');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_FILE_IMAGE:
                        $fieldVariable = "extra_{$field_details['variable']}";
                        $fieldTexts = array($field_details['display_text']);
                        if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
                            if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
                                $fieldTexts[] = Display::img(api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable], $field_details['display_text'], array('width' => '300'));
                            }
                        }
                        $form->addElement('file', $fieldVariable, $fieldTexts, array('accept' => 'image/*'));
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        $allowed_picture_types = array('jpg', 'jpeg', 'png', 'gif');
                        $form->addRule('extra_' . $field_details['variable'], get_lang('OnlyImagesAllowed') . ' (' . implode(',', $allowed_picture_types) . ')', 'filetype', $allowed_picture_types);
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_FLOAT:
                        $form->addElement('number', 'extra_' . $field_details['variable'], $field_details['display_text'], array('class' => 'span1', 'step' => '0.01'));
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        $form->applyFilter('extra_' . $field_details['variable'], 'floatval');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_FILE:
                        $fieldVariable = "extra_{$field_details['variable']}";
                        $fieldTexts = array($field_details['display_text']);
                        if (is_array($extraData) && array_key_exists($fieldVariable, $extraData)) {
                            if (file_exists(api_get_path(SYS_UPLOAD_PATH) . $extraData[$fieldVariable])) {
                                $fieldTexts[] = Display::url(api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable], api_get_path(WEB_UPLOAD_PATH) . $extraData[$fieldVariable], array('title' => $field_details['display_text'], 'target' => '_blank'));
                            }
                        }
                        $form->addElement('file', $fieldVariable, $fieldTexts, array());
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        $form->applyFilter('extra_' . $field_details['variable'], 'trim');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_VIDEO_URL:
                        //Added for correctly translate the extra_field
                        $get_lang_variables = false;
                        if (in_array($field_details['variable'], ['video_url'])) {
                            $get_lang_variables = true;
                        }
                        if ($get_lang_variables) {
                            $field_details['display_text'] = get_lang($field_details['display_text']);
                        }
                        $form->addUrl("extra_{$field_details['variable']}", $field_details['display_text'], false, ['placeholder' => 'https://']);
                        break;
                    case ExtraField::FIELD_TYPE_LETTERS_ONLY:
                        $form->addTextLettersOnly("extra_{$field_details['variable']}", $field_details['display_text']);
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_ALPHANUMERIC:
                        $form->addTextAlphanumeric("extra_{$field_details['variable']}", $field_details['display_text']);
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_LETTERS_SPACE:
                        $form->addTextLettersAndSpaces("extra_{$field_details['variable']}", $field_details['display_text']);
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                    case ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
                        $form->addTextAlphanumericAndSpaces("extra_{$field_details['variable']}", $field_details['display_text']);
                        $form->applyFilter('extra_' . $field_details['variable'], 'stripslashes');
                        if (!$admin_permissions) {
                            if ($field_details['visible'] == 0) {
                                $form->freeze('extra_' . $field_details['variable']);
                            }
                        }
                        break;
                }
            }
        }
        $return = array();
        $return['jquery_ready_content'] = $jquery_ready_content;
        return $return;
    }
 // Calendar list
 $groupList = GroupManager::get_group_list();
 $groupIdList = array('--');
 foreach ($groupList as $group) {
     $groupIdList[$group['id']] = $group['name'];
 }
 echo Display::page_subheader(get_lang('CalendarList'));
 echo '<ul class="list-group">';
 if (!empty($attendance_calendar)) {
     foreach ($attendance_calendar as $calendar) {
         echo '<li class="list-group-item">';
         if (isset($action) && $action == 'calendar_edit' && (isset($calendar_id) && $calendar_id == $calendar['id'])) {
             // calendar edit form
             echo '<div class="attendance-calendar-edit">';
             $form = new FormValidator('attendance_calendar_edit', 'POST', 'index.php?action=calendar_edit&attendance_id=' . $attendance_id . '&calendar_id=' . $calendar_id . '&' . api_get_cidreq(), '');
             $form->addDateTimePicker('date_time', array(get_lang('Date')), array('form_name' => 'attendance_calendar_edit'), 5);
             $defaults['date_time'] = $calendar['date_time'];
             $form->addButtonSave(get_lang('Save'));
             $form->addButtonCancel(get_lang('Cancel'), 'cancel');
             $form->setDefaults($defaults);
             $form->display();
             echo '</div>';
         } else {
             echo Display::return_icon('lp_calendar_event.png', get_lang('DateTime'), null, ICON_SIZE_MEDIUM) . ' ' . substr($calendar['date_time'], 0, strlen($calendar['date_time']) - 3) . '&nbsp;';
             if (isset($calendar['groups']) && !empty($calendar['groups'])) {
                 foreach ($calendar['groups'] as $group) {
                     echo '&nbsp;' . Display::label($groupIdList[$group['group_id']]);
                 }
             }
             if (!$is_locked_attendance || api_is_platform_admin()) {
                 if (api_is_allowed_to_edit()) {