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. 6
      main/inc/lib/database.constants.inc.php
  4. 57
      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(); $obj->display();
} else { } else {
echo '<div class="actions">'; 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>'; echo '</div>';
$form->addElement('hidden', 'sec_token'); $form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $token)); $form->setConstants(array('sec_token' => $token));
@ -149,7 +150,8 @@ switch ($action) {
$obj->display(); $obj->display();
} else { } else {
echo '<div class="actions">'; 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>'; echo '</div>';
$form->addElement('hidden', 'sec_token'); $form->addElement('hidden', 'sec_token');
$form->setConstants(array('sec_token' => $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', 'session' => 'external_session_id',
'course' => 'external_course_id', 'course' => 'external_course_id',
'user' => 'external_user_id', 'user' => 'external_user_id',
'calendar_event' => 'external_calendar_event_id'
); );
public $defaultAdminId = 1; public $defaultAdminId = 1;
public $defaultSessionVisibility = 1; public $defaultSessionVisibility = 1;
@ -199,18 +200,28 @@ class ImportCsv
'External user id', 'External user id',
null null
); );
// Create course extra field: extra_external_course_id // Create course extra field: extra_external_course_id
CourseManager::create_course_extra_field( CourseManager::create_course_extra_field(
$this->extraFieldIdNameList['course'], $this->extraFieldIdNameList['course'],
1, 1,
'External course id' 'External course id'
); );
// Create session extra field extra_external_session_id // Create session extra field extra_external_session_id
SessionManager::create_session_extra_field( SessionManager::create_session_extra_field(
$this->extraFieldIdNameList['session'], $this->extraFieldIdNameList['session'],
1, 1,
'External session id' '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, 'title' => $title,
'sender_id' => $teacherId, 'sender_id' => $teacherId,
'course_id' => $courseInfo['real_id'], '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; return 0;
} }
if ($errorFound == false) {
$this->logger->addInfo( $this->logger->addInfo(
"Ready to insert events" "Ready to insert events"
); );
@ -740,12 +751,62 @@ class ImportCsv
$content = null; $content = null;
$agenda = new Agenda(); $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) { 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']); $courseInfo = api_get_course_info_by_id($event['course_id']);
$agenda->set_course($courseInfo); $agenda->set_course($courseInfo);
$agenda->setType('course'); $agenda->setType('course');
$agenda->setSessionId($event['session_id']); $agenda->setSessionId($event['session_id']);
$agenda->setSenderId($event['sender_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( $eventId = $agenda->add_event(
$event['start'], $event['start'],
$event['end'], $event['end'],
@ -756,15 +817,15 @@ class ImportCsv
false //$addAsAnnouncement = false false //$addAsAnnouncement = false
); );
$extraFieldValue->save(array(
'field_value' => $externalEventId,
'field_id' => $extraFieldInfo['id']
));
$this->logger->addInfo( $this->logger->addInfo(
"Event added: #$eventId" "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) { 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; $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); $import = new ImportCsv($logger, $cronImportCSVConditions);
if (isset($_configuration['default_admin_user_id_for_cron'])) { if (isset($_configuration['default_admin_user_id_for_cron'])) {

@ -57,7 +57,7 @@ define('TABLE_MAIN_GRADEBOOK_LINK', 'gradebook_link');
define('TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY','gradebook_score_display'); define('TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY','gradebook_score_display');
define('TABLE_MAIN_GRADEBOOK_CERTIFICATE', 'gradebook_certificate'); define('TABLE_MAIN_GRADEBOOK_CERTIFICATE', 'gradebook_certificate');
//Profiling // Profiling
define('TABLE_MAIN_USER_FIELD', 'user_field'); define('TABLE_MAIN_USER_FIELD', 'user_field');
define('TABLE_MAIN_USER_FIELD_OPTIONS', 'user_field_options'); define('TABLE_MAIN_USER_FIELD_OPTIONS', 'user_field_options');
define('TABLE_MAIN_USER_FIELD_VALUES', 'user_field_values'); define('TABLE_MAIN_USER_FIELD_VALUES', 'user_field_values');
@ -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_OPTIONS', 'lp_field_options');
define('TABLE_MAIN_LP_FIELD_VALUES', 'lp_field_values'); 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 //User tags
define('TABLE_MAIN_TAG', 'tag'); define('TABLE_MAIN_TAG', 'tag');
define('TABLE_MAIN_USER_REL_TAG', 'user_rel_tag'); define('TABLE_MAIN_USER_REL_TAG', 'user_rel_tag');

@ -20,7 +20,6 @@ class ExtraField extends Model
'tms' 'tms'
); );
public $ops = array( public $ops = array(
'eq' => '=', //equal 'eq' => '=', //equal
'ne' => '<>', //not equal 'ne' => '<>', //not equal
@ -65,11 +64,18 @@ class ExtraField extends Model
{ {
$this->type = $type; $this->type = $type;
switch ($this->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': case 'course':
$this->table_field_options = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_OPTIONS); $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); $this->table_field_values = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
//Used for the model // Used for the model
$this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD); $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD);
$this->handler_id = 'course_code'; $this->handler_id = 'course_code';
$this->handlerEntityId = 'courseCode'; $this->handlerEntityId = 'courseCode';
@ -79,7 +85,7 @@ class ExtraField extends Model
$this->table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS); $this->table_field_options = Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES); $this->table_field_values = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
//Used for the model // Used for the model
$this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD); $this->table = Database::get_main_table(TABLE_MAIN_USER_FIELD);
$this->handler_id = 'user_id'; $this->handler_id = 'user_id';
$this->handlerEntityId = 'userId'; $this->handlerEntityId = 'userId';
@ -89,7 +95,7 @@ class ExtraField extends Model
//$this->table_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS); //$this->table_field_options = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_OPTIONS);
$this->table_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES); $this->table_field_values = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
//Used for the model // Used for the model
$this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD); $this->table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD);
$this->handler_id = 'session_id'; $this->handler_id = 'session_id';
$this->handlerEntityId = 'sessionId'; $this->handlerEntityId = 'sessionId';
@ -99,7 +105,7 @@ class ExtraField extends Model
$this->table_field_options = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_OPTIONS); $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); $this->table_field_values = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD_VALUES);
//Used for the model // Used for the model
$this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD); $this->table = Database::get_main_table(TABLE_MAIN_QUESTION_FIELD);
$this->handler_id = 'question_id'; $this->handler_id = 'question_id';
$this->handlerEntityId = 'questionId'; $this->handlerEntityId = 'questionId';
@ -124,14 +130,15 @@ class ExtraField extends Model
/** /**
* @return array * @return array
*/ */
static function getValidExtraFieldTypes() public static function getValidExtraFieldTypes()
{ {
return array( return array(
'user', 'user',
'course', 'course',
'session', 'session',
'question', 'question',
'lp' 'lp',
'calendar_event'
); );
} }
@ -174,14 +181,16 @@ class ExtraField extends Model
} }
/** /**
* @param $field_variable * @param string $field_variable
*
* @return array|bool * @return array|bool
*/ */
public function get_handler_field_info_by_field_variable($field_variable) public function get_handler_field_info_by_field_variable($field_variable)
{ {
$field_variable = Database::escape_string($field_variable); $field_variable = Database::escape_string($field_variable);
$sql_field = "SELECT * FROM {$this->table} WHERE field_variable = '$field_variable'"; $sql = "SELECT * FROM {$this->table}
$result = Database::query($sql_field); WHERE field_variable = '$field_variable'";
$result = Database::query($sql);
if (Database::num_rows($result)) { if (Database::num_rows($result)) {
$r_field = Database::fetch_array($result, 'ASSOC'); $r_field = Database::fetch_array($result, 'ASSOC');
@ -210,11 +219,12 @@ class ExtraField extends Model
/** /**
* @param $handler * @param $handler
*
* @return array * @return array
*/ */
public static function get_extra_fields_by_handler($handler) public static function get_extra_fields_by_handler($handler)
{ {
$types = array(); $types= array();
$types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText'); $types[self::FIELD_TYPE_TEXT] = get_lang('FieldTypeText');
$types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea'); $types[self::FIELD_TYPE_TEXTAREA] = get_lang('FieldTypeTextarea');
$types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio'); $types[self::FIELD_TYPE_RADIO] = get_lang('FieldTypeRadio');
@ -292,7 +302,9 @@ class ExtraField extends Model
if (!empty($fields) > 0) { if (!empty($fields) > 0) {
foreach ($fields as $field) { 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) { if ($field_value) {
$field_value = $field_value['field_value']; $field_value = $field_value['field_value'];
@ -372,12 +384,17 @@ class ExtraField extends Model
* Converts a string like this: * Converts a string like this:
* France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura; * France:Paris;Bretagne;Marseilles;Lyon|Belgique:Bruxelles;Namur;Liège;Bruges|Peru:Lima;Piura;
* into * 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 * @param string $string
* *
* @return array * @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 = explode('|', $string);
$options_parsed = array(); $options_parsed = array();
@ -398,7 +415,7 @@ class ExtraField extends Model
* @param array $options * @param array $options
* @return array * @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(); $options_parsed = array();
if (!empty($options)) { if (!empty($options)) {
@ -419,7 +436,7 @@ class ExtraField extends Model
* *
* @return string * @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; $string = null;
$options_parsed = self::extra_field_double_select_convert_array_to_ordered_array($options); $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) 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); $params = self::clean_parameters($params);
if ($session_field_info) { if ($fieldInfo) {
return $session_field_info['id']; return $fieldInfo['id'];
} else { } else {
if (!isset($params['tms'])) { if (!isset($params['tms'])) {
$params['tms'] = api_get_utc_datetime(); $params['tms'] = api_get_utc_datetime();
@ -495,6 +512,7 @@ class ExtraField extends Model
/** /**
* @param $params * @param $params
*
* @return bool|void * @return bool|void
*/ */
public function update($params) public function update($params)
@ -510,6 +528,7 @@ class ExtraField extends Model
/** /**
* @param $id * @param $id
*
* @return bool|void * @return bool|void
*/ */
public function delete($id) public function delete($id)

@ -18,7 +18,8 @@ class ExtraFieldValue extends Model
/** /**
* Formats the necessary elements for the given datatype * 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) * @return void (or false if unmanaged datatype)
* @assert (-1) === false * @assert (-1) === false
*/ */
@ -29,6 +30,12 @@ class ExtraFieldValue extends Model
$this->handler_id = $extra_field->handler_id; $this->handler_id = $extra_field->handler_id;
switch ($this->type) { 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': case 'course':
$this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES); $this->table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
$this->table_handler_field = Database::get_main_table(TABLE_MAIN_COURSE_FIELD); $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 * Save values in the *_field_values table
* @param array Structured array with the values to save * @param array $params Structured array with the values to save
* @param boolean Whether to show the insert query (passed to the parent save() method) * @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 * @result mixed The result sent from the parent method
* @assert (array()) === false * @assert (array()) === false
*/ */

Loading…
Cancel
Save