parent
705e1f5113
commit
b4bd2505c9
@ -0,0 +1,167 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
/** |
||||||
|
* @package chamilo.admin |
||||||
|
*/ |
||||||
|
|
||||||
|
// Language files that need to be included. |
||||||
|
$language_file = array('admin'); |
||||||
|
|
||||||
|
$cidReset = true; |
||||||
|
require_once '../inc/global.inc.php'; |
||||||
|
|
||||||
|
$this_section = SECTION_PLATFORM_ADMIN; |
||||||
|
|
||||||
|
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; |
||||||
|
|
||||||
|
api_protect_admin_script(); |
||||||
|
|
||||||
|
//Add the JS needed to use the jqgrid |
||||||
|
$htmlHeadXtra[] = api_get_jqgrid_js(); |
||||||
|
|
||||||
|
// setting breadcrumbs |
||||||
|
$interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin')); |
||||||
|
|
||||||
|
$tool_name = null; |
||||||
|
|
||||||
|
$action = isset($_GET['action']) ? $_GET['action'] : null; |
||||||
|
$field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null; |
||||||
|
|
||||||
|
if (empty($field_id)) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
if (!in_array($type, ExtraField::getValidExtraFieldTypes())) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
|
||||||
|
$extra_field = new ExtraField($type); |
||||||
|
$extra_field_info = $extra_field->get($field_id); |
||||||
|
|
||||||
|
$check = Security::check_token('request'); |
||||||
|
$token = Security::get_token(); |
||||||
|
|
||||||
|
if ($action == 'add') { |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type,'name' => $extra_field->pageName); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type.'&action=edit&id='.$extra_field_info['id'],'name' => $extra_field_info['field_display_text']); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extra_field->type.'&field_id='.$extra_field_info['id'], 'name' => get_lang('EditExtraFieldOptions')); |
||||||
|
$interbreadcrumb[]=array('url' => '#','name' => get_lang('Add')); |
||||||
|
} elseif ($action == 'edit') { |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type,'name' => $extra_field->pageName); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type.'&action=edit&id='.$extra_field_info['id'],'name' => $extra_field_info['field_display_text']); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extra_field->type.'&field_id='.$extra_field_info['id'], 'name' => get_lang('EditExtraFieldOptions')); |
||||||
|
|
||||||
|
$interbreadcrumb[]=array('url' => '#','name' => get_lang('Edit')); |
||||||
|
} else { |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type,'name' => $extra_field->pageName); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type.'&action=edit&id='.$extra_field_info['id'],'name' => $extra_field_info['field_display_text']); |
||||||
|
$interbreadcrumb[]=array('url' => '#','name' => get_lang('EditExtraFieldOptions')); |
||||||
|
} |
||||||
|
|
||||||
|
//jqgrid will use this URL to do the selects |
||||||
|
$params = 'field_id='.$field_id.'&type='.$extra_field->type; |
||||||
|
$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_extra_field_options&'.$params; |
||||||
|
|
||||||
|
//The order is important you need to check the the $column variable in the model.ajax.php file |
||||||
|
$columns = array(get_lang('Name'), get_lang('Value'), get_lang('Order'), get_lang('Actions')); |
||||||
|
|
||||||
|
//Column config |
||||||
|
$column_model = array( |
||||||
|
array('name'=>'option_display_text', 'index'=>'option_display_text', 'width'=>'180', 'align'=>'left'), |
||||||
|
array('name'=>'option_value', 'index'=>'option_value', 'width'=>'', 'align'=>'left','sortable'=>'false'), |
||||||
|
array('name'=>'option_order', 'index'=>'option_order', 'width'=>'', 'align'=>'left','sortable'=>'false'), |
||||||
|
array('name'=>'actions', 'index'=>'actions', 'width'=>'100', 'align'=>'left','formatter'=>'action_formatter','sortable'=>'false') |
||||||
|
); |
||||||
|
//Autowidth |
||||||
|
$extra_params['autowidth'] = 'true'; |
||||||
|
//height auto |
||||||
|
$extra_params['height'] = 'auto'; |
||||||
|
|
||||||
|
//With this function we can add actions to the jgrid (edit, delete, etc) |
||||||
|
$action_links = 'function action_formatter(cellvalue, options, rowObject) { |
||||||
|
return \'<a href="?action=edit&'.$params.'&id=\'+options.rowId+\'">'.Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'. |
||||||
|
' <a onclick="javascript:if(!confirm('."\'".addslashes(get_lang("ConfirmYourChoice"))."\'".')) return false;" href="?sec_token='.$token.'&action=delete&'.$params.'&id=\'+options.rowId+\'">'.Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>'. |
||||||
|
'\'; |
||||||
|
}'; |
||||||
|
$htmlHeadXtra[]=' |
||||||
|
<script> |
||||||
|
$(function() { |
||||||
|
// grid definition see the $obj->display() function |
||||||
|
'.Display::grid_js('extra_field_options', $url, $columns, $column_model, $extra_params, array(), $action_links, true).' |
||||||
|
|
||||||
|
}); |
||||||
|
</script>'; |
||||||
|
|
||||||
|
// The header. |
||||||
|
Display::display_header($tool_name); |
||||||
|
|
||||||
|
echo Display::page_header($extra_field_info['field_display_text']); |
||||||
|
|
||||||
|
$obj = new ExtraFieldOption($extra_field->type); |
||||||
|
$obj->field_id = $field_id; |
||||||
|
|
||||||
|
// Action handling: Add |
||||||
|
switch ($action) { |
||||||
|
case 'add': |
||||||
|
if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
$url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&'.$params; |
||||||
|
$form = $obj->return_form($url, 'add'); |
||||||
|
|
||||||
|
// The validation or display |
||||||
|
if ($form->validate()) { |
||||||
|
if ($check) { |
||||||
|
$values = $form->exportValues(); |
||||||
|
$res = $obj->save_one_item($values); |
||||||
|
if ($res) { |
||||||
|
Display::display_confirmation_message(get_lang('ItemAdded')); |
||||||
|
} |
||||||
|
} |
||||||
|
$obj->display(); |
||||||
|
} else { |
||||||
|
/*echo '<div class="actions">'; |
||||||
|
echo '<a href="'.api_get_self().'">'.Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM).'</a>'; |
||||||
|
echo '</div>'; */ |
||||||
|
$form->addElement('hidden', 'sec_token'); |
||||||
|
$form->setConstants(array('sec_token' => $token)); |
||||||
|
$form->display(); |
||||||
|
} |
||||||
|
break; |
||||||
|
case 'edit': |
||||||
|
// Action handling: Editing |
||||||
|
$url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']).'&'.$params; |
||||||
|
$form = $obj->return_form($url, 'edit'); |
||||||
|
|
||||||
|
// The validation or display |
||||||
|
if ($form->validate()) { |
||||||
|
if ($check) { |
||||||
|
$values = $form->exportValues(); |
||||||
|
$res = $obj->update($values); |
||||||
|
Display::display_confirmation_message(sprintf(get_lang('ItemUpdated'), $values['name']), false); |
||||||
|
} |
||||||
|
$obj->display(); |
||||||
|
} else { |
||||||
|
/*echo '<div class="actions">'; |
||||||
|
echo '<a href="'.api_get_self().'">'.Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM).'</a>'; |
||||||
|
echo '</div>';*/ |
||||||
|
$form->addElement('hidden', 'sec_token'); |
||||||
|
$form->setConstants(array('sec_token' => $token)); |
||||||
|
$form->display(); |
||||||
|
} |
||||||
|
break; |
||||||
|
case 'delete': |
||||||
|
// Action handling: delete |
||||||
|
if ($check) { |
||||||
|
$res = $obj->delete($_GET['id']); |
||||||
|
if ($res) { |
||||||
|
Display::display_confirmation_message(get_lang('ItemDeleted')); |
||||||
|
} |
||||||
|
} |
||||||
|
$obj->display(); |
||||||
|
break; |
||||||
|
default: |
||||||
|
$obj->display(); |
||||||
|
break; |
||||||
|
} |
||||||
|
Display :: display_footer(); |
||||||
@ -0,0 +1,212 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
/** |
||||||
|
* @package chamilo.admin |
||||||
|
*/ |
||||||
|
|
||||||
|
// Language files that need to be included. |
||||||
|
$language_file = array('admin'); |
||||||
|
|
||||||
|
$cidReset = true; |
||||||
|
require_once '../inc/global.inc.php'; |
||||||
|
|
||||||
|
$this_section = SECTION_PLATFORM_ADMIN; |
||||||
|
|
||||||
|
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; |
||||||
|
|
||||||
|
api_protect_admin_script(); |
||||||
|
|
||||||
|
// setting breadcrumbs |
||||||
|
$interbreadcrumb[] = array('url' => 'index.php','name' => get_lang('PlatformAdmin')); |
||||||
|
|
||||||
|
$tool_name = null; |
||||||
|
|
||||||
|
$action = isset($_GET['action']) ? $_GET['action'] : null; |
||||||
|
$field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null; |
||||||
|
|
||||||
|
if (empty($field_id)) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
if (!in_array($type, ExtraField::getValidExtraFieldTypes())) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
|
||||||
|
$extraField = new ExtraField($type); |
||||||
|
$extraFieldInfo = $extraField->get($field_id); |
||||||
|
|
||||||
|
$check = Security::check_token('request'); |
||||||
|
$token = Security::get_token(); |
||||||
|
|
||||||
|
if ($action == 'add') { |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions')); |
||||||
|
$interbreadcrumb[]=array('url' => '#','name' => get_lang('Add')); |
||||||
|
} elseif ($action == 'edit') { |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$extraField->type.'&field_id='.$extraFieldInfo['id'], 'name' => get_lang('EditExtraFieldOptions')); |
||||||
|
|
||||||
|
$interbreadcrumb[]=array('url' => '#','name' => get_lang('Edit')); |
||||||
|
} else { |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type,'name' => $extraField->pageName); |
||||||
|
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extraField->type.'&action=edit&id='.$extraFieldInfo['id'],'name' => $extraFieldInfo['field_display_text']); |
||||||
|
$interbreadcrumb[]=array('url' => '#','name' => get_lang('EditExtraFieldOptions')); |
||||||
|
} |
||||||
|
|
||||||
|
$roleId = isset($_REQUEST['roleId']) ? $_REQUEST['roleId'] : null; |
||||||
|
|
||||||
|
//jqgrid will use this URL to do the selects |
||||||
|
$params = 'field_id='.$field_id.'&type='.$extraField->type.'&roleId='.$roleId; |
||||||
|
$paramsNoRole = 'field_id='.$field_id.'&type='.$extraField->type; |
||||||
|
|
||||||
|
//The order is important you need to check the the $column variable in the model.ajax.php file |
||||||
|
$columns = array(get_lang('Name'), get_lang('Value'), get_lang('Order'), get_lang('Actions')); |
||||||
|
|
||||||
|
$htmlHeadXtra[]='<script> |
||||||
|
|
||||||
|
function setHidden(obj) { |
||||||
|
var name = $(obj).attr("name"); |
||||||
|
var hiddenName = "hidden_" + name; |
||||||
|
if ($("#" + hiddenName).attr("value") == 1) { |
||||||
|
$("#" + hiddenName).attr("value", 0); |
||||||
|
} else { |
||||||
|
$("#" + hiddenName).attr("value", 1); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function changeStatus(obj) { |
||||||
|
var roleId = $(obj).find(":selected").val(); |
||||||
|
if (roleId != 0) { |
||||||
|
window.location.replace("'.api_get_self().'?'.$paramsNoRole.'&roleId="+roleId); |
||||||
|
} |
||||||
|
} |
||||||
|
$().ready( function() { |
||||||
|
$(".select_all").on("click", function() { |
||||||
|
$("#workflow :checkbox").prop("checked", 1); |
||||||
|
$("#workflow :hidden").prop("value", 1); |
||||||
|
return false; |
||||||
|
}); |
||||||
|
$(".unselect_all").on("click", function() { |
||||||
|
$("#workflow :checkbox").prop("checked", 0); |
||||||
|
$("#workflow :hidden").prop("value", 0); |
||||||
|
return false; |
||||||
|
}); |
||||||
|
}); |
||||||
|
</script>'; |
||||||
|
|
||||||
|
// The header. |
||||||
|
Display::display_header($tool_name); |
||||||
|
|
||||||
|
echo Display::page_header($extraFieldInfo['field_display_text']); |
||||||
|
|
||||||
|
$obj = new ExtraFieldOption($type); |
||||||
|
$columns = array('option_display_text', 'option_value', 'option_order'); |
||||||
|
$result = Database::select('*', $obj->table, array('where' => array("field_id = ? " => $field_id), 'order'=>"option_order ASC")); |
||||||
|
|
||||||
|
$table = new HTML_Table(array('class' => 'data_table')); |
||||||
|
$column = 0; |
||||||
|
$row = 0; |
||||||
|
$table->setHeaderContents($row, $column, get_lang('CurrentStatus')); |
||||||
|
$column++; |
||||||
|
foreach ($result as $item) { |
||||||
|
$table->setHeaderContents($row, $column, $item['option_display_text']); |
||||||
|
$column++; |
||||||
|
} |
||||||
|
$row++; |
||||||
|
|
||||||
|
$form = new FormValidator('workflow', 'post', api_get_self().'?'.$params); |
||||||
|
$options = api_get_user_roles(); |
||||||
|
$options[0] = get_lang('SelectAnOption'); |
||||||
|
ksort($options); |
||||||
|
$form->addElement('select', 'status', get_lang('SelectRole'), $options, array('onclick' => 'changeStatus(this)')); |
||||||
|
|
||||||
|
$checks = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')->findBy(array('fieldId' => $field_id, 'roleId' => $roleId)); |
||||||
|
$includedFields = array(); |
||||||
|
if (!empty($checks)) { |
||||||
|
foreach ($checks as $availableField) { |
||||||
|
$includedFields[$availableField->getFieldOptionId()][] = $availableField->getRelatedFieldOptionId(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($result as $item) { |
||||||
|
$column = 0; |
||||||
|
$table->setCellContents($row, $column, $item['option_display_text']); |
||||||
|
$column++; |
||||||
|
$value = null; |
||||||
|
|
||||||
|
foreach ($result as $itemCol) { |
||||||
|
$id = 'extra_field_status_'.$item['id'].'_'.$itemCol['id']; |
||||||
|
$idForm = 'extra_field_status['.$item['id'].']['.$itemCol['id'].']'; |
||||||
|
$attributes = array('onclick' => 'setHidden(this)'); |
||||||
|
$value = 0; |
||||||
|
|
||||||
|
if (isset($includedFields[$itemCol['id']]) && in_array($item['id'], $includedFields[$itemCol['id']])) { |
||||||
|
$value = 1; |
||||||
|
$attributes['checked'] = 'checked'; |
||||||
|
} |
||||||
|
|
||||||
|
$element = Display::input('checkbox', $id, null, $attributes); |
||||||
|
$table->setCellContents($row, $column, $element); |
||||||
|
$form->addElement('hidden', 'hidden_'.$idForm, $value, array('id' => 'hidden_'.$id)); |
||||||
|
$column++; |
||||||
|
} |
||||||
|
$row++; |
||||||
|
} |
||||||
|
|
||||||
|
if (!empty($roleId)) { |
||||||
|
$form->addElement('html', $table->toHtml()); |
||||||
|
$group = array(); |
||||||
|
$group[]= $form->createElement('button', 'submit', get_lang('Save')); |
||||||
|
$group[]= $form->createElement('button', 'select_all', get_lang('SelectAll'), array('class' => 'btn select_all')); |
||||||
|
$group[]= $form->createElement('button', 'unselect_all', get_lang('UnSelectAll'), array('class' => 'btn unselect_all')); |
||||||
|
$form->addGroup($group, '', null, ' '); |
||||||
|
|
||||||
|
$form->setDefaults(array('status' => $roleId)); |
||||||
|
} else { |
||||||
|
$form->addElement('button', 'submit', get_lang('Edit')); |
||||||
|
} |
||||||
|
|
||||||
|
$form->display(); |
||||||
|
|
||||||
|
if ($form->validate()) { |
||||||
|
$values = $form->getSubmitValues(); |
||||||
|
$result = $values['hidden_extra_field_status']; |
||||||
|
if (!empty($result)) { |
||||||
|
foreach ($result as $id => $items) { |
||||||
|
foreach ($items as $subItemId => $value) { |
||||||
|
$extraFieldOptionRelFieldOption = $app['orm.em']->getRepository('ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption')->findOneBy( |
||||||
|
array( |
||||||
|
'fieldId' => $field_id, |
||||||
|
'fieldOptionId' => $subItemId, |
||||||
|
'roleId' => $roleId, |
||||||
|
'relatedFieldOptionId' => $id |
||||||
|
) |
||||||
|
); |
||||||
|
|
||||||
|
if ($value == 1) { |
||||||
|
if (empty($extraFieldOptionRelFieldOption)) { |
||||||
|
$extraFieldOptionRelFieldOption = new \ChamiloLMS\Entity\ExtraFieldOptionRelFieldOption(); |
||||||
|
$extraFieldOptionRelFieldOption->setFieldId($field_id); |
||||||
|
$extraFieldOptionRelFieldOption->setFieldOptionId($subItemId); |
||||||
|
$extraFieldOptionRelFieldOption->setRelatedFieldOptionId($id); |
||||||
|
$extraFieldOptionRelFieldOption->setRoleId($roleId); |
||||||
|
$app['orm.ems']['db_write']->persist($extraFieldOptionRelFieldOption); |
||||||
|
} |
||||||
|
} else { |
||||||
|
|
||||||
|
if ($extraFieldOptionRelFieldOption) { |
||||||
|
$app['orm.ems']['db_write']->remove($extraFieldOptionRelFieldOption); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
$app['orm.ems']['db_write']->flush(); |
||||||
|
header('Location:'.api_get_self().'?'.$params); |
||||||
|
exit; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Display :: display_footer(); |
||||||
@ -0,0 +1,216 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
/** |
||||||
|
* @package chamilo.admin |
||||||
|
*/ |
||||||
|
|
||||||
|
// Language files that need to be included. |
||||||
|
$language_file = array('admin'); |
||||||
|
|
||||||
|
$cidReset = true; |
||||||
|
|
||||||
|
$cidReset = true; |
||||||
|
require_once '../inc/global.inc.php'; |
||||||
|
|
||||||
|
$extraFieldType = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; |
||||||
|
|
||||||
|
$this_section = SECTION_PLATFORM_ADMIN; |
||||||
|
|
||||||
|
api_protect_admin_script(); |
||||||
|
|
||||||
|
//Add the JS needed to use the jqgrid |
||||||
|
$htmlHeadXtra[] = api_get_jqgrid_js(); |
||||||
|
|
||||||
|
// setting breadcrumbs |
||||||
|
$interbreadcrumb[]=array('url' => 'index.php','name' => get_lang('PlatformAdmin')); |
||||||
|
|
||||||
|
$tool_name = null; |
||||||
|
|
||||||
|
$action = isset($_GET['action']) ? $_GET['action'] : null; |
||||||
|
if (!in_array($extraFieldType, ExtraField::getValidExtraFieldTypes())) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
|
||||||
|
$check = Security::check_token('request'); |
||||||
|
$token = Security::get_token(); |
||||||
|
|
||||||
|
$obj = new ExtraField($extraFieldType); |
||||||
|
|
||||||
|
$obj->setupBreadcrumb($interbreadcrumb, $action); |
||||||
|
|
||||||
|
//jqgrid will use this URL to do the selects |
||||||
|
$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_extra_fields&type='.$extraFieldType; |
||||||
|
|
||||||
|
//The order is important you need to check the the $column variable in the model.ajax.php file |
||||||
|
$columns = $obj->getJqgridColumnNames(); |
||||||
|
|
||||||
|
//Column config |
||||||
|
$column_model = $obj->getJqgridColumnModel(); |
||||||
|
|
||||||
|
//Autowidth |
||||||
|
$extra_params['autowidth'] = 'true'; |
||||||
|
//height auto |
||||||
|
$extra_params['height'] = 'auto'; |
||||||
|
$extra_params['sortname'] = 'field_order'; |
||||||
|
|
||||||
|
$action_links = $obj->getJqgridActionLinks($token); |
||||||
|
|
||||||
|
$htmlHeadXtra[]='<script> |
||||||
|
$(function() { |
||||||
|
// grid definition see the $obj->display() function |
||||||
|
'.Display::grid_js($obj->type.'_fields', $url, $columns, $column_model, $extra_params, array(), $action_links, true).' |
||||||
|
|
||||||
|
$("#field_type").on("change", function() { |
||||||
|
id = $(this).val(); |
||||||
|
switch(id) { |
||||||
|
case "1": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_text.png')).'"); |
||||||
|
break; |
||||||
|
case "2": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_text_area.png')).'"); |
||||||
|
break; |
||||||
|
case "3": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('add_user_field_howto.png')).'"); |
||||||
|
break; |
||||||
|
case "4": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_drop_down.png')).'"); |
||||||
|
break; |
||||||
|
case "5": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_multidropdown.png')).'"); |
||||||
|
break; |
||||||
|
case "6": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_data.png')).'"); |
||||||
|
break; |
||||||
|
case "7": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_date_time.png')).'"); |
||||||
|
break; |
||||||
|
case "8": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_doubleselect.png')).'"); |
||||||
|
break; |
||||||
|
case "9": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_divider.png')).'"); |
||||||
|
break; |
||||||
|
case "10": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_user_tag.png')).'"); |
||||||
|
break; |
||||||
|
case "11": |
||||||
|
$("#example").html("'.addslashes(Display::return_icon('userfield_data.png')).'"); |
||||||
|
break; |
||||||
|
} |
||||||
|
}); |
||||||
|
}); |
||||||
|
</script>'; |
||||||
|
|
||||||
|
// The header. |
||||||
|
Display::display_header($tool_name); |
||||||
|
|
||||||
|
// Action handling: Add |
||||||
|
|
||||||
|
switch ($action) { |
||||||
|
case 'add': |
||||||
|
if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) { |
||||||
|
api_not_allowed(); |
||||||
|
} |
||||||
|
$url = api_get_self().'?type='.$obj->type.'&action='.Security::remove_XSS($_GET['action']); |
||||||
|
$form = $obj->return_form($url, 'add'); |
||||||
|
|
||||||
|
// The validation or display |
||||||
|
if ($form->validate()) { |
||||||
|
//if ($check) { |
||||||
|
$values = $form->exportValues(); |
||||||
|
$res = $obj->save($values); |
||||||
|
if ($res) { |
||||||
|
Display::display_confirmation_message(get_lang('ItemAdded')); |
||||||
|
} |
||||||
|
//} |
||||||
|
$obj->display(); |
||||||
|
} else { |
||||||
|
echo '<div class="actions">'; |
||||||
|
echo '<a href="'.api_get_self().'?type='.$obj->type.'">'.Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM).'</a>'; |
||||||
|
echo '</div>'; |
||||||
|
$form->addElement('hidden', 'sec_token'); |
||||||
|
$form->setConstants(array('sec_token' => $token)); |
||||||
|
$form->display(); |
||||||
|
} |
||||||
|
break; |
||||||
|
case 'edit': |
||||||
|
// Action handling: Editing |
||||||
|
$url = api_get_self().'?type='.$obj->type.'&action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']); |
||||||
|
$form = $obj->return_form($url, 'edit'); |
||||||
|
|
||||||
|
// The validation or display |
||||||
|
if ($form->validate()) { |
||||||
|
//if ($check) { |
||||||
|
$values = $form->exportValues(); |
||||||
|
$res = $obj->update($values); |
||||||
|
Display::display_confirmation_message(sprintf(get_lang('ItemUpdated'), $values['field_variable']), false); |
||||||
|
//} |
||||||
|
$obj->display(); |
||||||
|
} else { |
||||||
|
echo '<div class="actions">'; |
||||||
|
echo '<a href="'.api_get_self().'?type='.$obj->type.'">'.Display::return_icon('back.png',get_lang('Back'),'',ICON_SIZE_MEDIUM).'</a>'; |
||||||
|
echo '</div>'; |
||||||
|
$form->addElement('hidden', 'sec_token'); |
||||||
|
$form->setConstants(array('sec_token' => $token)); |
||||||
|
$form->display(); |
||||||
|
} |
||||||
|
break; |
||||||
|
case 'delete': |
||||||
|
// Action handling: delete |
||||||
|
//if ($check) { |
||||||
|
$res = $obj->delete($_GET['id']); |
||||||
|
if ($res) { |
||||||
|
Display::display_confirmation_message(get_lang('ItemDeleted')); |
||||||
|
} |
||||||
|
//} |
||||||
|
$obj->display(); |
||||||
|
break; |
||||||
|
default: |
||||||
|
$obj->display(); |
||||||
|
break; |
||||||
|
} |
||||||
|
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); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
*/ |
||||||
@ -0,0 +1,30 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
$language_file = array('admin', 'registration', 'userInfo'); |
||||||
|
require_once '../global.inc.php'; |
||||||
|
$action = $_GET['a']; |
||||||
|
|
||||||
|
switch ($action) { |
||||||
|
case 'get_second_select_options': |
||||||
|
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : null; |
||||||
|
$field_id = isset($_REQUEST['field_id']) ? $_REQUEST['field_id'] : null; |
||||||
|
$option_value_id = isset($_REQUEST['option_value_id']) ? $_REQUEST['option_value_id'] : null; |
||||||
|
|
||||||
|
if (!empty($type) && !empty($field_id) && !empty($option_value_id)) { |
||||||
|
$field_options = new ExtraFieldOption($type); |
||||||
|
echo $field_options->get_second_select_field_options_by_field($field_id, $option_value_id, true); |
||||||
|
} |
||||||
|
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: |
||||||
|
exit; |
||||||
|
break; |
||||||
|
} |
||||||
|
exit; |
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,659 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
/** |
||||||
|
* Declaration of the ExtraFieldOption class |
||||||
|
*/ |
||||||
|
/** |
||||||
|
* Handles the extra fields for various objects (users, sessions, courses) |
||||||
|
*/ |
||||||
|
class ExtraFieldOption extends Model |
||||||
|
{ |
||||||
|
public $columns = array( |
||||||
|
'id', 'field_id', 'option_value', 'option_display_text', 'option_order', 'priority', 'priority_message', 'tms' |
||||||
|
); |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets the table for the type of object for which we are using an extra field |
||||||
|
* @param string Type of object (course, user or session) |
||||||
|
*/ |
||||||
|
public function __construct($type) |
||||||
|
{ |
||||||
|
$this->type = $type; |
||||||
|
switch ($this->type) { |
||||||
|
case 'course': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_OPTIONS); |
||||||
|
break; |
||||||
|
case 'user': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); |
||||||
|
break; |
||||||
|
case 'session': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS); |
||||||
|
break; |
||||||
|
case 'question': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS); |
||||||
|
break; |
||||||
|
case 'lp': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_LP_FIELD_OPTIONS); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets the number of options already available in the table for this item type |
||||||
|
* @return int Number of options available |
||||||
|
* @assert () >= 0 |
||||||
|
*/ |
||||||
|
public function get_count() |
||||||
|
{ |
||||||
|
$row = Database::select('count(*) as count', $this->table, array(), 'first'); |
||||||
|
|
||||||
|
return $row['count']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets the number of options available for this field |
||||||
|
* @param int Field ID |
||||||
|
* @return int Number of options |
||||||
|
* @assert ('') === false |
||||||
|
* @assert (-1) == 0 |
||||||
|
* @assert (0) == 0 |
||||||
|
*/ |
||||||
|
public function get_count_by_field_id($field_id) |
||||||
|
{ |
||||||
|
if (empty($field_id)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
$row = Database::select( |
||||||
|
'count(*) as count', |
||||||
|
$this->table, |
||||||
|
array('where' => array('field_id = ?' => $field_id)), |
||||||
|
'first' |
||||||
|
); |
||||||
|
|
||||||
|
return $row['count']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a list of options for a specific field, separated by ";" |
||||||
|
* @param int Field ID |
||||||
|
* @param bool Indicates whether we want the results to be given with their id |
||||||
|
* @param string Order by clause (without the "order by") to be added to the SQL query |
||||||
|
* @return string List of options separated by ; |
||||||
|
* @assert (-1, false, 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); |
||||||
|
$new_options = array(); |
||||||
|
if (!empty($options)) { |
||||||
|
foreach ($options as $option) { |
||||||
|
$new_options[] = $option['option_value'].':'.$option['option_display_text']; |
||||||
|
} |
||||||
|
$string = implode(';', $new_options); |
||||||
|
|
||||||
|
return $string; |
||||||
|
} |
||||||
|
|
||||||
|
return ''; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Delete all the options of a specific field |
||||||
|
* @param int Field ID |
||||||
|
* @result void |
||||||
|
* @assert (-1) === false |
||||||
|
*/ |
||||||
|
public function delete_all_options_by_field_id($field_id) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; |
||||||
|
$r = Database::query($sql); |
||||||
|
|
||||||
|
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 |
||||||
|
* @param array Parameters to be considered for the insertion |
||||||
|
* @param bool Whether to show the query (sent to the parent save() method) |
||||||
|
* @return bool True on success, false on error |
||||||
|
* @assert (array('field_id'=>0), false) === false |
||||||
|
* @assert (array('field_id'=>1), false) === true |
||||||
|
*/ |
||||||
|
public function save($params, $show_query = false) |
||||||
|
{ |
||||||
|
$field_id = intval($params['field_id']); |
||||||
|
|
||||||
|
if (empty($field_id)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
$time = api_get_utc_datetime(); |
||||||
|
if (!empty($params['field_options']) && |
||||||
|
in_array( |
||||||
|
$params['field_type'], |
||||||
|
array( |
||||||
|
ExtraField::FIELD_TYPE_RADIO, |
||||||
|
ExtraField::FIELD_TYPE_SELECT, |
||||||
|
ExtraField::FIELD_TYPE_SELECT_MULTIPLE, |
||||||
|
ExtraField::FIELD_TYPE_DOUBLE_SELECT |
||||||
|
) |
||||||
|
) |
||||||
|
) { |
||||||
|
if ($params['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { |
||||||
|
//$params['field_options'] = France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; |
||||||
|
$options_parsed = ExtraField::extra_field_double_select_convert_string_to_array( |
||||||
|
$params['field_options'] |
||||||
|
); |
||||||
|
|
||||||
|
if (!empty($options_parsed)) { |
||||||
|
foreach ($options_parsed as $key => $option) { |
||||||
|
$sub_options = $option['options']; |
||||||
|
|
||||||
|
$new_params = array( |
||||||
|
'field_id' => $field_id, |
||||||
|
'option_value' => 0, |
||||||
|
'option_display_text' => $option['label'], |
||||||
|
'option_order' => 0, |
||||||
|
'tms' => $time, |
||||||
|
); |
||||||
|
|
||||||
|
// Looking if option already exists: |
||||||
|
$option_info = self::get_field_option_by_field_id_and_option_display_text( |
||||||
|
$field_id, |
||||||
|
$option['label'] |
||||||
|
); |
||||||
|
|
||||||
|
if (empty($option_info)) { |
||||||
|
$sub_id = parent::save($new_params, $show_query); |
||||||
|
} else { |
||||||
|
$sub_id = $option_info['id']; |
||||||
|
$new_params['id'] = $sub_id; |
||||||
|
parent::update($new_params, $show_query); |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($sub_options as $sub_option) { |
||||||
|
if (!empty($sub_option)) { |
||||||
|
$new_params = array( |
||||||
|
'field_id' => $field_id, |
||||||
|
'option_value' => $sub_id, |
||||||
|
'option_display_text' => $sub_option, |
||||||
|
'option_order' => 0, |
||||||
|
'tms' => $time, |
||||||
|
); |
||||||
|
$option_info = self::get_field_option_by_field_id_and_option_display_text_and_option_value( |
||||||
|
$field_id, |
||||||
|
$sub_option, |
||||||
|
$sub_id |
||||||
|
); |
||||||
|
if (empty($option_info)) { |
||||||
|
parent::save($new_params, $show_query); |
||||||
|
} else { |
||||||
|
$new_params['id'] = $option_info['id']; |
||||||
|
parent::update($new_params, $show_query); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
$list = array(); |
||||||
|
} else { |
||||||
|
$list = explode(';', $params['field_options']); |
||||||
|
} |
||||||
|
|
||||||
|
if (!empty($list)) { |
||||||
|
foreach ($list as $option) { |
||||||
|
$option_info = self::get_field_option_by_field_and_option($field_id, $option); |
||||||
|
// Use URLify only for new items |
||||||
|
$optionValue = URLify::filter($option); |
||||||
|
$option = trim($option); |
||||||
|
|
||||||
|
if ($option_info == false) { |
||||||
|
$order = self::get_max_order($field_id); |
||||||
|
|
||||||
|
$new_params = array( |
||||||
|
'field_id' => $field_id, |
||||||
|
'option_value' => trim($optionValue), |
||||||
|
'option_display_text' => trim($option), |
||||||
|
'option_order' => $order, |
||||||
|
'tms' => $time, |
||||||
|
); |
||||||
|
parent::save($new_params, $show_query); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Save one option item at a time |
||||||
|
* @param array Parameters specific to the option |
||||||
|
* @param bool Whether to show the query (sent to parent save() method) |
||||||
|
* @param bool Whether to insert even if the option already exists |
||||||
|
* @return bool True on success, false on failure |
||||||
|
* @assert (array('field_id'=>0),false) === false |
||||||
|
* @assert (array('field_id'=>0),false) === true |
||||||
|
*/ |
||||||
|
public function save_one_item($params, $show_query = false, $insert_repeated = true) |
||||||
|
{ |
||||||
|
$field_id = intval($params['field_id']); |
||||||
|
if (empty($field_id)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
if (isset($params['option_value'])) { |
||||||
|
$params['option_value'] = trim($params['option_value']); |
||||||
|
} |
||||||
|
|
||||||
|
if (isset($params['option_display_text'])) { |
||||||
|
$params['option_display_text'] = trim($params['option_display_text']); |
||||||
|
} |
||||||
|
|
||||||
|
$params['tms'] = api_get_utc_datetime(); |
||||||
|
if (empty($params['option_order'])) { |
||||||
|
$order = self::get_max_order($field_id); |
||||||
|
$params['option_order'] = $order; |
||||||
|
} |
||||||
|
if ($insert_repeated) { |
||||||
|
parent::save($params, $show_query); |
||||||
|
} else { |
||||||
|
$check = self::get_field_option_by_field_and_option($field_id, $params['option_value']); |
||||||
|
if ($check == false) { |
||||||
|
parent::save($params, $show_query); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the complete row of a specific option of a specific field |
||||||
|
* @param int Field ID |
||||||
|
* @param string Value of the option |
||||||
|
* @return mixed The row on success or false on failure |
||||||
|
* @assert (0,'') === false |
||||||
|
*/ |
||||||
|
public function get_field_option_by_field_and_option($field_id, $option_value) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$option_value = Database::escape_string($option_value); |
||||||
|
|
||||||
|
$sql = "SELECT * FROM {$this->table} |
||||||
|
WHERE field_id = $field_id AND option_value = '".$option_value."'"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result) > 0) { |
||||||
|
return Database::store_result($result, 'ASSOC'); |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the complete row of a specific option's display text of a specific field |
||||||
|
* @param int Field ID |
||||||
|
* @param string Display value of the option |
||||||
|
* @return mixed The row on success or false on failure |
||||||
|
* @assert (0, '') === false |
||||||
|
*/ |
||||||
|
public function get_field_option_by_field_id_and_option_display_text($field_id, $option_display_text) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$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."'"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result) > 0) { |
||||||
|
return Database::fetch_array($result, 'ASSOC'); |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the complete row of a specific option's display text of a specific field |
||||||
|
* @param int Field ID |
||||||
|
* @param string Display value of the option |
||||||
|
* @param string Value of the option |
||||||
|
* @return mixed The row on success or false on failure |
||||||
|
* @assert (0, '', '') === false |
||||||
|
*/ |
||||||
|
public function get_field_option_by_field_id_and_option_display_text_and_option_value( |
||||||
|
$field_id, |
||||||
|
$option_display_text, |
||||||
|
$option_value |
||||||
|
) { |
||||||
|
$field_id = intval($field_id); |
||||||
|
$option_display_text = Database::escape_string($option_display_text); |
||||||
|
$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'"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result) > 0) { |
||||||
|
return Database::fetch_array($result, 'ASSOC'); |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets an array of options for a specific field |
||||||
|
* @param int The field ID |
||||||
|
* @param bool Whether to add the row ID in the result |
||||||
|
* @param string Extra ordering query bit |
||||||
|
* @result mixed Row on success, false on failure |
||||||
|
* @assert (0, '') === false |
||||||
|
*/ |
||||||
|
public function get_field_options_by_field($field_id, $add_id_in_array = false, $ordered_by = null) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
|
||||||
|
$sql = "SELECT * FROM {$this->table} WHERE field_id = $field_id "; |
||||||
|
|
||||||
|
if (!empty($ordered_by)) { |
||||||
|
$sql .= " ORDER BY $ordered_by "; |
||||||
|
} |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result) > 0) { |
||||||
|
if ($add_id_in_array) { |
||||||
|
$options = array(); |
||||||
|
while ($row = Database::fetch_array($result, 'ASSOC')) { |
||||||
|
$options[$row['id']] = $row; |
||||||
|
} |
||||||
|
|
||||||
|
return $options; |
||||||
|
} else { |
||||||
|
return Database::store_result($result, 'ASSOC'); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get options for a specific field as array or in JSON format suited for the double-select format |
||||||
|
* @param int Field ID |
||||||
|
* @param int Option value ID |
||||||
|
* @param bool Return format (whether it should be formatted to JSON or not) |
||||||
|
* @return mixed Row/JSON on success |
||||||
|
*/ |
||||||
|
public function get_second_select_field_options_by_field($field_id, $option_value_id, $to_json = false) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$option_value_id = intval($option_value_id); |
||||||
|
$options = array(); |
||||||
|
$sql = "SELECT * FROM {$this->table} |
||||||
|
WHERE field_id = $field_id AND option_value = $option_value_id |
||||||
|
ORDER BY option_display_text"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result) > 0) { |
||||||
|
$options = Database::store_result($result, 'ASSOC'); |
||||||
|
} |
||||||
|
|
||||||
|
if ($to_json) { |
||||||
|
$string = null; |
||||||
|
if (!empty($options)) { |
||||||
|
$array = array(); |
||||||
|
foreach ($options as $option) { |
||||||
|
$array[$option['id']] = $option['option_display_text']; |
||||||
|
} |
||||||
|
$string = json_encode($array); |
||||||
|
} |
||||||
|
|
||||||
|
return $string; |
||||||
|
} |
||||||
|
|
||||||
|
return $options; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get options for a specific field as string split by ; |
||||||
|
* @param int Field ID |
||||||
|
* @param string Extra query bit for reordering |
||||||
|
* @return string HTML string of options |
||||||
|
* @assert (0, '') === null |
||||||
|
*/ |
||||||
|
public function get_field_options_by_field_to_string($field_id, $ordered_by = null) |
||||||
|
{ |
||||||
|
$field = new ExtraField($this->type); |
||||||
|
$field_info = $field->get($field_id); |
||||||
|
$options = self::get_field_options_by_field($field_id, false, $ordered_by); |
||||||
|
$elements = array(); |
||||||
|
if (!empty($options)) { |
||||||
|
switch ($field_info['field_type']) { |
||||||
|
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: |
||||||
|
$html = ExtraField::extra_field_double_select_convert_array_to_string($options); |
||||||
|
break; |
||||||
|
default: |
||||||
|
foreach ($options as $option) { |
||||||
|
$elements[] = $option['option_value']; |
||||||
|
} |
||||||
|
$html = implode(';', $elements); |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
return $html; |
||||||
|
} |
||||||
|
|
||||||
|
return null; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the maximum order value for a specific field |
||||||
|
* @param int Field ID |
||||||
|
* @return int Current max ID + 1 (we start from 0) |
||||||
|
* @assert (0, '') === 1 |
||||||
|
*/ |
||||||
|
public function get_max_order($field_id) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$sql = "SELECT MAX(option_order) FROM {$this->table} WHERE field_id = $field_id"; |
||||||
|
$res = Database::query($sql); |
||||||
|
$max = 1; |
||||||
|
if (Database::num_rows($res) > 0) { |
||||||
|
$row = Database::fetch_array($res); |
||||||
|
$max = $row[0] + 1; |
||||||
|
} |
||||||
|
|
||||||
|
return $max; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Update the option using the given params |
||||||
|
* @param array $params data to be saved |
||||||
|
*/ |
||||||
|
public function update($params) |
||||||
|
{ |
||||||
|
parent::update($params); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Display a form with the options for the field_id given in REQUEST |
||||||
|
* @return void Prints output |
||||||
|
*/ |
||||||
|
function display() |
||||||
|
{ |
||||||
|
// action links |
||||||
|
echo '<div class="actions">'; |
||||||
|
//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; |
||||||
|
echo '<a href="'.api_get_self( |
||||||
|
).'?action=add&type='.$this->type.'&field_id='.$field_id.'">'.Display::return_icon( |
||||||
|
'add_user_fields.png', |
||||||
|
get_lang('Add'), |
||||||
|
'', |
||||||
|
ICON_SIZE_MEDIUM |
||||||
|
).'</a>'; |
||||||
|
echo '</div>'; |
||||||
|
echo Display::grid_html('extra_field_options'); |
||||||
|
} |
||||||
|
|
||||||
|
public function getPriorityOptions() |
||||||
|
{ |
||||||
|
return array( |
||||||
|
'' => get_lang('SelectAnOption'), |
||||||
|
1 => get_lang('Success'), |
||||||
|
2 => get_lang('Info'), |
||||||
|
3 => get_lang('Warning'), |
||||||
|
4 => get_lang('Error'), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
public function getPriorityMessageType($priority) |
||||||
|
{ |
||||||
|
switch ($priority) { |
||||||
|
case 1: |
||||||
|
return 'success'; |
||||||
|
case 2: |
||||||
|
return 'info'; |
||||||
|
case 3: |
||||||
|
return 'warning'; |
||||||
|
case 4: |
||||||
|
return 'error'; |
||||||
|
} |
||||||
|
return null; |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns an HTML form for the current field |
||||||
|
* @param string URL to send the form to (action=...) |
||||||
|
* @param string Type of action to offer through the form (edit, usually) |
||||||
|
* @return string HTML form |
||||||
|
*/ |
||||||
|
public function return_form($url, $action) |
||||||
|
{ |
||||||
|
$form_name = $this->type.'_field'; |
||||||
|
$form = new FormValidator($form_name, 'post', $url); |
||||||
|
// Settting the form elements |
||||||
|
$header = get_lang('Add'); |
||||||
|
if ($action == 'edit') { |
||||||
|
$header = get_lang('Modify'); |
||||||
|
} |
||||||
|
|
||||||
|
$form->addElement('header', $header); |
||||||
|
$id = isset($_GET['id']) ? intval($_GET['id']) : ''; |
||||||
|
|
||||||
|
$form->addElement('hidden', 'id', $id); |
||||||
|
$form->addElement('hidden', 'type', $this->type); |
||||||
|
$form->addElement('hidden', 'field_id', $this->field_id); |
||||||
|
|
||||||
|
$form->addElement('text', 'option_display_text', get_lang('Name'), array('class' => 'span5')); |
||||||
|
$form->addElement('text', 'option_value', get_lang('Value'), array('class' => 'span5')); |
||||||
|
$form->addElement('text', 'option_order', get_lang('Order'), array('class' => 'span2')); |
||||||
|
$form->addElement('select', 'priority', get_lang('Priority'), $this->getPriorityOptions()); |
||||||
|
$form->addElement('textarea', 'priority_message', get_lang('PriorityMessage')); |
||||||
|
|
||||||
|
$defaults = array(); |
||||||
|
|
||||||
|
if ($action == 'edit') { |
||||||
|
// Setting the defaults |
||||||
|
$defaults = $this->get($id); |
||||||
|
$form->freeze('option_value'); |
||||||
|
$form->addElement('button', 'submit', get_lang('Modify'), 'class="save"'); |
||||||
|
} else { |
||||||
|
$form->addElement('button', 'submit', get_lang('Add'), 'class="save"'); |
||||||
|
} |
||||||
|
|
||||||
|
$form->setDefaults($defaults); |
||||||
|
|
||||||
|
// Setting the rules |
||||||
|
$form->addRule('option_display_text', get_lang('ThisFieldIsRequired'), 'required'); |
||||||
|
//$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required'); |
||||||
|
$form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required'); |
||||||
|
|
||||||
|
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; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,654 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
/** |
||||||
|
* Declaration for the ExtraFieldValue class, managing the values in extra |
||||||
|
* fields for any datatype |
||||||
|
* @package chamilo.library |
||||||
|
*/ |
||||||
|
/** |
||||||
|
* Class managing the values in extra fields for any datatype |
||||||
|
* @package chamilo.library.extrafields |
||||||
|
*/ |
||||||
|
|
||||||
|
class ExtraFieldValue extends Model |
||||||
|
{ |
||||||
|
public $type = null; |
||||||
|
public $columns = array('id', 'field_id', 'field_value', 'tms', 'comment'); |
||||||
|
/** @var string session_id, course_code, user_id, question id */ |
||||||
|
public $handler_id = null; |
||||||
|
public $entityName; |
||||||
|
|
||||||
|
/** |
||||||
|
* Formats the necessary elements for the given datatype |
||||||
|
* @param string The type of data to which this extra field applies (user, course, session, ...) |
||||||
|
* @return void (or false if unmanaged datatype) |
||||||
|
* @assert (-1) === false |
||||||
|
*/ |
||||||
|
public function __construct($type) |
||||||
|
{ |
||||||
|
$this->type = $type; |
||||||
|
$extra_field = new ExtraField($this->type); |
||||||
|
$this->handler_id = $extra_field->handler_id; |
||||||
|
|
||||||
|
switch ($this->type) { |
||||||
|
case 'course': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); |
||||||
|
$this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD); |
||||||
|
$this->author_id = 'user_id'; |
||||||
|
$this->entityName = 'ChamiloLMS\Entity\CourseFieldValues'; |
||||||
|
break; |
||||||
|
case 'user': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); |
||||||
|
$this->table_handler_field = Database::get_main_table(TABLE_MAIN_USER_FIELD); |
||||||
|
$this->author_id = 'author_id'; |
||||||
|
$this->entityName = 'ChamiloLMS\Entity\UserFieldValues'; |
||||||
|
break; |
||||||
|
case 'session': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); |
||||||
|
$this->table_handler_field = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); |
||||||
|
$this->author_id = 'user_id'; |
||||||
|
$this->entityName = 'ChamiloLMS\Entity\SessionFieldValues'; |
||||||
|
break; |
||||||
|
case 'question': |
||||||
|
$this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_VALUES); |
||||||
|
$this->table_handler_field = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD); |
||||||
|
$this->author_id = 'user_id'; |
||||||
|
$this->entityName = 'ChamiloLMS\Entity\QuestionFieldValues'; |
||||||
|
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: |
||||||
|
//unmanaged datatype, return false to let the caller know it |
||||||
|
// didn't work |
||||||
|
return false; |
||||||
|
} |
||||||
|
$this->columns[] = $this->handler_id; |
||||||
|
$this->columns[] = $this->author_id; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets the number of values stored in the table (all fields together) |
||||||
|
* for this type of resource |
||||||
|
* @return integer Number of rows in the table |
||||||
|
* @assert () !== false |
||||||
|
*/ |
||||||
|
public function get_count() |
||||||
|
{ |
||||||
|
$row = Database::select('count(*) as count', $this->table, array(), 'first'); |
||||||
|
return $row['count']; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Saves a series of records given as parameter into the coresponding table |
||||||
|
* @param array Structured parameter for the insertion into the *_field_values table |
||||||
|
* @return mixed false on empty params, void otherwise |
||||||
|
* @assert (array()) === false |
||||||
|
*/ |
||||||
|
public function save_field_values($params) |
||||||
|
{ |
||||||
|
$extra_field = new ExtraField($this->type); |
||||||
|
|
||||||
|
if (empty($params[$this->handler_id])) { |
||||||
|
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. |
||||||
|
foreach ($params as $key => $value) { |
||||||
|
if (substr($key, 0, 6) == 'extra_') { |
||||||
|
// An extra field. |
||||||
|
$field_variable = substr($key, 6); |
||||||
|
$extra_field_info = $extra_field->get_handler_field_info_by_field_variable($field_variable); |
||||||
|
|
||||||
|
if ($extra_field_info) { |
||||||
|
$commentVariable = 'extra_'.$field_variable.'_comment'; |
||||||
|
$comment = isset($params[$commentVariable]) ? $params[$commentVariable] : null; |
||||||
|
|
||||||
|
switch ($extra_field_info['field_type']) { |
||||||
|
case ExtraField::FIELD_TYPE_TAG : |
||||||
|
|
||||||
|
$old = self::getAllValuesByItemAndField( |
||||||
|
$extra_field_info['id'], |
||||||
|
$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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Save values in the *_field_values table |
||||||
|
* @param array Structured array with the values to save |
||||||
|
* @param boolean Whether to show the insert query (passed to the parent save() method) |
||||||
|
* @result mixed The result sent from the parent method |
||||||
|
* @assert (array()) === false |
||||||
|
*/ |
||||||
|
public function save($params, $show_query = false) |
||||||
|
{ |
||||||
|
$extra_field = new ExtraField($this->type); |
||||||
|
|
||||||
|
// Setting value to insert. |
||||||
|
$value = $params['field_value']; |
||||||
|
$value_to_insert = null; |
||||||
|
|
||||||
|
if (is_array($value)) { |
||||||
|
$value_to_insert = implode(';', $value); |
||||||
|
} else { |
||||||
|
$value_to_insert = Database::escape_string($value); |
||||||
|
} |
||||||
|
|
||||||
|
$params['field_value'] = $value_to_insert; |
||||||
|
|
||||||
|
//If field id exists |
||||||
|
$extra_field_info = $extra_field->get($params['field_id']); |
||||||
|
|
||||||
|
if ($extra_field_info) { |
||||||
|
switch ($extra_field_info['field_type']) { |
||||||
|
case ExtraField::FIELD_TYPE_RADIO: |
||||||
|
case ExtraField::FIELD_TYPE_SELECT: |
||||||
|
case ExtraField::FIELD_TYPE_SELECT_MULTIPLE: |
||||||
|
//$field_options = $session_field_option->get_field_options_by_field($params['field_id']); |
||||||
|
//$params['field_value'] = split(';', $value_to_insert); |
||||||
|
/* |
||||||
|
if ($field_options) { |
||||||
|
$check = false; |
||||||
|
foreach ($field_options as $option) { |
||||||
|
if (in_array($option['option_value'], $values)) { |
||||||
|
$check = true; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
if (!$check) { |
||||||
|
return false; //option value not found |
||||||
|
} |
||||||
|
} else { |
||||||
|
return false; //enumerated type but no option found |
||||||
|
}*/ |
||||||
|
break; |
||||||
|
case ExtraField::FIELD_TYPE_TEXT: |
||||||
|
case ExtraField::FIELD_TYPE_TEXTAREA: |
||||||
|
break; |
||||||
|
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: |
||||||
|
if (is_array($value)) { |
||||||
|
if (isset($value['extra_'.$extra_field_info['field_variable']]) && |
||||||
|
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']; |
||||||
|
} else { |
||||||
|
$value_to_insert = null; |
||||||
|
} |
||||||
|
} |
||||||
|
break; |
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
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['tms'] = api_get_utc_datetime(); |
||||||
|
$params[$this->author_id] = api_get_user_id(); |
||||||
|
|
||||||
|
// Insert |
||||||
|
if (empty($field_values)) { |
||||||
|
if ($extra_field_info['field_loggeable'] == 1) { |
||||||
|
global $app; |
||||||
|
switch($this->type) { |
||||||
|
case 'question': |
||||||
|
$extraFieldValue = new ChamiloLMS\Entity\QuestionFieldValues(); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setQuestionId($params[$this->handler_id]); |
||||||
|
break; |
||||||
|
case 'course': |
||||||
|
$extraFieldValue = new ChamiloLMS\Entity\CourseFieldValues(); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setQuestionId($params[$this->handler_id]); |
||||||
|
break; |
||||||
|
case 'user': |
||||||
|
$extraFieldValue = new ChamiloLMS\Entity\UserFieldValues(); |
||||||
|
$extraFieldValue->setUserId($params[$this->handler_id]); |
||||||
|
$extraFieldValue->setAuthorId(api_get_user_id()); |
||||||
|
break; |
||||||
|
case 'session': |
||||||
|
$extraFieldValue = new ChamiloLMS\Entity\SessionFieldValues(); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setSessionId($params[$this->handler_id]); |
||||||
|
break; |
||||||
|
} |
||||||
|
if (isset($extraFieldValue)) { |
||||||
|
if (!empty($params['field_value'])) { |
||||||
|
$extraFieldValue->setComment($params['comment']); |
||||||
|
$extraFieldValue->setFieldValue($params['field_value']); |
||||||
|
$extraFieldValue->setFieldId($params['field_id']); |
||||||
|
$extraFieldValue->setTms(api_get_utc_datetime(null, false, true)); |
||||||
|
$app['orm.ems']['db_write']->persist($extraFieldValue); |
||||||
|
$app['orm.ems']['db_write']->flush(); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
if ($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 { |
||||||
|
// Update |
||||||
|
if ($extra_field_info['field_loggeable'] == 1) { |
||||||
|
global $app; |
||||||
|
switch($this->type) { |
||||||
|
case 'question': |
||||||
|
$extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\QuestionFieldValues')->find($field_values['id']); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setQuestionId($params[$this->handler_id]); |
||||||
|
break; |
||||||
|
case 'course': |
||||||
|
$extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\CourseFieldValues')->find($field_values['id']); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setCourseCode($params[$this->handler_id]); |
||||||
|
break; |
||||||
|
case 'user': |
||||||
|
$extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\UserFieldValues')->find($field_values['id']); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setAuthorId(api_get_user_id()); |
||||||
|
break; |
||||||
|
case 'session': |
||||||
|
$extraFieldValue = $app['orm.ems']['db_write']->getRepository('ChamiloLMS\Entity\SessionFieldValues')->find($field_values['id']); |
||||||
|
$extraFieldValue->setUserId(api_get_user_id()); |
||||||
|
$extraFieldValue->setSessionId($params[$this->handler_id]); |
||||||
|
break; |
||||||
|
} |
||||||
|
|
||||||
|
if (isset($extraFieldValue)) { |
||||||
|
if (!empty($params['field_value'])) { |
||||||
|
|
||||||
|
/* |
||||||
|
* If the field value is similar to the previous value then the comment will be the same |
||||||
|
in order to no save in the log an empty record |
||||||
|
*/ |
||||||
|
if ($extraFieldValue->getFieldValue() == $params['field_value']) { |
||||||
|
if (empty($params['comment'])) { |
||||||
|
$params['comment'] = $extraFieldValue->getComment(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$extraFieldValue->setComment($params['comment']); |
||||||
|
$extraFieldValue->setFieldValue($params['field_value']); |
||||||
|
$extraFieldValue->setFieldId($params['field_id']); |
||||||
|
$extraFieldValue->setTms(api_get_utc_datetime(null, false, true)); |
||||||
|
$app['orm.ems']['db_write']->persist($extraFieldValue); |
||||||
|
$app['orm.ems']['db_write']->flush(); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
$params['id'] = $field_values['id']; |
||||||
|
return parent::update($params, $show_query); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 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 Field ID (the ID from the *_field table) |
||||||
|
* @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 false on error |
||||||
|
* @assert (-1,-1) === false |
||||||
|
*/ |
||||||
|
public function get_values_by_handler_and_field_id($item_id, $field_id, $transform = false) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$item_id = Database::escape_string($item_id); |
||||||
|
|
||||||
|
$sql = "SELECT s.*, field_type FROM {$this->table} s |
||||||
|
INNER JOIN {$this->table_handler_field} sf ON (s.field_id = sf.id) |
||||||
|
WHERE {$this->handler_id} = '$item_id' AND |
||||||
|
field_id = '".$field_id."' |
||||||
|
ORDER BY id"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result)) { |
||||||
|
$result = Database::fetch_array($result, 'ASSOC'); |
||||||
|
if ($transform) { |
||||||
|
if (!empty($result['field_value'])) { |
||||||
|
switch ($result['field_type']) { |
||||||
|
case ExtraField::FIELD_TYPE_DOUBLE_SELECT: |
||||||
|
$field_option = new ExtraFieldOption($this->type); |
||||||
|
$options = explode('::', $result['field_value']); |
||||||
|
// only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> '; |
||||||
|
$result = $field_option->get($options[0]); |
||||||
|
$result_second = $field_option->get($options[1]); |
||||||
|
if (!empty($result)) { |
||||||
|
$result['field_value'] = $result['option_display_text'].' -> '; |
||||||
|
$result['field_value'] .= $result_second['option_display_text']; |
||||||
|
} |
||||||
|
break; |
||||||
|
case ExtraField::FIELD_TYPE_SELECT: |
||||||
|
$field_option = new ExtraFieldOption($this->type); |
||||||
|
$extra_field_option_result = $field_option->get_field_option_by_field_and_option( |
||||||
|
$result['field_id'], |
||||||
|
$result['field_value'] |
||||||
|
); |
||||||
|
if (isset($extra_field_option_result[0])) { |
||||||
|
$result['field_value'] = $extra_field_option_result[0]['option_display_text']; |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return $result; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param string $tag |
||||||
|
* @param int $field_id |
||||||
|
* @param int $limit |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function searchValuesByField($tag, $field_id, $limit = 10) |
||||||
|
{ |
||||||
|
$field_id = 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 |
||||||
|
* a specific original item and a field name (like "branch", or "birthdate") |
||||||
|
* @param int Item ID from the original table |
||||||
|
* @param string The name of the field we are looking for |
||||||
|
* @return mixed Array of results, or false on error or not found |
||||||
|
* @assert (-1,'') === false |
||||||
|
*/ |
||||||
|
public function get_values_by_handler_and_field_variable($item_id, $field_variable, $transform = false) |
||||||
|
{ |
||||||
|
$item_id = Database::escape_string($item_id); |
||||||
|
$field_variable = Database::escape_string($field_variable); |
||||||
|
|
||||||
|
$sql = "SELECT s.*, field_type FROM {$this->table} s |
||||||
|
INNER JOIN {$this->table_handler_field} sf |
||||||
|
ON (s.field_id = sf.id) |
||||||
|
WHERE |
||||||
|
{$this->handler_id} = '$item_id' AND |
||||||
|
field_variable = '".$field_variable."' |
||||||
|
ORDER BY id"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result)) { |
||||||
|
$result = Database::fetch_array($result, 'ASSOC'); |
||||||
|
if ($transform) { |
||||||
|
if ($result['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) { |
||||||
|
if (!empty($result['field_value'])) { |
||||||
|
$field_option = new ExtraFieldOption($this->type); |
||||||
|
$options = explode('::', $result['field_value']); |
||||||
|
// only available for PHP 5.4 :( $result['field_value'] = $field_option->get($options[0])['id'].' -> '; |
||||||
|
$result = $field_option->get($options[0]); |
||||||
|
$result_second = $field_option->get($options[1]); |
||||||
|
if (!empty($result)) { |
||||||
|
$result['field_value'] = $result['option_display_text'].' -> '; |
||||||
|
$result['field_value'] .= $result_second['option_display_text']; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return $result; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Gets the ID from the item (course, session, etc) for which |
||||||
|
* the given field is defined with the given value |
||||||
|
* @param string Field (type of data) we want to check |
||||||
|
* @param string Data we are looking for in the given field |
||||||
|
* @return mixed Give the ID if found, or false on failure or not found |
||||||
|
* @assert (-1,-1) === false |
||||||
|
*/ |
||||||
|
public function get_item_id_from_field_variable_and_field_value($field_variable, $field_value, $transform = false) |
||||||
|
{ |
||||||
|
$field_value = Database::escape_string($field_value); |
||||||
|
$field_variable = Database::escape_string($field_variable); |
||||||
|
|
||||||
|
$sql = "SELECT {$this->handler_id} FROM {$this->table} s |
||||||
|
INNER JOIN {$this->table_handler_field} sf |
||||||
|
ON (s.field_id = sf.id) |
||||||
|
WHERE |
||||||
|
field_value = '$field_value' AND |
||||||
|
field_variable = '".$field_variable."' |
||||||
|
"; |
||||||
|
|
||||||
|
$result = Database::query($sql); |
||||||
|
if ($result !== false && Database::num_rows($result)) { |
||||||
|
$result = Database::fetch_array($result, 'ASSOC'); |
||||||
|
return $result; |
||||||
|
} else { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get all values for a specific field id |
||||||
|
* @param int Field ID |
||||||
|
* @return mixed Array of values on success, false on failure or not found |
||||||
|
* @assert (-1) === false |
||||||
|
*/ |
||||||
|
public function get_values_by_field_id($field_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"; |
||||||
|
$result = Database::query($sql); |
||||||
|
if (Database::num_rows($result)) { |
||||||
|
return Database::store_result($result, 'ASSOC'); |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param int $itemId |
||||||
|
* @param int $fieldId |
||||||
|
* @return array |
||||||
|
*/ |
||||||
|
public function getAllValuesByItemAndField($itemId, $fieldId) |
||||||
|
{ |
||||||
|
$fieldId = 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 |
||||||
|
* @param int Field ID |
||||||
|
* @return void |
||||||
|
* @assert ('a') == null |
||||||
|
*/ |
||||||
|
public function delete_all_values_by_field_id($field_id) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$sql = "DELETE FROM {$this->table} WHERE field_id = $field_id"; |
||||||
|
Database::query($sql); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Deletes values of a specific field for a specific item |
||||||
|
* @param int Item ID (session id, course id, etc) |
||||||
|
* @param int Field ID |
||||||
|
* @return void |
||||||
|
* @assert (-1,-1) == null |
||||||
|
*/ |
||||||
|
public function delete_values_by_handler_and_field_id($item_id, $field_id) |
||||||
|
{ |
||||||
|
$field_id = intval($field_id); |
||||||
|
$item_id = Database::escape_string($item_id); |
||||||
|
$sql = "DELETE FROM {$this->table} WHERE {$this->handler_id} = '$item_id' AND field_id = '".$field_id."' "; |
||||||
|
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 |
||||||
|
* @param int Item 1 |
||||||
|
* @param int Item 2 |
||||||
|
* @return mixed Differential array generated from the comparison |
||||||
|
*/ |
||||||
|
public function compare_item_values($item_id, $item_to_compare) |
||||||
|
{ |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue