Skip to content

Commit

Permalink
Adding calendar event extra fields see BT#7802
Browse files Browse the repository at this point in the history
  • Loading branch information
jmontoyaa committed Dec 17, 2014
1 parent 49f35de commit dc784cf
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 122 deletions.
70 changes: 56 additions & 14 deletions main/admin/extra_fields.php
Expand Up @@ -118,16 +118,17 @@
// 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'));
}
$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 '<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));
Expand All @@ -142,14 +143,15 @@
// 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);
$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 '<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));
Expand All @@ -159,10 +161,10 @@
case 'delete':
// Action handling: delete
//if ($check) {
$res = $obj->delete($_GET['id']);
if ($res) {
Display::display_confirmation_message(get_lang('ItemDeleted'));
}
$res = $obj->delete($_GET['id']);
if ($res) {
Display::display_confirmation_message(get_lang('ItemDeleted'));
}
//}
$obj->display();
break;
Expand Down Expand Up @@ -215,4 +217,44 @@
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)
);
*/
113 changes: 88 additions & 25 deletions main/cron/import_csv.php
Expand Up @@ -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;
Expand Down Expand Up @@ -199,18 +200,28 @@ private function prepareImport()
'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'
));
}

/**
Expand Down Expand Up @@ -625,7 +636,7 @@ private function importCoursesStatic($file)

/**
* @param string $file
* @param bool $moveFile
* @param bool $moveFile
*
* @return int
*/
Expand Down Expand Up @@ -719,7 +730,8 @@ private function importCalendarStatic($file, $moveFile = true)
'title' => $title,
'sender_id' => $teacherId,
'course_id' => $courseInfo['real_id'],
'session_id' => $sessionId
'session_id' => $sessionId,
$this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']
);
}
}
Expand All @@ -732,38 +744,87 @@ private function importCalendarStatic($file, $moveFile = true)
return 0;
}

if ($errorFound == false) {
$this->logger->addInfo(
"Ready to insert events"
);

$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(
"Ready to insert events"
"No calendar event extra field created: $extraFieldName"
);

$content = null;
$agenda = new Agenda();

foreach ($eventsToCreate as $event) {
$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']);
$eventId = $agenda->add_event(
$event['start'],
$event['end'],
false,
$event['title'],
$content,
array('everyone'), // send to
false //$addAsAnnouncement = false
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(
"Event added: #$eventId"
"No sender found: #".$event['sender_id']." Skipping ..."
);
continue;
}
} else {
echo 'There was an error check the logs in '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";

return 0;
$eventId = $agenda->add_event(
$event['start'],
$event['end'],
false,
$event['title'],
$content,
array('everyone'), // send to
false //$addAsAnnouncement = false
);

$extraFieldValue->save(array(
'field_value' => $externalEventId,
'field_id' => $extraFieldInfo['id']
));

$this->logger->addInfo(
"Event added: #$eventId"
);
}
}

Expand Down Expand Up @@ -1237,6 +1298,8 @@ private function dumpDatabaseTables()

$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'])) {
Expand Down
6 changes: 5 additions & 1 deletion main/inc/lib/database.constants.inc.php
Expand Up @@ -57,7 +57,7 @@
define('TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY','gradebook_score_display');
define('TABLE_MAIN_GRADEBOOK_CERTIFICATE', 'gradebook_certificate');

//Profiling
// Profiling
define('TABLE_MAIN_USER_FIELD', 'user_field');
define('TABLE_MAIN_USER_FIELD_OPTIONS', 'user_field_options');
define('TABLE_MAIN_USER_FIELD_VALUES', 'user_field_values');
Expand All @@ -66,6 +66,10 @@
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');
Expand Down

3 comments on commit dc784cf

@ywarnier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a good starting case for the "one table to fit them all" logic, where we group extra fields tables for courses, sessions, users and agenda events in the same 3 tables (with an item_type field)

@jmontoyaa
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think is a good idea to do this for this version of Chamilo, too much code to change and tables too migrate, maybe for version 2.

@ywarnier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I was just noting...

Please sign in to comment.