Adding calendar event extra fields see BT#7802

1.9.x
Julio Montoya 11 years ago
parent 49f35deea4
commit dc784cfe99
  1. 46
      main/admin/extra_fields.php
  2. 77
      main/cron/import_csv.php
  3. 4
      main/inc/lib/database.constants.inc.php
  4. 47
      main/inc/lib/extra_field.lib.php
  5. 13
      main/inc/lib/extra_field_value.lib.php

@ -127,7 +127,8 @@ switch ($action) {
$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 '<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));
@ -149,7 +150,8 @@ switch ($action) {
$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 '<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));
@ -215,4 +217,44 @@ ALTER TABLE lp_field_values ADD INDEX (lp_id, field_id);
CREATE TABLE IF NOT EXISTS calendar_event_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 calendar_event_options;
CREATE TABLE IF NOT EXISTS calendar_event_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',
priority VARCHAR(255),
priority_message VARCHAR(255),
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS calendar_event_values;
CREATE TABLE IF NOT EXISTS calendar_event_values(
id bigint NOT NULL auto_increment,
calendar_event_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)
);
*/

@ -28,6 +28,7 @@ class ImportCsv
'session' => 'external_session_id',
'course' => 'external_course_id',
'user' => 'external_user_id',
'calendar_event' => 'external_calendar_event_id'
);
public $defaultAdminId = 1;
public $defaultSessionVisibility = 1;
@ -199,18 +200,28 @@ class ImportCsv
'External user id',
null
);
// Create course extra field: extra_external_course_id
CourseManager::create_course_extra_field(
$this->extraFieldIdNameList['course'],
1,
'External course id'
);
// Create session extra field extra_external_session_id
SessionManager::create_session_extra_field(
$this->extraFieldIdNameList['session'],
1,
'External session id'
);
// Create calendar_event extra field extra_external_session_id
$extraField = new ExtraField('calendar_event');
$extraField->save(array(
'field_type' => ExtraField::FIELD_TYPE_TEXT,
'field_variable' => $this->extraFieldIdNameList['calendar_event'],
'field_display_text' => 'External calendar event id'
));
}
/**
@ -719,7 +730,8 @@ class ImportCsv
'title' => $title,
'sender_id' => $teacherId,
'course_id' => $courseInfo['real_id'],
'session_id' => $sessionId
'session_id' => $sessionId,
$this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']
);
}
}
@ -732,7 +744,6 @@ class ImportCsv
return 0;
}
if ($errorFound == false) {
$this->logger->addInfo(
"Ready to insert events"
);
@ -740,12 +751,62 @@ class ImportCsv
$content = null;
$agenda = new Agenda();
$extraFieldValue = new ExtraFieldValue('calendar_event');
$extraFieldName = $this->extraFieldIdNameList['calendar_event'];
$externalEventId = null;
$extraField = new ExtraField('calendar_event');
$extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
if (empty($extraFieldInfo)) {
$this->logger->addInfo(
"No calendar event extra field created: $extraFieldName"
);
return 0;
}
foreach ($eventsToCreate as $event) {
if (!isset($event[$extraFieldName])) {
$this->logger->addInfo(
"No external_calendar_itemID found. Skipping ..."
);
continue;
} else {
$externalEventId = $event[$extraFieldName];
$item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
$extraFieldName,
$externalEventId
);
if (!empty($item) || empty($externalEventId)) {
$this->logger->addInfo(
"Event #$externalEventId was already added . Skipping ..."
);
continue;
}
}
$courseInfo = api_get_course_info_by_id($event['course_id']);
$agenda->set_course($courseInfo);
$agenda->setType('course');
$agenda->setSessionId($event['session_id']);
$agenda->setSenderId($event['sender_id']);
if (empty($courseInfo)) {
$this->logger->addInfo(
"No course found for added: #".$event['course_id']." Skipping ..."
);
continue;
}
if (empty($event['sender_id'])) {
$this->logger->addInfo(
"No sender found: #".$event['sender_id']." Skipping ..."
);
continue;
}
$eventId = $agenda->add_event(
$event['start'],
$event['end'],
@ -756,15 +817,15 @@ class ImportCsv
false //$addAsAnnouncement = false
);
$extraFieldValue->save(array(
'field_value' => $externalEventId,
'field_id' => $extraFieldInfo['id']
));
$this->logger->addInfo(
"Event added: #$eventId"
);
}
} else {
echo 'There was an error check the logs in '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
return 0;
}
}
if ($moveFile) {
@ -1237,6 +1298,8 @@ $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
$cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
echo 'To check error in '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
$import = new ImportCsv($logger, $cronImportCSVConditions);
if (isset($_configuration['default_admin_user_id_for_cron'])) {

@ -66,6 +66,10 @@ define('TABLE_MAIN_LP_FIELD', 'lp_field');
define('TABLE_MAIN_LP_FIELD_OPTIONS', 'lp_field_options');
define('TABLE_MAIN_LP_FIELD_VALUES', 'lp_field_values');
define('TABLE_MAIN_CALENDAR_EVENT_FIELD', 'calendar_event_field');
define('TABLE_MAIN_CALENDAR_EVENT_OPTIONS', 'calendar_event_options');
define('TABLE_MAIN_CALENDAR_EVENT_VALUES', 'calendar_event_values');
//User tags
define('TABLE_MAIN_TAG', 'tag');
define('TABLE_MAIN_USER_REL_TAG', 'user_rel_tag');

@ -20,7 +20,6 @@ class ExtraField extends Model
'tms'
);
public $ops = array(
'eq' => '=', //equal
'ne' => '<>', //not equal
@ -65,6 +64,13 @@ class ExtraField extends Model
{
$this->type = $type;
switch ($this->type) {
case 'calendar_event':
$this->table = Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD);
$this->table_field_options = Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_OPTIONS);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
$this->handler_id = 'calendar_event_id';
$this->primaryKey = 'id';
break;
case 'course':
$this->table_field_options = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_OPTIONS);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
@ -124,14 +130,15 @@ class ExtraField extends Model
/**
* @return array
*/
static function getValidExtraFieldTypes()
public static function getValidExtraFieldTypes()
{
return array(
'user',
'course',
'session',
'question',
'lp'
'lp',
'calendar_event'
);
}
@ -174,14 +181,16 @@ class ExtraField extends Model
}
/**
* @param $field_variable
* @param string $field_variable
*
* @return array|bool
*/
public function get_handler_field_info_by_field_variable($field_variable)
{
$field_variable = Database::escape_string($field_variable);
$sql_field = "SELECT * FROM {$this->table} WHERE field_variable = '$field_variable'";
$result = Database::query($sql_field);
$sql = "SELECT * FROM {$this->table}
WHERE field_variable = '$field_variable'";
$result = Database::query($sql);
if (Database::num_rows($result)) {
$r_field = Database::fetch_array($result, 'ASSOC');
@ -210,6 +219,7 @@ class ExtraField extends Model
/**
* @param $handler
*
* @return array
*/
public static function get_extra_fields_by_handler($handler)
@ -292,7 +302,9 @@ class ExtraField extends Model
if (!empty($fields) > 0) {
foreach ($fields as $field) {
$field_value = $field_values->get_values_by_handler_and_field_id($item_id, $field['id']);
$field_value = $field_values->get_values_by_handler_and_field_id(
$item_id, $field['id']
);
if ($field_value) {
$field_value = $field_value['field_value'];
@ -372,12 +384,17 @@ class ExtraField extends Model
* Converts a string like this:
* France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
* into
* array('France' => array('Paris', 'Bregtane', 'Marseilles'), 'Belgique' => array('Namur', 'Liège', etc
* array(
* 'France' =>
* array('Paris', 'Bregtane', 'Marseilles'),
* 'Belgique' =>
* array('Namur', 'Liège')
* ), etc
* @param string $string
*
* @return array
*/
static function extra_field_double_select_convert_string_to_array($string)
public static function extra_field_double_select_convert_string_to_array($string)
{
$options = explode('|', $string);
$options_parsed = array();
@ -398,7 +415,7 @@ class ExtraField extends Model
* @param array $options
* @return array
*/
static function extra_field_double_select_convert_array_to_ordered_array($options)
public static function extra_field_double_select_convert_array_to_ordered_array($options)
{
$options_parsed = array();
if (!empty($options)) {
@ -419,7 +436,7 @@ class ExtraField extends Model
*
* @return string
*/
static function extra_field_double_select_convert_array_to_string($options)
public static function extra_field_double_select_convert_array_to_string($options)
{
$string = null;
$options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options);
@ -474,10 +491,10 @@ class ExtraField extends Model
*/
public function save($params, $show_query = false)
{
$session_field_info = self::get_handler_field_info_by_field_variable($params['field_variable']);
$fieldInfo = self::get_handler_field_info_by_field_variable($params['field_variable']);
$params = self::clean_parameters($params);
if ($session_field_info) {
return $session_field_info['id'];
if ($fieldInfo) {
return $fieldInfo['id'];
} else {
if (!isset($params['tms'])) {
$params['tms'] = api_get_utc_datetime();
@ -495,6 +512,7 @@ class ExtraField extends Model
/**
* @param $params
*
* @return bool|void
*/
public function update($params)
@ -510,6 +528,7 @@ class ExtraField extends Model
/**
* @param $id
*
* @return bool|void
*/
public function delete($id)

@ -18,7 +18,8 @@ class ExtraFieldValue extends Model
/**
* Formats the necessary elements for the given datatype
* @param string The type of data to which this extra field applies (user, course, session, ...)
* @param string $type The type of data to which this extra field
* applies (user, course, session, ...)
* @return void (or false if unmanaged datatype)
* @assert (-1) === false
*/
@ -29,6 +30,12 @@ class ExtraFieldValue extends Model
$this->handler_id = $extra_field->handler_id;
switch ($this->type) {
case 'calendar_event':
$this->table = Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
$this->author_id = 'user_id';
break;
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);
@ -168,8 +175,8 @@ class ExtraFieldValue extends Model
/**
* 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)
* @param array $params Structured array with the values to save
* @param boolean $show_query Whether to show the insert query (passed to the parent save() method)
* @result mixed The result sent from the parent method
* @assert (array()) === false
*/

Loading…
Cancel
Save