Agenda: Allow import course events in CSV - refs BT#20356

Author: @AngelFQC
pull/4505/head
Angel Fernando Quiroz Campos 3 years ago committed by GitHub
parent ba8d1ce3fb
commit 949f8cce6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 169
      main/admin/import_course_agenda_reminders.php
  2. 9
      main/admin/index.php
  3. 18
      main/inc/ajax/agenda.ajax.php
  4. 3
      main/inc/lib/AnnouncementManager.php

@ -0,0 +1,169 @@
<?php
/* For licensing terms, see /license.txt */
require_once __DIR__.'/../inc/global.inc.php';
api_protect_admin_script();
$isAgendaRemindersEnabled = api_get_configuration_value('agenda_reminders');
if (!$isAgendaRemindersEnabled) {
api_not_allowed(true);
}
$tblPersonalAgenda = Database::get_main_table(TABLE_PERSONAL_AGENDA);
$tags = AnnouncementManager::getTags();
$tags[] = '((date_start))';
$tags[] = '((date_end))';
$tagsHelp = '<strong>'.get_lang('Tags').'</strong>'
.'<pre>'.implode("\n", $tags).'</pre>';
$form = new FormValidator('agenda_reminders');
$form->addHeader(get_lang('CsvImport'));
$form->addFile(
'events_file',
[get_lang('ImportAsCSV'), get_lang('ImportCSVFileLocation')],
['accept' => 'text/csv']
);
$form->addRule('events_file', get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('events_file', get_lang('InvalidExtension'), 'filetype', ['csv']);
$form->addHeader(get_lang('AddEventInCourseCalendar'));
$form->addText(
'title',
[get_lang('ItemTitle'), get_lang('TagsCanBeUsed')],
true,
['cols-size' => [2, 7, 3]]
);
$form->applyFilter('title', 'trim');
$form->addHtmlEditor(
'description',
[get_lang('Description'), null, $tagsHelp],
true,
false,
['ToolbarSet' => 'Minimal', 'cols-size' => [2, 7, 3]]
);
//$form->applyFilter('description', 'html_filter_teacher');
if ($isAgendaRemindersEnabled) {
$form->addHeader(get_lang('NotifyBeforeTheEventStarts'));
$form->addHtml('<div id="notification_list"></div>');
$form->addButton('add_notification', get_lang('AddNotification'), 'bell-o')->setType('button');
}
$form->addHtml('<hr>');
$form->addButtonImport(get_lang('Import'));
if ($form->validate()) {
$values = $form->exportValues();
$uploadInfo = pathinfo($_FILES['events_file']['name']);
$notificationCount = $_POST['notification_count'] ?? [];
$notificationPeriod = $_POST['notification_period'] ?? [];
$reminders = $notificationCount ? array_map(null, $notificationCount, $notificationPeriod) : [];
if ('csv' !== $uploadInfo['extension']) {
Display::addFlash(
Display::return_message(get_lang('NotCSV'), 'error')
);
header('Location: '.api_get_self());
exit;
}
$csvEvents = Import::csvToArray($_FILES['events_file']['tmp_name']);
if (empty($csvEvents)) {
exit;
}
$agenda = new Agenda('personal');
$grouppedData = [];
foreach ($csvEvents as $csvEvent) {
$hashDate = base64_encode($csvEvent['StartDate'].'||'.$csvEvent['EndDate']);
$courseInfo = api_get_course_info($csvEvent['CourseCode']);
if (!$courseInfo) {
continue;
}
$userInfo = api_get_user_info_from_username($csvEvent['UserName']);
if (!$userInfo) {
continue;
}
$grouppedData[$courseInfo['code']][$hashDate][] = $userInfo['id'];
}
foreach ($grouppedData as $courseCode => $eventInfo) {
foreach ($eventInfo as $hashDate => $userIdList) {
$dateRange = base64_decode($hashDate);
list($dateStart, $dateEnd) = explode('||', $dateRange);
$dateStart = api_get_utc_datetime($dateStart);
$dateEnd = api_get_utc_datetime($dateEnd);
$strDateStart = api_format_date($dateStart, DATE_TIME_FORMAT_LONG_24H);
$strDateEnd = api_format_date($dateEnd, DATE_TIME_FORMAT_LONG_24H);
foreach ($userIdList as $userId) {
$title = AnnouncementManager::parseContent($userId, $values['title'], $courseCode);
$content = AnnouncementManager::parseContent($userId, $values['description'], $courseCode);
$title = str_replace(['((date_start))', '((date_end))'], [$strDateStart, $strDateEnd], $title);
$content = str_replace(['((date_start))', '((date_end))'], [$strDateStart, $strDateEnd], $content);
$attributes = [
'user' => $userId,
'title' => $title,
'text' => $content,
'date' => $dateStart,
'enddate' => $dateEnd,
'all_day' => 0,
'color' => '',
];
$eventId = Database::insert($tblPersonalAgenda, $attributes);
if ($isAgendaRemindersEnabled) {
foreach ($reminders as $reminder) {
$agenda->addReminder($eventId, $reminder[0], $reminder[1]);
}
}
}
}
}
Display::addFlash(
Display::return_message(get_lang('FileImported'), 'success')
);
header('Location: '.api_get_self());
exit;
}
$form->setDefaults(
[
'title' => get_lang('ImportCourseAgendaReminderTitleDefault'),
'description' => get_lang('ImportCourseAgendaReminderDescriptionDefault'),
]
);
$htmlHeadXtra[] = '<script>$(function () {'
.Agenda::getJsForReminders('#agenda_reminders_add_notification')
.'});</script>';
$pageTitle = get_lang('ImportCourseEvents');
$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
$template = new Template($pageTitle);
$template->assign('header', $pageTitle);
$template->assign('content', $form->returnForm());
$template->display_one_col_template();

@ -396,6 +396,15 @@ if (api_is_platform_admin()) {
'url' => api_get_path(WEB_CODE_PATH).'calendar/agenda_js.php?type=admin',
'label' => get_lang('GlobalAgenda'),
];
if (true === api_get_configuration_value('agenda_reminders')) {
$items[] = [
'class' => 'item-agenda-reminders',
'url' => api_get_path(WEB_CODE_PATH).'admin/import_course_agenda_reminders.php',
'label' => get_lang('AgendaReminders'),
];
}
$items[] = [
'class' => 'item-homepage',
'url' => 'configure_homepage.php',

@ -13,7 +13,7 @@ if ($type === 'personal') {
require_once __DIR__.'/../global.inc.php';
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
$action = $_REQUEST['a'] ?? null;
$group_id = api_get_group_id();
if ($type === 'course') {
@ -40,11 +40,11 @@ switch ($action) {
if (false === Security::check_token('get')) {
exit;
}
$add_as_announcement = isset($_REQUEST['add_as_annonuncement']) ? $_REQUEST['add_as_annonuncement'] : null;
$title = isset($_REQUEST['title']) ? $_REQUEST['title'] : null;
$content = isset($_REQUEST['content']) ? $_REQUEST['content'] : null;
$comment = isset($_REQUEST['comment']) ? $_REQUEST['comment'] : null;
$userToSend = isset($_REQUEST['users_to_send']) ? $_REQUEST['users_to_send'] : [];
$add_as_announcement = $_REQUEST['add_as_annonuncement'] ?? null;
$title = $_REQUEST['title'] ?? null;
$content = $_REQUEST['content'] ?? null;
$comment = $_REQUEST['comment'] ?? null;
$userToSend = $_REQUEST['users_to_send'] ?? [];
$inviteesList = $_REQUEST['invitees'] ?? [];
$isCollective = isset($_REQUEST['collective']);
$notificationCount = $_REQUEST['notification_count'] ?? [];
@ -103,7 +103,7 @@ switch ($action) {
}
$id_list = explode('_', $_REQUEST['id']);
$id = $id_list[1];
$deleteAllEventsFromSerie = isset($_REQUEST['delete_all_events']) ? true : false;
$deleteAllEventsFromSerie = isset($_REQUEST['delete_all_events']);
$agenda->deleteEvent($id, $deleteAllEventsFromSerie);
break;
case 'resize_event':
@ -132,8 +132,8 @@ switch ($action) {
$agenda->move_event($id, $minute_delta, $allDay);
break;
case 'get_events':
$filter = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : null;
$sessionId = isset($_REQUEST['session_id']) ? $_REQUEST['session_id'] : null;
$filter = $_REQUEST['user_id'] ?? null;
$sessionId = $_REQUEST['session_id'] ?? null;
$result = $agenda->parseAgendaFilter($filter);
$groupId = current($result['groups']);

@ -36,6 +36,7 @@ class AnnouncementManager
'((user_firstname))',
'((user_lastname))',
'((user_picture))',
'((user_complete_name))',
'((user_official_code))',
'((course_title))',
'((course_link))',
@ -99,6 +100,7 @@ class AnnouncementManager
$data['user_name'] = '';
$data['user_firstname'] = '';
$data['user_lastname'] = '';
$data['user_complete_name'] = '';
$data['user_official_code'] = '';
$data['user_email'] = '';
if (!empty($readerInfo)) {
@ -107,6 +109,7 @@ class AnnouncementManager
$data['user_firstname'] = $readerInfo['firstname'];
$data['user_lastname'] = $readerInfo['lastname'];
$data['user_official_code'] = $readerInfo['official_code'];
$data['user_complete_name'] = $readerInfo['complete_name'];
}
$data['user_picture'] = UserManager::getUserPicture($userId, USER_IMAGE_SIZE_ORIGINAL);

Loading…
Cancel
Save