/** * @param string $file * @param bool $moveFile * * @return int */ private function importCalendarStatic($file, $moveFile = true) { $data = Import::csv_to_array($file); if (!empty($data)) { $this->logger->addInfo(count($data) . " records found."); $eventsToCreate = array(); $errorFound = false; foreach ($data as $row) { $sessionId = null; $externalSessionId = null; if (isset($row['external_sessionID'])) { $externalSessionId = $row['external_sessionID']; $sessionId = SessionManager::get_session_id_from_original_id($externalSessionId, $this->extraFieldIdNameList['session']); } $courseCode = null; if (isset($row['coursecode'])) { $courseCode = $row['coursecode']; } $courseInfo = api_get_course_info($courseCode); if (empty($courseInfo)) { $this->logger->addInfo("Course '{$courseCode}' does not exists"); } if (empty($sessionId)) { $this->logger->addInfo("external_sessionID: " . $externalSessionId . " does not exists."); } $teacherId = null; if (!empty($sessionId) && !empty($courseInfo)) { $courseIncluded = SessionManager::relation_session_course_exist($sessionId, $courseInfo['code']); if ($courseIncluded == false) { $this->logger->addInfo("Course '{$courseCode}' is not included in session: {$sessionId}"); $errorFound = true; } else { $teachers = CourseManager::get_coach_list_from_course_code($courseInfo['code'], $sessionId); // Getting first teacher. if (!empty($teachers)) { $teacher = current($teachers); $teacherId = $teacher['user_id']; } else { $sessionInfo = api_get_session_info($sessionId); $teacherId = $sessionInfo['id_coach']; } } } else { $errorFound = true; } if (empty($teacherId)) { $errorFound = true; $this->logger->addInfo("No teacher found in course code : '{$courseCode}' and session: '{$sessionId}'"); } $date = $row['date']; $startTime = $row['time_start']; $endTime = $row['time_end']; $title = $row['title']; $comment = $row['comment']; $color = isset($row['color']) ? $row['color'] : ''; $startDateYear = substr($date, 0, 4); $startDateMonth = substr($date, 4, 2); $startDateDay = substr($date, 6, 8); $startDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $startTime . ":00"; $endDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $endTime . ":00"; if (!api_is_valid_date($startDate) || !api_is_valid_date($endDate)) { $this->logger->addInfo("Verify your dates: '{$startDate}' : '{$endDate}' "); $errorFound = true; } // If old events do nothing. /*if (api_strtotime($startDate) < time()) { continue; }*/ if ($errorFound == false) { $eventsToCreate[] = array('start' => $startDate, 'end' => $endDate, 'title' => $title, 'sender_id' => $teacherId, 'course_id' => $courseInfo['real_id'], 'session_id' => $sessionId, 'comment' => $comment, 'color' => $color, $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']); } } if (empty($eventsToCreate)) { $this->logger->addInfo("No events to add"); return 0; } $this->logger->addInfo("Ready to insert events"); $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) { $update = false; $item = null; if (!isset($event[$extraFieldName])) { $this->logger->addInfo("No external_calendar_itemID found. Skipping ..."); continue; } else { $externalEventId = $event[$extraFieldName]; if (empty($externalEventId)) { $this->logger->addInfo("external_calendar_itemID was set but empty. Skipping ..."); continue; } $items = $extraFieldValue->get_item_id_from_field_variable_and_field_value($extraFieldName, $externalEventId, false, false, true); $item = null; foreach ($items as $tempItem) { if ($tempItem['c_id'] == $event['course_id']) { $item = $tempItem; } } if (!empty($item)) { $this->logger->addInfo("Event #{$externalEventId} was already added. Updating ..."); $update = true; //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']); $agenda->setIsAllowedToEdit(true); $eventComment = $event['comment']; $color = $event['color']; // To use the event comment you need // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT; // add in configuration.php allow_agenda_event_comment = true 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; } $content = ''; if ($update && isset($item['calendar_event_id'])) { //the event already exists, just update $eventId = $agenda->edit_event($item['calendar_event_id'], $event['start'], $event['end'], false, $event['title'], $content, array('everyone'), array(), null, $eventComment, $color); if ($eventId !== false) { $this->logger->addInfo("Event updated: #{$eventId}"); } else { $this->logger->addInfo("Error while updating event."); } } else { // New event. Create it. $eventId = $agenda->add_event($event['start'], $event['end'], false, $event['title'], $content, array('everyone'), false, null, array(), null, $eventComment, $color); if (!empty($eventId)) { $extraFieldValue->is_course_model = true; $extraFieldValue->save(array('field_value' => $externalEventId, 'field_id' => $extraFieldInfo['id'], 'calendar_event_id' => $eventId, 'c_id' => $event['course_id'])); $this->logger->addInfo("Event added: #{$eventId}"); } else { $this->logger->addInfo("Error while creating event."); } } } } if ($moveFile) { $this->moveFile($file); } }
/** * @param string $file * @param bool $moveFile * * @return int */ private function importCalendarStatic($file, $moveFile = true) { $data = Import::csvToArray($file); if ($this->getDumpValues()) { // Remove all calendar items $truncateTables = array(Database::get_course_table(TABLE_AGENDA), Database::get_course_table(TABLE_AGENDA_ATTACHMENT), Database::get_course_table(TABLE_AGENDA_REPEAT), Database::get_course_table(TABLE_AGENDA_REPEAT_NOT), Database::get_main_table(TABLE_PERSONAL_AGENDA), Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT), Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT)); foreach ($truncateTables as $table) { $sql = "TRUNCATE {$table}"; Database::query($sql); } $table = Database::get_course_table(TABLE_ITEM_PROPERTY); $sql = "DELETE FROM {$table} WHERE tool = 'calendar_event'"; Database::query($sql); } if (!empty($data)) { $this->logger->addInfo(count($data) . " records found."); $eventsToCreate = array(); $errorFound = false; foreach ($data as $row) { $sessionId = null; $externalSessionId = null; if (isset($row['external_sessionID'])) { $externalSessionId = $row['external_sessionID']; $sessionId = SessionManager::getSessionIdFromOriginalId($externalSessionId, $this->extraFieldIdNameList['session']); } $courseCode = null; if (isset($row['coursecode'])) { $courseCode = $row['coursecode']; } $courseInfo = api_get_course_info($courseCode); if (empty($courseInfo)) { $this->logger->addInfo("Course '{$courseCode}' does not exists"); } if (empty($sessionId)) { $this->logger->addInfo("external_sessionID: " . $externalSessionId . " does not exists."); } $teacherId = null; if (!empty($sessionId) && !empty($courseInfo)) { $courseIncluded = SessionManager::relation_session_course_exist($sessionId, $courseInfo['real_id']); if ($courseIncluded == false) { $this->logger->addInfo("Course '{$courseCode}' is not included in session: {$sessionId}"); $errorFound = true; } else { $teachers = CourseManager::get_coach_list_from_course_code($courseInfo['code'], $sessionId); // Getting first teacher. if (!empty($teachers)) { $teacher = current($teachers); $teacherId = $teacher['user_id']; } else { $sessionInfo = api_get_session_info($sessionId); $teacherId = $sessionInfo['id_coach']; } } } else { $errorFound = true; } if (empty($teacherId)) { $errorFound = true; $this->logger->addInfo("No teacher found in course code : '{$courseCode}' and session: '{$sessionId}'"); } $date = $row['date']; $startTime = $row['time_start']; $endTime = $row['time_end']; $title = $row['title']; $startDateYear = substr($date, 0, 4); $startDateMonth = substr($date, 4, 2); $startDateDay = substr($date, 6, 8); $startDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $startTime . ":00"; $endDate = $startDateYear . '-' . $startDateMonth . '-' . $startDateDay . ' ' . $endTime . ":00"; if (!api_is_valid_date($startDate) or !api_is_valid_date($endDate)) { $this->logger->addInfo("Verify your dates: '{$startDate}' : '{$endDate}' "); $errorFound = true; } if ($errorFound == false) { $eventsToCreate[] = array('start' => $startDate, 'end' => $endDate, 'title' => $title, 'sender_id' => $teacherId, 'course_id' => $courseInfo['real_id'], 'session_id' => $sessionId, $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']); } } if (empty($eventsToCreate)) { $this->logger->addInfo("No events to add"); return 0; } $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("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']); $agenda->setIsAllowedToEdit(true); $eventComment = $event['comment']; 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->addEvent($event['start'], $event['end'], false, $event['title'], $content, array('everyone'), false, null, array(), null, $eventComment); if (!empty($eventId)) { $extraFieldValue->save(array('value' => $externalEventId, 'field_id' => $extraFieldInfo['id'], 'item_id' => $eventId)); $this->logger->addInfo("Event added: #{$eventId}"); } else { $this->logger->addInfo("Error while creating event."); } } } if ($moveFile) { $this->moveFile($file); } }