Improving extra field management, with 1 script we can manage all extra fields (course, session, question) see BT#6098

skala
Julio Montoya 13 years ago
parent b5acc6cb3b
commit 3b60576f0c
  1. 82
      main/admin/extra_field_options.php
  2. 185
      main/admin/extra_fields.php
  3. 7
      main/admin/index.php
  4. 200
      main/admin/session_fields.php
  5. 5
      main/inc/lib/database.constants.inc.php
  6. 166
      main/inc/lib/extra_field.lib.php
  7. 206
      main/inc/lib/extra_field_option.lib.php
  8. 86
      main/inc/lib/model.lib.php
  9. 103
      main/inc/lib/session_field.lib.php

@ -25,67 +25,69 @@ $tool_name = null;
$action = isset($_GET['action']) ? $_GET['action'] : null;
$field_id = isset($_GET['field_id']) ? $_GET['field_id'] : null;
$type = 'session';
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 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();
$token = Security::get_token();
if ($action == 'add') {
$interbreadcrumb[]=array('url' => 'session_fields.php','name' => get_lang('ExtraFields'));
$interbreadcrumb[]=array('url' => 'session_fields.php?action=edit&id='.$extra_field_info['id'],'name' => $extra_field_info['field_display_text']);
$interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$type.'&field_id='.$extra_field_info['id'], 'name' => get_lang('EditExtraFieldOptions'));
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type,'name' => get_lang('ExtraFields'));
$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' => 'session_fields.php','name' => get_lang('ExtraFields'));
$interbreadcrumb[]=array('url' => 'session_fields.php?action=edit&id='.$extra_field_info['id'],'name' => $extra_field_info['field_display_text']);
$interbreadcrumb[]=array('url' => 'extra_field_options.php?type='.$type.'&field_id='.$extra_field_info['id'], 'name' => get_lang('EditExtraFieldOptions'));
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type,'name' => get_lang('ExtraFields'));
$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' => 'session_fields.php','name' => get_lang('ExtraFields'));
$interbreadcrumb[]=array('url' => 'session_fields.php?action=edit&id='.$extra_field_info['id'],'name' => $extra_field_info['field_display_text']);
$interbreadcrumb[]=array('url' => 'extra_fields.php?type='.$extra_field->type,'name' => get_lang('ExtraFields'));
$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='.$type;
$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
//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
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';
//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>'.
'&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) return false;" href="?sec_token='.$token.'&action=delete&'.$params.'&id=\'+options.rowId+\'">'.Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>'.
'\';
}';
return \'<a href="?action=edit&'.$params.'&id=\'+options.rowId+\'">'.Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'.
'&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) 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>';
@ -94,7 +96,7 @@ Display::display_header($tool_name);
echo Display::page_header($extra_field_info['field_display_text']);
$obj = new ExtraFieldOption($type);
$obj = new ExtraFieldOption($extra_field->type);
$obj->field_id = $field_id;
// Action handling: Add
@ -102,7 +104,7 @@ 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');
@ -110,11 +112,11 @@ switch ($action) {
if ($form->validate()) {
if ($check) {
$values = $form->exportValues();
$res = $obj->save_one_item($values);
$res = $obj->save_one_item($values);
if ($res) {
Display::display_confirmation_message(get_lang('ItemAdded'));
}
}
}
$obj->display();
} else {
/*echo '<div class="actions">';
@ -126,17 +128,17 @@ switch ($action) {
}
break;
case 'edit':
// Action handling: Editing
// Action handling: Editing
$url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']).'&'.$params;
$form = $obj->return_form($url, 'edit');
$form = $obj->return_form($url, 'edit');
// The validation or display
if ($form->validate()) {
if ($form->validate()) {
if ($check) {
$values = $form->exportValues();
$res = $obj->update($values);
Display::display_confirmation_message(sprintf(get_lang('ItemUpdated'), $values['name']), false);
}
$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">';
@ -158,7 +160,7 @@ switch ($action) {
$obj->display();
break;
default:
$obj->display();
$obj->display();
break;
}
Display :: display_footer();

@ -0,0 +1,185 @@
<?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;
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;
$extraFieldType = isset($_REQUEST['type']) ? $_REQUEST['type'] : 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;
}
});
var value = 1;
$("#advanced_parameters").on("click", function() {
$("#options").toggle(function() {
if (value == 1) {
$("#advanced_parameters").addClass("btn-hide");
value = 0;
} else {
$("#advanced_parameters").removeClass("btn-hide");
value = 1;
}
});
});
});
</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['name']), 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();

@ -120,6 +120,11 @@ if (api_is_platform_admin()) {
$items[] = array('url'=>'subscribe_user2course.php', 'label' => get_lang('AddUsersToACourse'));
$items[] = array('url'=>'course_user_import.php', 'label' => get_lang('ImportUsersToACourse'));
$items[] = array('url'=>'extra_fields.php?type=course', 'label' => get_lang('ManageCourseFields'));
$items[] = array('url'=>'extra_fields.php?type=question', 'label' => get_lang('ManageQuestionFields'));
if (api_get_setting('gradebook_enable_grade_model') == 'true') {
$items[] = array('url'=>'grade_models.php', 'label' => get_lang('GradeModel'));
}
@ -203,7 +208,7 @@ if (api_is_platform_admin()) {
}
$items[] = array('url'=>'usergroups.php', 'label' => get_lang('Classes'));
$items[] = array('url'=>'session_fields.php', 'label' => get_lang('ManageSessionFields'));
$items[] = array('url'=>'extra_fields.php?type=session', 'label' => get_lang('ManageSessionFields'));
$blocks['sessions']['items'] = $items;
$blocks['sessions']['extra'] = null;

@ -1,200 +0,0 @@
<?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;
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;
$check = Security::check_token('request');
$token = Security::get_token();
if ($action == 'add') {
$interbreadcrumb[]=array('url' => 'session_fields.php','name' => get_lang('SessionFields'));
$interbreadcrumb[]=array('url' => '#','name' => get_lang('Add'));
} elseif ($action == 'edit') {
$interbreadcrumb[]=array('url' => 'session_fields.php','name' => get_lang('SessionFields'));
$interbreadcrumb[]=array('url' => '#','name' => get_lang('Edit'));
} else {
$interbreadcrumb[]=array('url' => '#','name' => get_lang('SessionFields'));
}
//jqgrid will use this URL to do the selects
$url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_extra_fields&type=session';
//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('FieldLabel'), get_lang('Type'), get_lang('FieldChangeability'), get_lang('Visibility'), get_lang('Filter'), get_lang('FieldOrder'), get_lang('Actions'));
//Column config
$column_model = array(
array('name'=>'field_display_text', 'index'=>'field_display_text', 'width'=>'180', 'align'=>'left'),
array('name'=>'field_variable', 'index'=>'field_variable', 'width'=>'', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_type', 'index'=>'field_type', 'width'=>'', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_changeable', 'index'=>'field_changeable', 'width'=>'50', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_visible', 'index'=>'field_visible', 'width'=>'40', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_filter', 'index'=>'field_filter', 'width'=>'30', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_order', 'index'=>'field_order', 'width'=>'40', 'align'=>'left','sortable'=>'true'),
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';
$extra_params['sortname'] = 'field_order';
//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&id=\'+options.rowId+\'">'.Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'.
'&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) return false;" href="?sec_token='.$token.'&action=delete&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('session_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;
}
});
var value = 1;
$("#advanced_parameters").on("click", function() {
$("#options").toggle(function() {
if (value == 1) {
$("#advanced_parameters").addClass("btn-hide");
value = 0;
} else {
$("#advanced_parameters").removeClass("btn-hide");
value = 1;
}
});
});
});
</script>';
// The header.
Display::display_header($tool_name);
$obj = new SessionField();
// 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']);
$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().'">'.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']);
$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();

@ -105,6 +105,11 @@ define('TABLE_MAIN_SESSION_FIELD', 'session_field');
define('TABLE_MAIN_SESSION_FIELD_OPTIONS', 'session_field_options');
define('TABLE_MAIN_SESSION_FIELD_VALUES', 'session_field_values');
define('TABLE_MAIN_QUESTION_FIELD', 'question_field');
define('TABLE_MAIN_QUESTION_FIELD_OPTIONS', 'question_field_options');
define('TABLE_MAIN_QUESTION_FIELD_VALUES', 'question_field_values');
// Message
define('TABLE_MAIN_MESSAGE', 'message');

@ -32,6 +32,8 @@ class ExtraField extends Model
public $type = 'user'; //or session or course
public $handler_id = 'user_id';
public $pageName;
public $pageUrl;
function __construct($type)
{
@ -61,7 +63,26 @@ class ExtraField extends Model
$this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$this->handler_id = 'session_id';
break;
case 'question':
$this->table_field_options = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_VALUES);
//Used for the model
$this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD);
$this->handler_id = 'question_id';
break;
}
$this->pageUrl = 'extra_fields.php?type='.$this->type;
$this->pageName = get_lang(ucwords($this->type).'Fields'); // Example QuestionFields
}
static function getValidExtraFieldTypes() {
return array(
'user',
'course',
'session',
'question'
);
}
public function get_count()
@ -760,4 +781,149 @@ EOF;
return $return;
}
function setupBreadcrumb(&$breadcrumb, $action)
{
if ($action == 'add') {
$breadcrumb[]=array('url' => $this->pageUrl,'name' => $this->pageName);
$breadcrumb[]=array('url' => '#','name' => get_lang('Add'));
} elseif ($action == 'edit') {
$breadcrumb[]=array('url' => $this->pageUrl,'name' => $this->pageName);
$breadcrumb[]=array('url' => '#','name' => get_lang('Edit'));
} else {
$breadcrumb[]=array('url' => '#','name' => $this->pageName);
}
}
/**
* Displays the title + grid
*/
public 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>';
echo '<a href="' . api_get_self() . '?action=add&type='.$this->type.'">' . Display::return_icon('add_user_fields.png', get_lang('Add'), '', ICON_SIZE_MEDIUM) . '</a>';
echo '</div>';
echo Display::grid_html($this->type.'_fields');
}
public function getJqgridColumnNames() {
return array(get_lang('Name'), get_lang('FieldLabel'), get_lang('Type'), get_lang('FieldChangeability'), get_lang('Visibility'), get_lang('Filter'), get_lang('FieldOrder'), get_lang('Actions'));
}
public function getJqgridColumnModel() {
return array(
array('name'=>'field_display_text', 'index'=>'field_display_text', 'width'=>'180', 'align'=>'left'),
array('name'=>'field_variable', 'index'=>'field_variable', 'width'=>'', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_type', 'index'=>'field_type', 'width'=>'', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_changeable', 'index'=>'field_changeable', 'width'=>'50', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_visible', 'index'=>'field_visible', 'width'=>'40', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_filter', 'index'=>'field_filter', 'width'=>'30', 'align'=>'left','sortable'=>'true'),
array('name'=>'field_order', 'index'=>'field_order', 'width'=>'40', 'align'=>'left','sortable'=>'true'),
array('name'=>'actions', 'index'=>'actions', 'width'=>'100', 'align'=>'left','formatter'=>'action_formatter','sortable'=>'false')
);
}
public function return_form($url, $action)
{
$form = new FormValidator($this->type.'_field', 'post', $url);
$form->addElement('hidden', 'type', $this->type);
$id = isset($_GET['id']) ? intval($_GET['id']) : null;
$form->addElement('hidden', 'id', $id);
// Setting the form elements
$header = get_lang('Add');
$defaults = array();
if ($action == 'edit') {
$header = get_lang('Modify');
// Setting the defaults
$defaults = $this->get($id);
}
$form->addElement('header', $header);
$form->addElement('text', 'field_display_text', get_lang('Name'), array('class' => 'span5'));
// Field type
$types = self::get_field_types();
$form->addElement('select', 'field_type', get_lang('FieldType'), $types, array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select')));
$form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
//$form->addElement('advanced_settings','<a class="btn btn-show" id="advanced_parameters" href="javascript://">'.get_lang('AdvancedParameters').'</a>');
//$form->addElement('html','<div id="options" style="display:none">');
$form->addElement('text', 'field_variable', get_lang('FieldLabel'), array('class' => 'span5'));
$form->addElement('text', 'field_options', get_lang('FieldPossibleValues'), array('id' => 'field_options', 'class' => 'span6'));
if ($action == 'edit') {
if (in_array($defaults['field_type'], array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT))) {
$url = Display::url(get_lang('EditExtraFieldOptions'), 'extra_field_options.php?type='.$this->type.'&field_id=' . $id);
$form->addElement('label', null, $url);
$form->freeze('field_options');
}
}
$form->addElement('text', 'field_default_value', get_lang('FieldDefaultValue'), array('id' => 'field_default_value', 'class' => 'span5'));
$group = array();
$group[] = $form->createElement('radio', 'field_visible', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'field_visible', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('Visible'), '', false);
$group = array();
$group[] = $form->createElement('radio', 'field_changeable', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'field_changeable', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
$group = array();
$group[] = $form->createElement('radio', 'field_filter', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'field_filter', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('FieldFilter'), '', false);
$form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
if ($action == 'edit') {
$option = new ExtraFieldOption($this->type);
if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
$form->freeze('field_options');
}
$defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
$form->addElement('button', 'submit', get_lang('Modify'), 'class="save"');
} else {
$defaults['field_visible'] = 0;
$defaults['field_changeable'] = 0;
$defaults['field_filter'] = 0;
$form->addElement('button', 'submit', get_lang('Add'), 'class="save"');
}
/*if (!empty($defaults['created_at'])) {
$defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
}
if (!empty($defaults['updated_at'])) {
$defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
}*/
$form->setDefaults($defaults);
// Setting the rules
$form->addRule('field_display_text', get_lang('ThisFieldIsRequired'), 'required');
//$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
return $form;
}
public function getJqgridActionLinks($token)
{
//With this function we can add actions to the jgrid (edit, delete, etc)
return 'function action_formatter(cellvalue, options, rowObject) {
return \'<a href="?action=edit&type='.$this->type.'&id=\'+options.rowId+\'">'.Display::return_icon('edit.png',get_lang('Edit'),'',ICON_SIZE_SMALL).'</a>'.
'&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES))."\'".')) return false;" href="?sec_token='.$token.'&type='.$this->type.'&action=delete&id=\'+options.rowId+\'">'.Display::return_icon('delete.png',get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>'.
'\';
}';
}
}

