Skip to content
Permalink
Browse files

Add "preview" button before sending an announcement

To see the list of users and groups that will be sent BT#14824
  • Loading branch information...
jmontoyaa committed Sep 26, 2018
1 parent 91a0e34 commit 7b95d607a130a91a30f35f97946451e75632eaa2
@@ -9338,6 +9338,10 @@ ul.dropdown-menu.inner > li > a {

/* end document mp3 preview */

#announcement_preview_result .label {
display: inline-table;
}

/* Fix grid with one row and multiples columns. Works for .col-sm-6.col-md-4.col-lg-3 */
.row.clear-rows-6-4-3 {
}
@@ -156,8 +156,8 @@
break;
case 'view':
$interbreadcrumb[] = [
"url" => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
"name" => $nameTools,
'url' => api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq(),
'name' => $nameTools,
];
$nameTools = get_lang('View');
@@ -309,8 +309,16 @@
if ($sessionId != 0 && api_is_allowed_to_session_edit(false, true) == false) {
api_not_allowed();
}
$delete = false;
if (api_is_platform_admin()) {
$delete = true;
}
if (!api_is_session_general_coach() || api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $id)) {
$delete = true;
}
if ($delete) {
AnnouncementManager::delete_announcement($_course, $id);
Display::addFlash(Display::return_message(get_lang('AnnouncementDeleted')));
}
@@ -382,7 +390,7 @@
$url = api_get_self().'?action='.$action.'&id='.$id.'&'.api_get_cidreq();
$form = new FormValidator(
'f1',
'announcement',
'post',
$url,
null,
@@ -503,10 +511,47 @@
}
}
$ajaxUrl = api_get_path(WEB_AJAX_PATH).'announcement.ajax.php?'.api_get_cidreq().'&a=preview';
$form->addHtml("
<script>
$(document).on('ready', function () {
$('#announcement_preview').on('click', function() {
var users = [];
$('#users_to option').each(function() {
users.push($(this).val());
});
var form = $('#announcement').serialize();
$.ajax({
type: 'POST',
dataType: 'json',
url: '".$ajaxUrl."',
data: {users : JSON.stringify(users), form: form},
beforeSend: function() {
$('#announcement_preview_result').html('<i class=\"fa fa-spinner\"></i>');
$('#send_button').hide();
},
success: function(result) {
var resultToString = '';
$.each(result, function(index, value) {
resultToString += '&nbsp;' + value;
});
$('#announcement_preview_result').html('' +
'".addslashes(get_lang('AnnouncementWillBeSentTo'))."<br/>' + resultToString
);
$('#announcement_preview_result').show();
$('#send_button').show();
}
});
});
});
</script>
");
if (isset($defaults['users'])) {
foreach ($defaults['users'] as $value) {
$parts = explode(':', $value);
if (!isset($parts[1]) || empty($parts[1])) {
continue;
}
@@ -557,14 +602,28 @@
$config = api_get_configuration_value('announcements_hide_send_to_hrm_users');
if ($config === false) {
$form->addCheckBox('send_to_hrm_users', null, get_lang('SendAnnouncementCopyToDRH'));
$form->addCheckBox(
'send_to_hrm_users',
null,
get_lang('SendAnnouncementCopyToDRH'),
['id' => 'send_to_hrm_users']
);
}
$form->addCheckBox('send_me_a_copy_by_email', null, get_lang('SendAnnouncementCopyToMyself'));
$defaults['send_me_a_copy_by_email'] = true;
if ($showSubmitButton) {
$form->addLabel('',
Display::url(
get_lang('Preview'),
'javascript:void(0)',
['class' => 'btn btn-default', 'id' => 'announcement_preview']
).'<div id="announcement_preview_result" style="display:none"></div>'
);
$form->addHtml('<div id="send_button" style="display:none">');
$form->addButtonSave(get_lang('ButtonPublishAnnouncement'));
$form->addHtml('</div>');
}
$form->setDefaults($defaults);
@@ -23,6 +23,161 @@
}
switch ($action) {
case 'preview':
$allowToEdit = (
api_is_allowed_to_edit(false, true) ||
(api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
);
$drhHasAccessToSessionContent = api_drh_can_access_all_session_content();
if (!empty($sessionId) && $drhHasAccessToSessionContent) {
$allowToEdit = $allowToEdit || api_is_drh();
}
if ($allowToEdit === false && !empty($groupId)) {
$groupProperties = GroupManager:: get_group_properties($groupId);
// Check if user is tutor group
$isTutor = GroupManager::is_tutor_of_group(api_get_user_id(), $groupProperties, $courseId);
if ($isTutor) {
$allowToEdit = true;
}
}
if ($allowToEdit === false) {
exit;
}
$users = isset($_REQUEST['users']) ? json_decode($_REQUEST['users']) : '';
$formParams = [];
if (isset($_REQUEST['form'])) {
parse_str($_REQUEST['form'], $formParams);
}
$previewGroups = [];
$previewUsers = [];
$previewTotal = [];
if (empty($groupId)) {
if (empty($users) ||
(!empty($users) && isset($users[0]) && $users[0] == 'everyone')
) {
// All users in course session
if (empty($sessionId)) {
$students = CourseManager::get_user_list_from_course_code($courseInfo['code']);
} else {
$students = CourseManager::get_user_list_from_course_code($courseInfo['code'], $sessionId);
}
foreach ($students as $student) {
$previewUsers[] = $student['user_id'];
}
$groupList = GroupManager::get_group_list(null, $courseInfo, null, $sessionId);
foreach ($groupList as $group) {
$previewGroups[] = $group['iid'];
}
} else {
$send_to = CourseManager::separateUsersGroups($users);
// Storing the selected groups
if (is_array($send_to['groups']) &&
!empty($send_to['groups'])
) {
$counter = 1;
foreach ($send_to['groups'] as $group) {
$previewGroups[] = $group;
}
}
// Storing the selected users
if (is_array($send_to['users'])) {
$counter = 1;
foreach ($send_to['users'] as $user) {
$previewUsers[] = $user;
}
}
}
} else {
$send_to_users = CourseManager::separateUsersGroups($users);
$sentToAllGroup = false;
if (empty($send_to_users['groups']) && empty($send_to_users['users'])) {
$previewGroups[] = $groupId;
$sentToAllGroup = true;
}
if ($sentToAllGroup === false) {
if (!empty($send_to_users['groups'])) {
foreach ($send_to_users['groups'] as $group) {
$previewGroups[] = $group;
}
}
if (!empty($send_to_users['users'])) {
foreach ($send_to_users['users'] as $user) {
$previewUsers[] = $user;
}
}
}
}
if (isset($formParams['send_to_users_in_session']) && $formParams['send_to_users_in_session'] == 1) {
$sessionList = SessionManager::get_session_by_course(api_get_course_int_id());
if (!empty($sessionList)) {
foreach ($sessionList as $sessionInfo) {
$sessionId = $sessionInfo['id'];
$userList = CourseManager::get_user_list_from_course_code(
$courseCode,
$sessionId
);
if (!empty($userList)) {
foreach ($userList as $user) {
$previewUsers[] = $user;
}
}
}
}
}
if (isset($formParams['send_to_hrm_users']) && $formParams['send_to_hrm_users'] == 1) {
foreach ($previewUsers as $userId) {
$userInfo = api_get_user_info($userId);
$drhList = UserManager::getDrhListFromUser($userId);
if (!empty($drhList)) {
foreach ($drhList as $drhInfo) {
$previewUsers[] = $drhInfo['id'];
}
}
}
}
if (isset($formParams['send_me_a_copy_by_email']) && $formParams['send_me_a_copy_by_email'] == 1) {
$previewUsers[] = api_get_user_id();
}
$previewUserNames = [];
$previewGroupNames = [];
if (!empty($previewGroups)) {
$previewGroups = array_unique($previewGroups);
foreach ($previewGroups as $groupId) {
$groupInfo = GroupManager::get_group_properties($groupId);
$previewGroupNames[] = Display::label($groupInfo['name'],'info');
}
$previewTotal = $previewGroupNames;
}
if (!empty($previewUsers)) {
$previewUsers = array_unique($previewUsers);
foreach ($previewUsers as $userId) {
$userInfo = api_get_user_info($userId);
$previewUserNames[] = Display::label($userInfo['complete_name']);
}
$previewTotal = array_merge($previewTotal, $previewUserNames);
}
$previewTotal = array_map(function($value) { return ''.$value; }, $previewTotal);
echo json_encode($previewTotal);
break;
case 'delete_item':
if ($isAllowedToEdit) {
if (empty($_REQUEST['id'])) {

0 comments on commit 7b95d60

Please sign in to comment.
You can’t perform that action at this time.