diff --git a/main/inc/lib/AnnouncementManager.php b/main/inc/lib/AnnouncementManager.php index c0e32df1ac..c0ff82a776 100755 --- a/main/inc/lib/AnnouncementManager.php +++ b/main/inc/lib/AnnouncementManager.php @@ -28,7 +28,7 @@ class AnnouncementManager /** * @return array */ - public static function getTags() + public static function getTags(array $excluded = []) { $tags = [ '((user_name))', @@ -58,6 +58,10 @@ class AnnouncementManager $tags[] = '((general_coach_email))'; } + if ($excluded) { + return array_diff($tags, $excluded); + } + return $tags; } @@ -72,11 +76,11 @@ class AnnouncementManager public static function parseContent( $userId, $content, - $courseCode, + $courseCode = '', $sessionId = 0 ) { $readerInfo = api_get_user_info($userId, false, false, true, true, false, true); - $courseInfo = api_get_course_info($courseCode); + $courseInfo = $courseCode ? api_get_course_info($courseCode) : []; $teacherList = ''; if ($courseInfo) { $teacherList = CourseManager::getTeacherListFromCourseCodeToString($courseInfo['code']); @@ -86,10 +90,9 @@ class AnnouncementManager $coaches = ''; if (!empty($sessionId)) { $sessionInfo = api_get_session_info($sessionId); - $coaches = CourseManager::get_coachs_from_course_to_string( - $sessionId, - $courseInfo['real_id'] - ); + $coaches = $courseInfo + ? CourseManager::get_coachs_from_course_to_string($sessionId, $courseInfo['real_id']) + : ''; $generalCoach = api_get_user_info($sessionInfo['id_coach']); $generalCoachName = $generalCoach['complete_name']; @@ -114,7 +117,7 @@ class AnnouncementManager $data['user_picture'] = UserManager::getUserPicture($userId, USER_IMAGE_SIZE_ORIGINAL); $data['course_title'] = $courseInfo['name'] ?? ''; - $courseLink = api_get_course_url($courseCode, $sessionId); + $courseLink = $courseCode ? api_get_course_url($courseCode, $sessionId) : ''; $data['course_link'] = Display::url($courseLink, $courseLink); $data['teachers'] = $teacherList; @@ -152,7 +155,7 @@ class AnnouncementManager $tags = self::getTags(); foreach ($tags as $tag) { $simpleTag = str_replace(['((', '))'], '', $tag); - $value = isset($data[$simpleTag]) ? $data[$simpleTag] : ''; + $value = $data[$simpleTag] ?? ''; $content = str_replace($tag, $value, $content); } diff --git a/main/session/importCourseEventInSessionExample.csv b/main/session/importCourseEventInSessionExample.csv new file mode 100644 index 0000000000..21c5d86d1e --- /dev/null +++ b/main/session/importCourseEventInSessionExample.csv @@ -0,0 +1,2 @@ +StartDate;EndDate +YYYY-MM-DD HH:ii:ss;YYYY-MM-DD HH:ii:ss diff --git a/main/session/import_course_agenda_reminders.php b/main/session/import_course_agenda_reminders.php new file mode 100644 index 0000000000..8403600c0e --- /dev/null +++ b/main/session/import_course_agenda_reminders.php @@ -0,0 +1,190 @@ +'.get_lang('Tags').'' + .'
'.implode("\n", $tags).'
'; + +$fileHelpText = get_lang('ImportCSVFileLocation').'
' + .Display::url( + get_lang('ExampleCSVFile'), + 'importCourseEventInSessionExample.csv', + [ + 'target' => '_blank', + 'download' => 'importCourseEventInSessionExample.csv', + ] + ) + .'
StartDate;EndDate
xxx;YYYY-MM-DD HH:ii:ss;YYYY-MM-DD HH:ii:ss
'; + +$form = new FormValidator('agenda_reminders', 'post', $selfUrl); +$form->addHeader(get_lang('CsvImport')); +$form->addFile( + 'events_file', + [get_lang('ImportAsCSV'), $fileHelpText], + ['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('
'); + $form->addButton('add_notification', get_lang('AddNotification'), 'bell-o')->setType('button'); +} + +$form->addHtml('
'); +$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 = []; + + $studentList = SessionManager::get_users_by_session($sessionId, Session::STUDENT); + + foreach ($csvEvents as $csvEvent) { + $hashDate = base64_encode($csvEvent['StartDate'].'||'.$csvEvent['EndDate']); + + foreach ($studentList as $studentInfo) { + $grouppedData[$hashDate][] = $studentInfo['user_id']; + } + } + + foreach ($grouppedData 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'], '', $sessionId); + $content = AnnouncementManager::parseContent($userId, $values['description'], '', $sessionId); + + $title = str_replace(['((date_start))', '((date_end))'], [$strDateStart, $strDateEnd], $title); + $content = str_replace(['((date_start))', '((date_end))'], [$strDateStart, $strDateEnd], $content); + + $eventId = Database::insert( + $tblPersonalAgenda, + [ + 'user' => $userId, + 'title' => $title, + 'text' => $content, + 'date' => $dateStart, + 'enddate' => $dateEnd, + 'all_day' => 0, + 'color' => '', + ] + ); + + if ($isAgendaRemindersEnabled) { + foreach ($reminders as $reminder) { + $agenda->addReminder($eventId, $reminder[0], $reminder[1]); + } + } + } + } + + Display::addFlash( + Display::return_message(get_lang('FileImported'), 'success') + ); + + header("Location: $selfUrl"); + exit; +} + +$form->setDefaults( + [ + 'title' => get_lang('ImportSessionAgendaReminderTitleDefault'), + 'description' => get_lang('ImportSessionAgendaReminderDescriptionDefault'), + ] +); + +$htmlHeadXtra[] = ''; + +$pageTitle = get_lang('ImportCourseEvents'); + +$interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')]; +$interbreadcrumb[] = [ + 'url' => 'session_list.php', + 'name' => get_lang('SessionList'), +]; +$interbreadcrumb[] = [ + 'url' => $sessionUrl, + 'name' => get_lang('SessionOverview'), +]; + +$template = new Template($pageTitle); +$template->assign('header', $pageTitle); +$template->assign('content', $form->returnForm()); +$template->display_one_col_template(); diff --git a/main/template/default/session/resume_session.tpl b/main/template/default/session/resume_session.tpl index e8b365fe25..d3cba3547b 100644 --- a/main/template/default/session/resume_session.tpl +++ b/main/template/default/session/resume_session.tpl @@ -109,6 +109,16 @@ {% endif %} + + {% if true == 'agenda_reminders'|api_get_configuration_value %} + + + + {{ 'ImportCourseEvents'|get_lang }} + + + + {% endif %} {{ course_list }}