@ -6,25 +6,27 @@
/**
* Handles the extra fields for various objects (users, sessions, courses)
*/
class ExtraFieldOption extends Model {
class ExtraFieldOption extends Model
{
public $columns = array('id', 'field_id', 'option_value', 'option_display_text', 'option_order', '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) {
public function __construct($type)
{
$this->type = $type;
switch ($this->type) {
case 'course':
switch ($this->type) {
case 'course':
$this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_OPTIONS);
break;
case 'user':
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;
$this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS);
break;
}
}
/**
@ -35,7 +37,7 @@ class ExtraFieldOption extends Model {
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
@ -48,7 +50,7 @@ class ExtraFieldOption extends Model {
if (empty($field_id)) {
return false;
}
$row = Database::select('count(*) as count', $this->table, array('where' => array('field_id = ?' => $field_id)), 'first');
$row = Database::select('count(*) as count', $this->table, array('where' => array('field_id = ?' => $field_id)), 'first');
return $row['count'];
}
/**
@ -61,7 +63,7 @@ class ExtraFieldOption extends Model {
*/
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();
$new_options = array();
if (!empty($options)) {
foreach ($options as $option) {
$new_options[] = $option['option_value'].':'.$option['option_display_text'];
@ -87,35 +89,35 @@ class ExtraFieldOption extends Model {
* 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
* @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']) &&
$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_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']);
//$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) {
foreach ($options_parsed as $key => $option) {
$sub_options = $option['options'];
$new_params = array(
'field_id' => $field_id,
'field_id' => $field_id,
'option_value' => 0,
'option_display_text' => $option['label'],
'option_order' => 0,
@ -123,32 +125,32 @@ class ExtraFieldOption extends Model {
);
//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) {
parent::update($new_params, $show_query);
}
foreach ($sub_options as $sub_option) {
if (!empty($sub_option)) {
$new_params = array(
'field_id' => $field_id,
'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);
$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 {
parent::save($new_params, $show_query);
} else {
$new_params['id'] = $option_info['id'];
parent::update($new_params, $show_query);
}
parent::update($new_params, $show_query);
}
}
}
}
}
@ -156,28 +158,28 @@ class ExtraFieldOption extends Model {
} 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);
if ($option_info == false) {
$order = self::get_max_order($field_id);
$order = self::get_max_order($field_id);
$new_params = array(
'field_id' => $field_id,
'field_id' => $field_id,
'option_value' => $option,
'option_display_text' => $option,
'option_order' => $order,
'tms' => $time,
);
parent::save($new_params, $show_query);
parent::save($new_params, $show_query);
}
}
}
}
return true;
return true;
}
/**
* Save one option item at a time
* @param array Parameters specific to the option
@ -188,46 +190,46 @@ class ExtraFieldOption extends Model {
* @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']);
$field_id = intval($params['field_id']);
if (empty($field_id)) {
return false;
}
$params['tms'] = api_get_utc_datetime();
$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);
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);
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."'";
$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;
return false;
}
/**
* Get the complete row of a specific option's display text of a specific field
* @param int Field ID
@ -238,15 +240,15 @@ class ExtraFieldOption extends Model {
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;
return false;
}
/**
* Get the complete row of a specific option's display text of a specific field
* @param int Field ID
@ -259,15 +261,15 @@ class ExtraFieldOption extends Model {
$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'";
$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;
return false;
}
/**
* Gets an array of options for a specific field
* @param int The field ID
@ -278,19 +280,19 @@ class ExtraFieldOption extends Model {
*/
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}
$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')) {
while ($row = Database::fetch_array($result, 'ASSOC')) {
$options[$row['id']] = $row;
}
return $options;
@ -298,7 +300,7 @@ class ExtraFieldOption extends Model {
return Database::store_result($result, 'ASSOC');
}
}
return false;
return false;
}
/**
* Get options for a specific field as array or in JSON format suited for the double-select format
@ -309,31 +311,31 @@ class ExtraFieldOption extends Model {
*/
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);
$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
$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) {
foreach ($options as $option) {
$array[$option['id']] = $option['option_display_text'];
}
$string = json_encode($array);
}
$string = json_encode($array);
}
return $string;
}
return $options;
}
return $options;
}
/**
* Get options for a specific field as string split by ;
* @param int Field ID
@ -341,28 +343,28 @@ class ExtraFieldOption extends Model {
* @return string HTML string of options
* @assert (0, '') === null
*/
public function get_field_options_by_field_to_string($field_id, $ordered_by = 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);
$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);
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;
break;
}
return $html;
}
return null;
}
/**
* Get the maximum order value for a specific field
@ -388,7 +390,7 @@ class ExtraFieldOption extends Model {
public function update($params) {
parent::update($params);
}
/**
* Display a form with the options for the field_id given in REQUEST
* @return void Prints output
@ -396,9 +398,9 @@ class ExtraFieldOption extends Model {
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>';
//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 '<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');
}
@ -412,39 +414,39 @@ class ExtraFieldOption extends Model {
$form_name = $this->type.'_field';
$form = new FormValidator($form_name, 'post', $url);
// Settting the form elements
$header = get_lang('Add');
$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', '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('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'));
$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 {
} 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('option_display_text', get_lang('ThisFieldIsRequired'), 'required');
//$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('option_value', get_lang('ThisFieldIsRequired'), 'required');
return $form;
}
}
}

@ -10,17 +10,17 @@
* @package chamilo.library
*/
class Model {
public $table;
public $columns;
public $required;
public $is_course_model =false;
// var $pk; some day this will be implemented
public function __construct() {
}
public function set($id) {
/*$data = self::get($id);
foreach ($data as $key => $value) {
@ -29,7 +29,7 @@ class Model {
}
}*/
}
/**
* Useful finder - experimental akelos like only use in notification.lib.php send function
*/
@ -43,48 +43,48 @@ class Model {
break;
}
}
/**
* Delets an item
*/
public function delete($id) {
if (empty($id) or $id != strval(intval($id))) { return false; }
$params = array('id = ?' => $id);
$params = array('id = ?' => $id);
if ($this->is_course_model) {
$course_id = api_get_course_int_id();
$params = array('id = ? AND c_id = ?' => array($id, $course_id));
}
// Database table definition
$result = Database :: delete($this->table,$params );
$result = Database :: delete($this->table,$params );
if ($result != 1){
return false;
}
}
return true;
}
private function clean_parameters($params){
$clean_params = array();
if (!empty($params)) {
foreach($params as $key=>$value) {
if (in_array($key, $this->columns)) {
$clean_params[$key] = $value;
}
}
}
}
return $clean_params;
}
/**
* Displays the title + grid
*/
public function display() {
}
public function display() {
}
/**
* Gets an element
*/
public function get($id) {
if (empty($id)) { return array(); }
if (empty($id)) { return array(); }
$params = array('id = ?'=>intval($id));
if ($this->is_course_model) {
$course_id = api_get_course_int_id();
@ -93,31 +93,31 @@ class Model {
$result = Database::select('*',$this->table, array('where' => $params),'first');
return $result;
}
public function get_all($options = null) {
return Database::select('*', $this->table, $options);
}
public function get_first($options = null) {
return Database::select('*', $this->table, $options, 'first');
}
public function get_all_for_export($options = null) {
return Database::select('name, description', $this->table, $options);
}
/**
* Get the count of elements
*/
public function get_count() {
public function get_count() {
$row = Database::select('count(*) as count', $this->table, array('where' => array('parent_id = ?' => '0')),'first');
return $row['count'];
}
/**
* a little bit of javascript to display
*/
public function javascript() {
public function javascript() {
}
/**
@ -129,15 +129,15 @@ class Model {
*/
public function save($params, $show_query = false) {
$params = $this->clean_parameters($params);
if ($this->is_course_model) {
if ($this->is_course_model) {
if (!isset($params['c_id']) || empty($params['c_id'])) {
$params['c_id'] = api_get_course_int_id();
}
}
if (!empty($this->required)) {
$require_ok = true;
$require_ok = true;
$kay_params = array_keys($params);
foreach ($this->required as $field) {
if (!in_array($field, $kay_params)) {
@ -145,23 +145,23 @@ class Model {
}
}
if (!$require_ok) {
return false;
return false;
}
}
if (in_array('created_at', $this->columns)) {
if (in_array('created_at', $this->columns)) {
$params['created_at'] = api_get_utc_datetime();
}
if (!empty($params)) {
$id = Database::insert($this->table, $params, $show_query);
$id = Database::insert($this->table, $params, $show_query);
if (is_numeric($id)){
return $id;
}
}
return false;
}
/**
* Updates the obj in the database. The $params['id'] must exist in order to update a record
*
@ -169,32 +169,32 @@ class Model {
*
*/
public function update($params) {
$params = $this->clean_parameters($params);
if ($this->is_course_model) {
$params = $this->clean_parameters($params);
if ($this->is_course_model) {
if (!isset($params['c_id']) || empty($params['c_id'])) {
$params['c_id'] = api_get_course_int_id();
}
}
//If the class has the updated_at field we update the date
if (in_array('updated_at', $this->columns)) {
if (in_array('updated_at', $this->columns)) {
$params['updated_at'] = api_get_utc_datetime();
}
//If the class has the created_at field then we remove it
if (in_array('created_at', $this->columns)) {
unset($params['created_at']);
}
if (!empty($params) && !empty($params['id'])) {
$id = intval($params['id']);
unset($params['id']); //To not overwrite the id
unset($params['id']); //To not overwrite the id
if (is_numeric($id)) {
$result = Database::update($this->table, $params, array('id = ?'=>$id));
$result = Database::update($this->table, $params, array('id = ?'=>$id));
if ($result){
return true;
}
}
}
}
}
return false;
}

@ -1,105 +1,10 @@
<?php
/* For licensing terms, see /license.txt */
class SessionField extends ExtraField {
public function __construct() {
class SessionField extends ExtraField
{
public function __construct()
{
parent::__construct('session');
}
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>';
echo '<a href="' . api_get_self() . '?action=add">' . Display::return_icon('add_user_fields.png', get_lang('Add'), '', ICON_SIZE_MEDIUM) . '</a>';
echo '</div>';
echo Display::grid_html('session_fields');
}
public function return_form($url, $action) {
$form = new FormValidator('session_field', 'post', $url);
$id = isset($_GET['id']) ? intval($_GET['id']) : null;
$form->addElement('hidden', 'id', $id);
// Settting the form elements
$header = get_lang('Add');
$defaults = array();
if ($action == 'edit') {
$header = get_lang('Modify');
// Setting the defaults
$defaults = $this->get($id);
}
$form->addElement('header', $header);
$form->addElement('text', 'field_display_text', get_lang('Name'), array('class' => 'span5'));
// Field type
$types = self::get_field_types();
$form->addElement('select', 'field_type', get_lang('FieldType'), $types, array('id' => 'field_type', 'class' => 'chzn-select', 'data-placeholder' => get_lang('Select')));
$form->addElement('label', get_lang('Example'), '<div id="example">-</div>');
//$form->addElement('advanced_settings','<a class="btn btn-show" id="advanced_parameters" href="javascript://">'.get_lang('AdvancedParameters').'</a>');
//$form->addElement('html','<div id="options" style="display:none">');
$form->addElement('text', 'field_variable', get_lang('FieldLabel'), array('class' => 'span5'));
$form->addElement('text', 'field_options', get_lang('FieldPossibleValues'), array('id' => 'field_options', 'class' => 'span6'));
if ($action == 'edit') {
if (in_array($defaults['field_type'], array(ExtraField::FIELD_TYPE_SELECT, ExtraField::FIELD_TYPE_DOUBLE_SELECT))) {
$url = Display::url(get_lang('EditExtraFieldOptions'), 'extra_field_options.php?type=session&field_id=' . $id);
$form->addElement('label', null, $url);
$form->freeze('field_options');
}
}
$form->addElement('text', 'field_default_value', get_lang('FieldDefaultValue'), array('id' => 'field_default_value', 'class' => 'span5'));
$group = array();
$group[] = $form->createElement('radio', 'field_visible', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'field_visible', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('Visible'), '', false);
$group = array();
$group[] = $form->createElement('radio', 'field_changeable', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'field_changeable', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('FieldChangeability'), '', false);
$group = array();
$group[] = $form->createElement('radio', 'field_filter', null, get_lang('Yes'), 1);
$group[] = $form->createElement('radio', 'field_filter', null, get_lang('No'), 0);
$form->addGroup($group, '', get_lang('FieldFilter'), '', false);
$form->addElement('text', 'field_order', get_lang('FieldOrder'), array('class' => 'span1'));
if ($action == 'edit') {
$option = new SessionFieldOption('session');
if ($defaults['field_type'] == ExtraField::FIELD_TYPE_DOUBLE_SELECT) {
$form->freeze('field_options');
}
$defaults['field_options'] = $option->get_field_options_by_field_to_string($id);
$form->addElement('button', 'submit', get_lang('Modify'), 'class="save"');
} else {
$defaults['field_visible'] = 0;
$defaults['field_changeable'] = 0;
$defaults['field_filter'] = 0;
$form->addElement('button', 'submit', get_lang('Add'), 'class="save"');
}
/*if (!empty($defaults['created_at'])) {
$defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
}
if (!empty($defaults['updated_at'])) {
$defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
}*/
$form->setDefaults($defaults);
// Setting the rules
$form->addRule('field_display_text', get_lang('ThisFieldIsRequired'), 'required');
//$form->addRule('field_variable', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('field_type', get_lang('ThisFieldIsRequired'), 'required');
return $form;
}
}
Loading…
Cancel
Save