Adding LP extra field see BT#7975

1.10.x
Julio Montoya 11 years ago
parent e5b5042e82
commit e8c1cdcc2b
  1. 71
      main/admin/extra_fields.php
  2. 9
      main/inc/ajax/extra_field.ajax.php
  3. 166
      main/inc/lib/extra_field.lib.php
  4. 125
      main/inc/lib/extra_field_option.lib.php
  5. 281
      main/inc/lib/extra_field_value.lib.php
  6. 6
      main/newscorm/lp_controller.php
  7. 22
      main/newscorm/lp_edit.php

@ -10,6 +10,9 @@ $language_file = array('admin');
$cidReset = true; $cidReset = true;
$cidReset = true;
require_once '../inc/global.inc.php';
$extraFieldType = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; $extraFieldType = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
$this_section = SECTION_PLATFORM_ADMIN; $this_section = SECTION_PLATFORM_ADMIN;
@ -25,7 +28,6 @@ $interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin'
$tool_name = null; $tool_name = null;
$action = isset($_GET['action']) ? $_GET['action'] : null; $action = isset($_GET['action']) ? $_GET['action'] : null;
if (!in_array($extraFieldType, ExtraField::getValidExtraFieldTypes())) { if (!in_array($extraFieldType, ExtraField::getValidExtraFieldTypes())) {
api_not_allowed(); api_not_allowed();
} }
@ -116,11 +118,11 @@ switch ($action) {
// The validation or display // The validation or display
if ($form->validate()) { if ($form->validate()) {
//if ($check) { //if ($check) {
$values = $form->exportValues(); $values = $form->exportValues();
$res = $obj->save($values); $res = $obj->save($values);
if ($res) { if ($res) {
Display::display_confirmation_message(get_lang('ItemAdded')); Display::display_confirmation_message(get_lang('ItemAdded'));
} }
//} //}
$obj->display(); $obj->display();
} else { } else {
@ -140,9 +142,9 @@ switch ($action) {
// The validation or display // The validation or display
if ($form->validate()) { if ($form->validate()) {
//if ($check) { //if ($check) {
$values = $form->exportValues(); $values = $form->exportValues();
$res = $obj->update($values); $res = $obj->update($values);
Display::display_confirmation_message(sprintf(get_lang('ItemUpdated'), $values['field_variable']), false); Display::display_confirmation_message(sprintf(get_lang('ItemUpdated'), $values['field_variable']), false);
//} //}
$obj->display(); $obj->display();
} else { } else {
@ -157,10 +159,10 @@ switch ($action) {
case 'delete': case 'delete':
// Action handling: delete // Action handling: delete
//if ($check) { //if ($check) {
$res = $obj->delete($_GET['id']); $res = $obj->delete($_GET['id']);
if ($res) { if ($res) {
Display::display_confirmation_message(get_lang('ItemDeleted')); Display::display_confirmation_message(get_lang('ItemDeleted'));
} }
//} //}
$obj->display(); $obj->display();
break; break;
@ -169,3 +171,46 @@ switch ($action) {
break; break;
} }
Display :: display_footer(); Display :: display_footer();
/*
CREATE TABLE IF NOT EXISTS lp_field(
id INT NOT NULL auto_increment,
field_type int NOT NULL DEFAULT 1,
field_variable varchar(64) NOT NULL,
field_display_text varchar(64),
field_default_value text,
field_order int,
field_visible tinyint default 0,
field_changeable tinyint default 0,
field_filter tinyint default 0,
tms DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY(id)
);
DROP TABLE IF EXISTS lp_field_options;
CREATE TABLE IF NOT EXISTS lp_field_options (
id int NOT NULL auto_increment,
field_id int NOT NULL,
option_value text,
option_display_text varchar(64),
option_order int,
tms DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS lp_field_values;
CREATE TABLE IF NOT EXISTS lp_field_values(
id bigint NOT NULL auto_increment,
lp_id int unsigned NOT NULL,
field_id int NOT NULL,
field_value text,
comment VARCHAR(100) default '',
tms DATETIME NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY(id)
);
ALTER TABLE lp_field_values ADD INDEX (lp_id, field_id);
*/

@ -16,8 +16,15 @@ switch ($action) {
echo $field_options->get_second_select_field_options_by_field($field_id, $option_value_id, true); echo $field_options->get_second_select_field_options_by_field($field_id, $option_value_id, true);
} }
break; break;
case 'search_tags':
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null;
$fieldId = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null;
$tag = isset($_REQUEST['tag']) ? $_REQUEST['tag'] : null;
$extraFieldOption = new ExtraFieldOption($type);
echo $extraFieldOption->getSearchOptionsByField($tag, $fieldId, 10, 'json');
break;
default: default:
exit; exit;
break; break;
} }
exit; exit;

@ -104,6 +104,16 @@ class ExtraField extends Model
$this->handlerEntityId = 'questionId'; $this->handlerEntityId = 'questionId';
$this->primaryKey = 'iid'; $this->primaryKey = 'iid';
break; break;
case 'lp':
$this->table_field_options = Database::get_main_table(TABLE_MAIN_LP_FIELD_OPTIONS);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_LP_FIELD_VALUES);
// Used for the model
$this->table = Database::get_main_table(TABLE_MAIN_LP_FIELD);
$this->handler_id = 'lp_id';
$this->handlerEntityId = 'lpId';
$this->primaryKey = 'id';
break;
} }
$this->pageUrl = 'extra_fields.php?type='.$this->type; $this->pageUrl = 'extra_fields.php?type='.$this->type;
// Example QuestionFields // Example QuestionFields
@ -116,7 +126,8 @@ class ExtraField extends Model
'user', 'user',
'course', 'course',
'session', 'session',
'question' 'question',
'lp'
); );
} }
@ -197,9 +208,6 @@ class ExtraField extends Model
switch ($handler) { switch ($handler) {
case 'course': case 'course':
case 'session': case 'session':
unset($types[self::FIELD_TYPE_TAG]);
unset($types[self::FIELD_TYPE_SOCIAL_PROFILE]);
break;
case 'user': case 'user':
break; break;
} }
@ -219,6 +227,7 @@ class ExtraField extends Model
if (empty($form)) { if (empty($form)) {
return false; return false;
} }
$extra_data = false; $extra_data = false;
if (!empty($item_id)) { if (!empty($item_id)) {
$extra_data = self::get_handler_extra_data($item_id); $extra_data = self::get_handler_extra_data($item_id);
@ -226,8 +235,8 @@ class ExtraField extends Model
$form->setDefaults($extra_data); $form->setDefaults($extra_data);
} }
} }
$extra_fields = $this->get_all(null, 'option_order');
$extra_fields = $this->get_all(null, 'option_order');
$extra = $this->set_extra_fields_in_form( $extra = $this->set_extra_fields_in_form(
$form, $form,
$extra_data, $extra_data,
@ -461,21 +470,22 @@ class ExtraField extends Model
/** /**
* @param FormValidator $form * @param FormValidator $form
* @param array $extra_data * @param array $extraData
* @param string $form_name * @param string $form_name
* @param bool $admin_permissions * @param bool $admin_permissions
* @param null $user_id * @param int $user_id
* @param string $type * @param array $extra
* @param null $extra * @param int $itemId
*
* @return array * @return array
*/ */
public function set_extra_fields_in_form( public function set_extra_fields_in_form(
$form, $form,
$extra_data, $extraData,
$form_name, $form_name,
$admin_permissions = false, $admin_permissions = false,
$user_id = null, $user_id = null,
$extra = null, $extra = array(),
$itemId = null $itemId = null
) { ) {
$user_id = intval($user_id); $user_id = intval($user_id);
@ -499,8 +509,8 @@ class ExtraField extends Model
$valueToFind = $field_details['field_default_value']; $valueToFind = $field_details['field_default_value'];
} }
// If a value is found we override the default value // If a value is found we override the default value
if (isset($extra_data['extra_'.$field_details['field_variable']])) { if (isset($extraData['extra_'.$field_details['field_variable']])) {
$valueToFind = $extra_data['extra_'.$field_details['field_variable']]; $valueToFind = $extraData['extra_'.$field_details['field_variable']];
} }
foreach ($field_details['options'] as $option) { foreach ($field_details['options'] as $option) {
@ -594,7 +604,7 @@ class ExtraField extends Model
); );
} }
} else { } else {
// We asume that is a switch on/off with 1 and 0 as values // We assume that is a switch on/off with 1 and 0 as values
$group[] = $form->createElement( $group[] = $form->createElement(
'checkbox', 'checkbox',
'extra_'.$field_details['field_variable'], 'extra_'.$field_details['field_variable'],
@ -641,13 +651,13 @@ class ExtraField extends Model
if (isset($userInfo['status']) && !empty($userInfo['status'])) { if (isset($userInfo['status']) && !empty($userInfo['status'])) {
$fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption') $fieldWorkFlow = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')
->findBy( ->findBy(
array( array(
'fieldId' => $field_details['id'], 'fieldId' => $field_details['id'],
'relatedFieldOptionId' => $defaultValueId, 'relatedFieldOptionId' => $defaultValueId,
'roleId' => $userInfo['status'] 'roleId' => $userInfo['status']
) )
); );
foreach ($fieldWorkFlow as $item) { foreach ($fieldWorkFlow as $item) {
$addOptions[] = $item->getFieldOptionId(); $addOptions[] = $item->getFieldOptionId();
} }
@ -838,7 +848,6 @@ class ExtraField extends Model
break; break;
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
$first_select_id = 'first_extra_'.$field_details['field_variable']; $first_select_id = 'first_extra_'.$field_details['field_variable'];
$url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1'; $url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php?1=1';
$jquery_ready_content .= ' $jquery_ready_content .= '
@ -867,9 +876,9 @@ class ExtraField extends Model
$first_id = null; $first_id = null;
$second_id = null; $second_id = null;
if (!empty($extra_data)) { if (!empty($extraData)) {
$first_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable']]; $first_id = $extraData['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable']];
$second_id = $extra_data['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable'].'_second']; $second_id = $extraData['extra_'.$field_details['field_variable']]['extra_'.$field_details['field_variable'].'_second'];
} }
$options = ExtraField::extra_field_double_select_convert_array_to_ordered_array( $options = ExtraField::extra_field_double_select_convert_array_to_ordered_array(
@ -927,29 +936,52 @@ class ExtraField extends Model
); );
break; break;
case ExtraField::FIELD_TYPE_TAG: case ExtraField::FIELD_TYPE_TAG:
//the magic should be here $field_variable = $field_details['field_variable'];
$user_tags = UserManager::get_user_tags($user_id, $field_details['id']); $field_id = $field_details['id'];
$tag_list = ''; if ($this->type == 'user') {
if (is_array($user_tags) && count($user_tags) > 0) {
foreach ($user_tags as $tag) { // The magic should be here
$tag_list .= '<option value="'.$tag['tag'].'" class="selected">'.$tag['tag'].'</option>'; $user_tags = UserManager::get_user_tags($user_id, $field_details['id']);
$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>';
}
}
$url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?';
} else {
$extraFieldValue = new ExtraFieldValue($this->type);
$tags = array();
if (!empty($itemId)) {
$tags = $extraFieldValue->getAllValuesByItemAndField($itemId, $field_id);
}
$tag_list = '';
if (is_array($tags) && count($tags) > 0) {
$extraFieldOption = new ExtraFieldOption($this->type);
foreach ($tags as $tag) {
$option = $extraFieldOption->get($tag['field_value']);
$tag_list .= '<option value="'.$option['id'].'" class="selected">'.$option['option_display_text'].'</option>';
}
} }
$url = api_get_path(WEB_AJAX_PATH).'extra_field.ajax.php';
} }
$multi_select = '<select id="extra_'.$field_details['field_variable'].'" name="extra_'.$field_details['field_variable'].'"> $form->addElement('hidden', 'extra_'.$field_details['field_variable'].'__persist__', 1);
'.$tag_list.'
</select>';
$form->addElement('label', $field_details['field_display_text'], $multi_select); $multiSelect = '<select id="extra_'.$field_details['field_variable'].'" name="extra_'.$field_details['field_variable'].'">
$url = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php'; '.$tag_list.'
</select>';
$form->addElement('label', $field_details['field_display_text'], $multiSelect);
$complete_text = get_lang('StartToType'); $complete_text = get_lang('StartToType');
//if cache is set to true the jquery will be called 1 time //if cache is set to true the jquery will be called 1 time
$field_variable = $field_details['field_variable'];
$field_id = $field_details['id'];
$jquery_ready_content .= <<<EOF $jquery_ready_content .= <<<EOF
$("#extra_$field_variable").fcbkcomplete({ $("#extra_$field_variable").fcbkcomplete({
json_url: "$url?a=search_tags&field_id=$field_id", json_url: "$url?a=search_tags&field_id=$field_id&type={$this->type}",
cache: false, cache: false,
filter_case: true, filter_case: true,
filter_hide: true, filter_hide: true,
@ -977,7 +1009,7 @@ EOF;
case ExtraField::FIELD_TYPE_SOCIAL_PROFILE: case ExtraField::FIELD_TYPE_SOCIAL_PROFILE:
// get the social network's favicon // get the social network's favicon
$icon_path = UserManager::get_favicon_from_url( $icon_path = UserManager::get_favicon_from_url(
$extra_data['extra_'.$field_details['field_variable']], $extraData['extra_'.$field_details['field_variable']],
$field_details['field_default_value'] $field_details['field_default_value']
); );
// special hack for hi5 // special hack for hi5
@ -1007,7 +1039,7 @@ EOF;
} }
} }
} }
$return = array(); $return = array();
$return['jquery_ready_content'] = $jquery_ready_content; $return['jquery_ready_content'] = $jquery_ready_content;
return $return; return $return;
@ -1035,17 +1067,17 @@ EOF;
// action links // action links
echo '<div class="actions">'; echo '<div class="actions">';
echo '<a href="../admin/index.php">'.Display::return_icon( echo '<a href="../admin/index.php">'.Display::return_icon(
'back.png', 'back.png',
get_lang('BackTo').' '.get_lang('PlatformAdmin'), get_lang('BackTo').' '.get_lang('PlatformAdmin'),
'', '',
ICON_SIZE_MEDIUM ICON_SIZE_MEDIUM
).'</a>'; ).'</a>';
echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon( echo '<a href="'.api_get_self().'?action=add&type='.$this->type.'">'.Display::return_icon(
'add_user_fields.png', 'add_user_fields.png',
get_lang('Add'), get_lang('Add'),
'', '',
ICON_SIZE_MEDIUM ICON_SIZE_MEDIUM
).'</a>'; ).'</a>';
echo '</div>'; echo '</div>';
echo Display::grid_html($this->type.'_fields'); echo Display::grid_html($this->type.'_fields');
} }
@ -1260,7 +1292,7 @@ EOF;
'', '',
ICON_SIZE_SMALL ICON_SIZE_SMALL
).'</a>'. ).'</a>'.
'&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes( '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(
api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES) api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES)
)."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon( )."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon(
'delete.png', 'delete.png',
@ -1268,8 +1300,8 @@ EOF;
'', '',
ICON_SIZE_SMALL ICON_SIZE_SMALL
).'</a>'. ).'</a>'.
'\'; '\';
}'; }';
} }
/** /**
@ -1282,7 +1314,7 @@ EOF;
{ {
$fields = $this->get_all( $fields = $this->get_all(
array('field_visible = ? AND field_filter = ?' array('field_visible = ? AND field_filter = ?'
=> array(1, 1) => array(1, 1)
), ),
'option_display_text' 'option_display_text'
); );
@ -1396,13 +1428,13 @@ EOF;
$extra['extra_field_info'] = $extra_field_info; $extra['extra_field_info'] = $extra_field_info;
if (isset($extra_field_info['field_type']) && in_array( if (isset($extra_field_info['field_type']) && in_array(
$extra_field_info['field_type'], $extra_field_info['field_type'],
array( array(
ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT,
ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT,
ExtraField::FIELD_TYPE_DOUBLE_SELECT ExtraField::FIELD_TYPE_DOUBLE_SELECT
)
) )
)
) { ) {
$inject_extra_fields .= " fvo$counter.option_display_text as {$extra['field']}, "; $inject_extra_fields .= " fvo$counter.option_display_text as {$extra['field']}, ";
} else { } else {
@ -1450,13 +1482,13 @@ EOF;
//Add options //Add options
if (isset($extra_field_info['field_type']) && in_array( if (isset($extra_field_info['field_type']) && in_array(
$extra_field_info['field_type'], $extra_field_info['field_type'],
array( array(
ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT,
ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_SELECT,
ExtraField::FIELD_TYPE_DOUBLE_SELECT ExtraField::FIELD_TYPE_DOUBLE_SELECT
)
) )
)
) { ) {
$options['where'] = str_replace( $options['where'] = str_replace(
$extra_info['field'], $extra_info['field'],

@ -32,6 +32,9 @@ class ExtraFieldOption extends Model
case 'question': case 'question':
$this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS); $this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS);
break; break;
case 'lp':
$this->table = Database::get_main_table(TABLE_MAIN_LP_FIELD_OPTIONS);
break;
} }
} }
@ -80,7 +83,7 @@ class ExtraFieldOption extends Model
*/ */
public function get_field_options_to_string($field_id, $add_id_in_array = false, $ordered_by = null) public function get_field_options_to_string($field_id, $add_id_in_array = false, $ordered_by = null)
{ {
$options = self::get_field_options_by_field($field_id, $add_id_in_array, $ordered_by); $options = self::get_field_options_by_field($field_id, $add_id_in_array, $ordered_by);
$new_options = array(); $new_options = array();
if (!empty($options)) { if (!empty($options)) {
foreach ($options as $option) { foreach ($options as $option) {
@ -103,12 +106,41 @@ class ExtraFieldOption extends Model
public function delete_all_options_by_field_id($field_id) public function delete_all_options_by_field_id($field_id)
{ {
$field_id = intval($field_id); $field_id = intval($field_id);
$sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
$r = Database::query($sql); $r = Database::query($sql);
return $r; return $r;
} }
/**
* @param array $params
*
* @return int
*/
public function saveOptions($params, $show_query = false)
{
$optionInfo = self::get_field_option_by_field_and_option($params['field_id'], $params['option_value']);
// Use URLify only for new items
//$optionValue = URLify::filter($params['option_value']);
$optionValue = replace_dangerous_char($params['option_value']);
$option = $params['option_value'];
if ($optionInfo == false) {
$order = self::get_max_order($params['field_id']);
$new_params = array(
'field_id' => $params['field_id'],
'option_value' => trim($optionValue),
'option_display_text' => trim($option),
'option_order' => $order,
'tms' => api_get_utc_datetime(),
);
return parent::save($new_params, $show_query);
}
return false;
}
/** /**
* Saves an option into the corresponding *_field_options table * Saves an option into the corresponding *_field_options table
* @param array Parameters to be considered for the insertion * @param array Parameters to be considered for the insertion
@ -257,7 +289,6 @@ class ExtraFieldOption extends Model
if ($insert_repeated) { if ($insert_repeated) {
parent::save($params, $show_query); parent::save($params, $show_query);
} else { } else {
$check = self::get_field_option_by_field_and_option($field_id, $params['option_value']); $check = self::get_field_option_by_field_and_option($field_id, $params['option_value']);
if ($check == false) { if ($check == false) {
parent::save($params, $show_query); parent::save($params, $show_query);
@ -279,7 +310,8 @@ class ExtraFieldOption extends Model
$field_id = intval($field_id); $field_id = intval($field_id);
$option_value = Database::escape_string($option_value); $option_value = Database::escape_string($option_value);
$sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_value = '".$option_value."'"; $sql = "SELECT * FROM {$this->table}
WHERE field_id = $field_id AND option_value = '".$option_value."'";
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result) > 0) { if (Database::num_rows($result) > 0) {
return Database::store_result($result, 'ASSOC'); return Database::store_result($result, 'ASSOC');
@ -297,10 +329,11 @@ class ExtraFieldOption extends Model
*/ */
public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text) public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text)
{ {
$field_id = intval($field_id); $field_id = intval($field_id);
$option_display_text = Database::escape_string($option_display_text); $option_display_text = Database::escape_string($option_display_text);
$sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."'"; $sql = "SELECT * FROM {$this->table}
WHERE field_id = $field_id AND option_display_text = '".$option_display_text."'";
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result) > 0) { if (Database::num_rows($result) > 0) {
return Database::fetch_array($result, 'ASSOC'); return Database::fetch_array($result, 'ASSOC');
@ -326,7 +359,11 @@ class ExtraFieldOption extends Model
$option_display_text = Database::escape_string($option_display_text); $option_display_text = Database::escape_string($option_display_text);
$option_value = Database::escape_string($option_value); $option_value = Database::escape_string($option_value);
$sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id AND option_display_text = '".$option_display_text."' AND option_value = '$option_value'"; $sql = "SELECT * FROM {$this->table}
WHERE
field_id = $field_id AND
option_display_text = '".$option_display_text."' AND
option_value = '$option_value'";
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result) > 0) { if (Database::num_rows($result) > 0) {
return Database::fetch_array($result, 'ASSOC'); return Database::fetch_array($result, 'ASSOC');
@ -400,7 +437,6 @@ class ExtraFieldOption extends Model
} }
return $string; return $string;
} }
return $options; return $options;
@ -461,7 +497,7 @@ class ExtraFieldOption extends Model
/** /**
* Update the option using the given params * Update the option using the given params
* @param array Parameters with the data to be saved * @param array $params data to be saved
*/ */
public function update($params) public function update($params)
{ {
@ -479,12 +515,12 @@ class ExtraFieldOption extends Model
//echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'),'', ICON_SIZE_MEDIUM).'</a>'; //echo '<a href="../admin/index.php">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('PlatformAdmin'),'', ICON_SIZE_MEDIUM).'</a>';
$field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null; $field_id = isset($_REQUEST['field_id']) ? intval($_REQUEST['field_id']) : null;
echo '<a href="'.api_get_self( echo '<a href="'.api_get_self(
).'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.Display::return_icon( ).'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.Display::return_icon(
'add_user_fields.png', 'add_user_fields.png',
get_lang('Add'), get_lang('Add'),
'', '',
ICON_SIZE_MEDIUM ICON_SIZE_MEDIUM
).'</a>'; ).'</a>';
echo '</div>'; echo '</div>';
echo Display::grid_html('extra_field_options'); echo Display::grid_html('extra_field_options');
} }
@ -525,7 +561,7 @@ class ExtraFieldOption extends Model
public function return_form($url, $action) public function return_form($url, $action)
{ {
$form_name = $this->type.'_field'; $form_name = $this->type.'_field';
$form = new FormValidator($form_name, 'post', $url); $form = new FormValidator($form_name, 'post', $url);
// Settting the form elements // Settting the form elements
$header = get_lang('Add'); $header = get_lang('Add');
if ($action == 'edit') { if ($action == 'edit') {
@ -565,4 +601,59 @@ class ExtraFieldOption extends Model
return $form; return $form;
} }
/**
* @param string $tag
* @param int $field_id
* @param int $limit
* @return array
*/
public function searchByField($tag, $field_id, $limit = 10)
{
$field_id = intval($field_id);
$limit = intval($limit);
$tag = Database::escape_string($tag);
$sql = "SELECT DISTINCT id, option_display_text
FROM {$this->table}
WHERE
field_id = '".$field_id."' AND
option_value LIKE '%$tag%'
ORDER BY option_value
LIMIT 0, $limit
";
$result = Database::query($sql);
$values = array();
if (Database::num_rows($result)) {
$values = Database::store_result($result, 'ASSOC');
}
return $values;
}
/**
* @param string $tag
* @param int $field_id
* @param int $limit
*
* @return string
*/
public function getSearchOptionsByField($tag, $field_id, $limit = 10)
{
$result = $this->searchByField($tag, $field_id, $limit = 10);
$values = array();
$json = null;
if (!empty($result)) {
foreach ($result as $item) {
$values[] = array(
'value' => $item['id'],
'caption' => $item['option_display_text'],
);
}
$json = json_encode($values);
}
return $json;
}
} }

@ -56,6 +56,12 @@ class ExtraFieldValue extends Model
$this->author_id = 'user_id'; $this->author_id = 'user_id';
$this->entityName = 'ChamiloLMS\Entity\QuestionFieldValues'; $this->entityName = 'ChamiloLMS\Entity\QuestionFieldValues';
break; break;
case 'lp':
$this->table = Database::get_main_table(TABLE_MAIN_LP_FIELD_VALUES);
$this->table_handler_field = Database::get_main_table(TABLE_MAIN_LP_FIELD);
$this->author_id = 'lp_id';
//$this->entityName = 'ChamiloLMS\Entity\QuestionFieldValues';
break;
default: default:
//unmanaged datatype, return false to let the caller know it //unmanaged datatype, return false to let the caller know it
// didn't work // didn't work
@ -86,25 +92,79 @@ class ExtraFieldValue extends Model
public function save_field_values($params) public function save_field_values($params)
{ {
$extra_field = new ExtraField($this->type); $extra_field = new ExtraField($this->type);
if (empty($params[$this->handler_id])) { if (empty($params[$this->handler_id])) {
return false; return false;
} }
foreach ($params as $key => $value) {
$found = strpos($key, '__persist__');
if ($found) {
$tempKey = str_replace('__persist__', '', $key);
if (!isset($params[$tempKey])) {
$params[$tempKey] = array();
}
break;
}
}
// Parse params. // Parse params.
foreach ($params as $key => $value) { foreach ($params as $key => $value) {
if (substr($key, 0, 6) == 'extra_') { if (substr($key, 0, 6) == 'extra_') {
// An extra field. // An extra field.
$field_variable = substr($key, 6); $field_variable = substr($key, 6);
$extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable); $extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable);
if ($extra_field_info) { if ($extra_field_info) {
$commentVariable = 'extra_'.$field_variable.'_comment'; $commentVariable = 'extra_'.$field_variable.'_comment';
$comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null; $comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null;
$new_params = array(
$this->handler_id => $params[$this->handler_id], switch ($extra_field_info['field_type']) {
'field_id' => $extra_field_info['id'], case ExtraField::FIELD_TYPE_TAG :
'field_value' => $value,
'comment' => $comment $old = self::getAllValuesByItemAndField(
); $extra_field_info['id'],
self::save($new_params); $params[$this->handler_id]
);
$deleteItems = array();
if (!empty($old)) {
$oldIds = array();
foreach ($old as $oldItem) {
$oldIds[] = $oldItem['field_value'];
}
$deleteItems = array_diff($oldIds, $value);
}
foreach ($value as $optionId) {
$new_params = array(
$this->handler_id => $params[$this->handler_id],
'field_id' => $extra_field_info['id'],
'field_value' => $optionId,
'comment' => $comment
);
self::save($new_params);
}
if (!empty($deleteItems)) {
foreach ($deleteItems as $deleteFieldValue) {
self::deleteValuesByHandlerAndFieldAndValue(
$extra_field_info['id'],
$params[$this->handler_id],
$deleteFieldValue
);
}
}
break;
default;
$new_params = array(
$this->handler_id => $params[$this->handler_id],
'field_id' => $extra_field_info['id'],
'field_value' => $value,
'comment' => $comment
);
self::save($new_params);
}
} }
} }
} }
@ -123,7 +183,6 @@ class ExtraFieldValue extends Model
// Setting value to insert. // Setting value to insert.
$value = $params['field_value']; $value = $params['field_value'];
$value_to_insert = null; $value_to_insert = null;
if (is_array($value)) { if (is_array($value)) {
@ -131,6 +190,7 @@ class ExtraFieldValue extends Model
} else { } else {
$value_to_insert = Database::escape_string($value); $value_to_insert = Database::escape_string($value);
} }
$params['field_value'] = $value_to_insert; $params['field_value'] = $value_to_insert;
//If field id exists //If field id exists
@ -138,28 +198,26 @@ class ExtraFieldValue extends Model
if ($extra_field_info) { if ($extra_field_info) {
switch ($extra_field_info['field_type']) { switch ($extra_field_info['field_type']) {
case ExtraField::FIELD_TYPE_TAG:
break;
case ExtraField::FIELD_TYPE_RADIO: case ExtraField::FIELD_TYPE_RADIO:
case ExtraField::FIELD_TYPE_SELECT: case ExtraField::FIELD_TYPE_SELECT:
case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: case ExtraField::FIELD_TYPE_SELECT_MULTIPLE:
//$field_options = $session_field_option->get_field_options_by_field($params['field_id']); //$field_options = $session_field_option->get_field_options_by_field($params['field_id']);
//$params['field_value'] = split(';', $value_to_insert); //$params['field_value'] = split(';', $value_to_insert);
/* /*
if ($field_options) { if ($field_options) {
$check = false; $check = false;
foreach ($field_options as $option) { foreach ($field_options as $option) {
if (in_array($option['option_value'], $values)) { if (in_array($option['option_value'], $values)) {
$check = true; $check = true;
break; break;
}
} }
} if (!$check) {
if (!$check) { return false; //option value not found
return false; //option value not found }
} } else {
} else { return false; //enumerated type but no option found
return false; //enumerated type but no option found }*/
}*/
break; break;
case ExtraField::FIELD_TYPE_TEXT: case ExtraField::FIELD_TYPE_TEXT:
case ExtraField::FIELD_TYPE_TEXTAREA: case ExtraField::FIELD_TYPE_TEXTAREA:
@ -168,7 +226,7 @@ class ExtraFieldValue extends Model
if (is_array($value)) { if (is_array($value)) {
if (isset($value['extra_'.$extra_field_info['field_variable']]) && if (isset($value['extra_'.$extra_field_info['field_variable']]) &&
isset($value['extra_'.$extra_field_info['field_variable'].'_second']) isset($value['extra_'.$extra_field_info['field_variable'].'_second'])
) { ) {
$value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second']; $value_to_insert = $value['extra_'.$extra_field_info['field_variable']].'::'.$value['extra_'.$extra_field_info['field_variable'].'_second'];
} else { } else {
$value_to_insert = null; $value_to_insert = null;
@ -179,7 +237,18 @@ class ExtraFieldValue extends Model
break; break;
} }
$field_values = self::get_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']); if ($extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG) {
$field_values = self::getAllValuesByItemAndFieldAndValue(
$params[$this->handler_id],
$params['field_id'],
$value
);
} else {
$field_values = self::get_values_by_handler_and_field_id(
$params[$this->handler_id],
$params['field_id']
);
}
$params['field_value'] = $value_to_insert; $params['field_value'] = $value_to_insert;
$params['tms'] = api_get_utc_datetime(); $params['tms'] = api_get_utc_datetime();
@ -221,13 +290,30 @@ class ExtraFieldValue extends Model
$app['orm.ems']['db_write']->flush(); $app['orm.ems']['db_write']->flush();
} }
} }
} else { } else {
return parent::save($params, $show_query); if ($extra_field_info['field_type'] == ExtraField::FIELD_TYPE_TAG) {
$option = new ExtraFieldOption($this->type);
$optionExists = $option->get($params['field_value']);
if (empty($optionExists)) {
$optionParams = array(
'field_id' => $params['field_id'],
'option_value' => $params['field_value']
);
$optionId = $option->saveOptions($optionParams);
} else {
$optionId = $optionExists['id'];
}
$params['field_value'] = $optionId;
if ($optionId) {
return parent::save($params, $show_query);
}
} else {
return parent::save($params, $show_query);
}
} }
} else { } else {
//self::delete_values_by_handler_and_field_id($params[$this->handler_id], $params['field_id']);
// Update // Update
if ($extra_field_info['field_loggeable'] == 1) { if ($extra_field_info['field_loggeable'] == 1) {
global $app; global $app;
@ -283,13 +369,12 @@ class ExtraFieldValue extends Model
} }
} }
/** /**
* Returns the value of the given extra field on the given resource * Returns the value of the given extra field on the given resource
* @param int Item ID (It could be a session_id, course_id or user_id) * @param int Item ID (It could be a session_id, course_id or user_id)
* @param int Field ID (the ID from the *_field table) * @param int Field ID (the ID from the *_field table)
* @param bool Whether to transform the result to a human readable strings * @param bool Whether to transform the result to a human readable strings
* @return mixed A structured array with the field_id and field_value, or fals on error * @return mixed A structured array with the field_id and field_value, or false on error
* @assert (-1,-1) === false * @assert (-1,-1) === false
*/ */
public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false) public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false)
@ -309,7 +394,6 @@ class ExtraFieldValue extends Model
if (!empty($result['field_value'])) { if (!empty($result['field_value'])) {
switch ($result['field_type']) { switch ($result['field_type']) {
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: case ExtraField::FIELD_TYPE_DOUBLE_SELECT:
$field_option = new ExtraFieldOption($this->type); $field_option = new ExtraFieldOption($this->type);
$options = explode('::', $result['field_value']); $options = explode('::', $result['field_value']);
// only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> '; // only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> ';
@ -322,7 +406,10 @@ class ExtraFieldValue extends Model
break; break;
case ExtraField::FIELD_TYPE_SELECT: case ExtraField::FIELD_TYPE_SELECT:
$field_option = new ExtraFieldOption($this->type); $field_option = new ExtraFieldOption($this->type);
$extra_field_option_result = $field_option->get_field_option_by_field_and_option($result['field_id'], $result['field_value']); $extra_field_option_result = $field_option->get_field_option_by_field_and_option(
$result['field_id'],
$result['field_value']
);
if (isset($extra_field_option_result[0])) { if (isset($extra_field_option_result[0])) {
$result['field_value'] = $extra_field_option_result[0]['option_display_text']; $result['field_value'] = $extra_field_option_result[0]['option_display_text'];
} }
@ -336,6 +423,34 @@ class ExtraFieldValue extends Model
} }
} }
/**
* @param string $tag
* @param int $field_id
* @param int $limit
* @return array
*/
public function searchValuesByField($tag, $field_id, $limit = 10)
{
$field_id = intval($field_id);
$limit = intval($limit);
$tag = Database::escape_string($tag);
$sql = "SELECT DISTINCT s.field_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
field_value LIKE '%$tag%'
ORDER BY field_value
LIMIT 0, $limit
";
$result = Database::query($sql);
$values = array();
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 * 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") * a specific original item and a field name (like "branch", or "birthdate")
@ -350,9 +465,11 @@ class ExtraFieldValue extends Model
$field_variable = Database::escape_string($field_variable); $field_variable = Database::escape_string($field_variable);
$sql = "SELECT s.*, field_type FROM {$this->table} s $sql = "SELECT s.*, field_type FROM {$this->table} s
INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id) INNER JOIN {$this->table_handler_field} sf
WHERE {$this->handler_id} = '$item_id' AND ON (s.field_id = sf.id)
field_variable = '".$field_variable."' WHERE
{$this->handler_id} = '$item_id' AND
field_variable = '".$field_variable."'
ORDER BY id"; ORDER BY id";
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result)) { if (Database::num_rows($result)) {
@ -386,14 +503,17 @@ class ExtraFieldValue extends Model
* @return mixed Give the ID if found, or false on failure or not found * @return mixed Give the ID if found, or false on failure or not found
* @assert (-1,-1) === false * @assert (-1,-1) === false
*/ */
public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) { public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false)
{
$field_value = Database::escape_string($field_value); $field_value = Database::escape_string($field_value);
$field_variable = Database::escape_string($field_variable); $field_variable = Database::escape_string($field_variable);
$sql = "SELECT {$this->handler_id} FROM {$this->table} s $sql = "SELECT {$this->handler_id} FROM {$this->table} s
INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id) INNER JOIN {$this->table_handler_field} sf
WHERE field_value = '$field_value' AND ON (s.field_id = sf.id)
field_variable = '".$field_variable."' WHERE
field_value = '$field_value' AND
field_variable = '".$field_variable."'
"; ";
$result = Database::query($sql); $result = Database::query($sql);
@ -413,7 +533,10 @@ class ExtraFieldValue extends Model
*/ */
public function get_values_by_field_id($field_id) public function get_values_by_field_id($field_id)
{ {
$sql = "SELECT s.*, field_type FROM {$this->table} s INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id) $field_id = intval($field_id);
$sql = "SELECT s.*, field_type FROM {$this->table} s
INNER JOIN {$this->table_handler_field} sf
ON (s.field_id = sf.id)
WHERE field_id = '".$field_id."' ORDER BY id"; WHERE field_id = '".$field_id."' ORDER BY id";
$result = Database::query($sql); $result = Database::query($sql);
if (Database::num_rows($result)) { if (Database::num_rows($result)) {
@ -422,13 +545,64 @@ class ExtraFieldValue extends Model
return false; return false;
} }
/**
* @param int $itemId
* @param int $fieldId
* @return array
*/
public function getAllValuesByItemAndField($itemId, $fieldId)
{
$fieldId = intval($fieldId);
$itemId = intval($itemId);
$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
{$this->handler_id} = '$itemId'
ORDER BY field_value";
$result = Database::query($sql);
if (Database::num_rows($result)) {
return Database::store_result($result, 'ASSOC');
}
return false;
}
/**
* @param int $itemId
* @param int $fieldId
* @param string $fieldValue
* @return array|bool
*/
public function getAllValuesByItemAndFieldAndValue($itemId, $fieldId, $fieldValue)
{
$fieldId = intval($fieldId);
$itemId = intval($itemId);
$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
{$this->handler_id} = '$itemId' AND
field_value = $fieldValue
ORDER BY field_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 * Deletes all the values related to a specific field ID
* @param int Field ID * @param int Field ID
* @return void * @return void
* @assert ('a') == null * @assert ('a') == null
*/ */
public function delete_all_values_by_field_id($field_id) { public function delete_all_values_by_field_id($field_id)
{
$field_id = intval($field_id); $field_id = intval($field_id);
$sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; $sql = "DELETE FROM {$this->table} WHERE field_id = $field_id";
Database::query($sql); Database::query($sql);
@ -449,6 +623,25 @@ class ExtraFieldValue extends Model
Database::query($sql); Database::query($sql);
} }
/**
* @param int $itemId
* @param int $fieldId
* @param int $fieldValue
*/
public function deleteValuesByHandlerAndFieldAndValue($itemId, $fieldId, $fieldValue)
{
$itemId = intval($itemId);
$fieldId = intval($fieldId);
$fieldValue = Database::escape_string($fieldValue);
$sql = "DELETE FROM {$this->table}
WHERE
{$this->handler_id} = '$itemId' AND
field_id = '".$fieldId."' AND
field_value = '$fieldValue'";
Database::query($sql);
}
/** /**
* Not yet implemented - Compares the field values of two items * Not yet implemented - Compares the field values of two items
* @param int Item 1 * @param int Item 1

@ -732,6 +732,12 @@ switch ($action) {
$_SESSION['oLP']->delete_lp_image(); $_SESSION['oLP']->delete_lp_image();
} }
$extraFieldValue = new ExtraFieldValue('lp');
$params = array(
'lp_id' => $_SESSION['oLP']->id
);
$extraFieldValue->save_field_values($_REQUEST);
if ($_FILES['lp_preview_image']['size'] > 0) if ($_FILES['lp_preview_image']['size'] > 0)
$_SESSION['oLP']->upload_image($_FILES['lp_preview_image']); $_SESSION['oLP']->upload_image($_FILES['lp_preview_image']);

@ -49,11 +49,6 @@ function activate_end_date() {
</script>'; </script>';
Display::display_header(get_lang('CourseSettings'), 'Path');
echo $_SESSION['oLP']->build_action_menu();
$gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null; $gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']) : null;
$defaults=array(); $defaults=array();
@ -177,6 +172,18 @@ if (api_is_platform_admin()) {
$form->addElement('checkbox', 'subscribe_users', null, get_lang('SubscribeUsersToLP')); $form->addElement('checkbox', 'subscribe_users', null, get_lang('SubscribeUsersToLP'));
$extraField = new ExtraField('lp');
$extra = $extraField->addElements($form, $_SESSION['oLP']->get_id());
$htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/jquery.fcbkcomplete.js" type="text/javascript" language="javascript"></script>';
$htmlHeadXtra[] = '<link href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/tag/style.css" rel="stylesheet" type="text/css" />';
$htmlHeadXtra[] ='<script>
$(function() {
'.$extra['jquery_ready_content'].'
});
</script>';
//Submit button //Submit button
$form->addElement('style_submit_button', 'Submit', get_lang('SaveLPSettings'),'class="save"'); $form->addElement('style_submit_button', 'Submit', get_lang('SaveLPSettings'),'class="save"');
@ -190,6 +197,11 @@ $defaults['max_attempts'] = $_SESSION['oLP']->get_max_attempts();
$defaults['subscribe_users'] = $_SESSION['oLP']->get_subscribe_users(); $defaults['subscribe_users'] = $_SESSION['oLP']->get_subscribe_users();
$form->setDefaults($defaults); $form->setDefaults($defaults);
Display::display_header(get_lang('CourseSettings'), 'Path');
echo $_SESSION['oLP']->build_action_menu();
echo '<div class="row">'; echo '<div class="row">';
if ($_SESSION['oLP']->get_hide_toc_frame() == 1) { if ($_SESSION['oLP']->get_hide_toc_frame() == 1) {

Loading…
Cancel
Save