Skip to content

Commit adde7f5

Browse files
committed
Pause training: Block emails not chamilo inbox msgs BT#16298
1 parent e959758 commit adde7f5

File tree

2 files changed

+97
-94
lines changed

2 files changed

+97
-94
lines changed

main/inc/lib/message.lib.php

Lines changed: 93 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -481,50 +481,56 @@ public static function send_message(
481481
$topic_id = (int) $topic_id;
482482
$status = empty($status) ? MESSAGE_STATUS_UNREAD : (int) $status;
483483

484+
$sendEmail = true;
484485
if (!empty($receiver_user_id)) {
485-
// Check if receipent is allow to recieve a message
486486
$receiverUserInfo = api_get_user_info($receiver_user_id);
487+
if (empty($receiverUserInfo)) {
488+
return false;
489+
}
487490

488491
// Disabling messages for inactive users.
489492
if (0 == $receiverUserInfo['active']) {
490493
return false;
491494
}
492495

493496
// Disabling messages depending the pausetraining plugin.
494-
$allowPauseFormation = api_get_plugin_setting('pausetraining', 'tool_enable') === 'true' &&
497+
$allowPauseFormation =
498+
api_get_plugin_setting('pausetraining', 'tool_enable') === 'true' &&
495499
api_get_plugin_setting('pausetraining', 'allow_users_to_edit_pause_formation') === 'true';
496500

497501
if ($allowPauseFormation) {
498-
if (!empty($receiverUserInfo)) {
499-
$extraFieldValue = new ExtraFieldValue('user');
500-
$allowNotifications = $extraFieldValue->get_values_by_handler_and_field_variable(
502+
$extraFieldValue = new ExtraFieldValue('user');
503+
$allowEmailNotifications = $extraFieldValue->get_values_by_handler_and_field_variable(
504+
$receiverUserInfo['user_id'],
505+
'allow_notifications'
506+
);
507+
508+
if (empty($allowEmailNotifications)) {
509+
$sendEmail = false;
510+
}
511+
512+
if (!empty($allowEmailNotifications) &&
513+
isset($allowEmailNotifications['value']) && 1 === (int) $allowEmailNotifications['value']
514+
) {
515+
$startDate = $extraFieldValue->get_values_by_handler_and_field_variable(
516+
$receiverUserInfo['user_id'],
517+
'start_pause_date'
518+
);
519+
$endDate = $extraFieldValue->get_values_by_handler_and_field_variable(
501520
$receiverUserInfo['user_id'],
502-
'allow_notifications'
521+
'end_pause_date'
503522
);
504523

505-
if (!empty($allowNotifications) &&
506-
isset($allowNotifications['value']) && 0 === (int) $allowNotifications['value']
524+
if (
525+
!empty($startDate) && isset($startDate['value']) && !empty($startDate['value']) &&
526+
!empty($endDate) && isset($endDate['value']) && !empty($endDate['value'])
507527
) {
508-
$startDate = $extraFieldValue->get_values_by_handler_and_field_variable(
509-
$receiverUserInfo['user_id'],
510-
'start_pause_date'
511-
);
512-
$endDate = $extraFieldValue->get_values_by_handler_and_field_variable(
513-
$receiverUserInfo['user_id'],
514-
'end_pause_date'
515-
);
528+
$now = time();
529+
$start = api_strtotime($startDate['value']);
530+
$end = api_strtotime($startDate['value']);
516531

517-
if (
518-
!empty($startDate) && isset($startDate['value']) && !empty($startDate['value']) &&
519-
!empty($endDate) && isset($endDate['value']) && !empty($endDate['value'])
520-
) {
521-
$now = time();
522-
$start = api_strtotime($startDate['value']);
523-
$end = api_strtotime($startDate['value']);
524-
525-
if ($now > $start && $now < $end) {
526-
return false;
527-
}
532+
if ($now > $start && $now < $end) {
533+
$sendEmail = false;
528534
}
529535
}
530536
}
@@ -587,7 +593,7 @@ public static function send_message(
587593
);
588594

589595
return false;
590-
} elseif ($totalFileSize > intval(api_get_setting('message_max_upload_filesize'))) {
596+
} elseif ($totalFileSize > (int) api_get_setting('message_max_upload_filesize')) {
591597
$warning = sprintf(
592598
get_lang('FilesSizeExceedsX'),
593599
format_file_size(api_get_setting('message_max_upload_filesize'))
@@ -693,69 +699,70 @@ public static function send_message(
693699
}
694700
}
695701

696-
// Load user settings.
697-
$notification = new Notification();
698-
$sender_info = api_get_user_info($user_sender_id);
699-
700-
// add file attachment additional attributes
701-
$attachmentAddedByMail = [];
702-
foreach ($attachmentList as $attachment) {
703-
$attachmentAddedByMail[] = [
704-
'path' => $attachment['tmp_name'],
705-
'filename' => $attachment['name'],
706-
];
707-
}
702+
if ($sendEmail) {
703+
$notification = new Notification();
704+
$sender_info = api_get_user_info($user_sender_id);
708705

709-
if (empty($group_id)) {
710-
$type = Notification::NOTIFICATION_TYPE_MESSAGE;
711-
if ($directMessage) {
712-
$type = Notification::NOTIFICATION_TYPE_DIRECT_MESSAGE;
706+
// add file attachment additional attributes
707+
$attachmentAddedByMail = [];
708+
foreach ($attachmentList as $attachment) {
709+
$attachmentAddedByMail[] = [
710+
'path' => $attachment['tmp_name'],
711+
'filename' => $attachment['name'],
712+
];
713713
}
714-
$notification->saveNotification(
715-
$messageId,
716-
$type,
717-
[$receiver_user_id],
718-
$subject,
719-
$content,
720-
$sender_info,
721-
$attachmentAddedByMail,
722-
$smsParameters,
723-
$forceTitleWhenSendingEmail
724-
);
725-
} else {
726-
$usergroup = new UserGroup();
727-
$group_info = $usergroup->get($group_id);
728-
$group_info['topic_id'] = $topic_id;
729-
$group_info['msg_id'] = $messageId;
730714

731-
$user_list = $usergroup->get_users_by_group(
732-
$group_id,
733-
false,
734-
[],
735-
0,
736-
1000
737-
);
715+
if (empty($group_id)) {
716+
$type = Notification::NOTIFICATION_TYPE_MESSAGE;
717+
if ($directMessage) {
718+
$type = Notification::NOTIFICATION_TYPE_DIRECT_MESSAGE;
719+
}
720+
$notification->saveNotification(
721+
$messageId,
722+
$type,
723+
[$receiver_user_id],
724+
$subject,
725+
$content,
726+
$sender_info,
727+
$attachmentAddedByMail,
728+
$smsParameters,
729+
$forceTitleWhenSendingEmail
730+
);
731+
} else {
732+
$usergroup = new UserGroup();
733+
$group_info = $usergroup->get($group_id);
734+
$group_info['topic_id'] = $topic_id;
735+
$group_info['msg_id'] = $messageId;
738736

739-
// Adding more sense to the message group
740-
$subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']);
741-
$new_user_list = [];
742-
foreach ($user_list as $user_data) {
743-
$new_user_list[] = $user_data['id'];
737+
$user_list = $usergroup->get_users_by_group(
738+
$group_id,
739+
false,
740+
[],
741+
0,
742+
1000
743+
);
744+
745+
// Adding more sense to the message group
746+
$subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']);
747+
$new_user_list = [];
748+
foreach ($user_list as $user_data) {
749+
$new_user_list[] = $user_data['id'];
750+
}
751+
$group_info = [
752+
'group_info' => $group_info,
753+
'user_info' => $sender_info,
754+
];
755+
$notification->saveNotification(
756+
$messageId,
757+
Notification::NOTIFICATION_TYPE_GROUP,
758+
$new_user_list,
759+
$subject,
760+
$content,
761+
$group_info,
762+
$attachmentAddedByMail,
763+
$smsParameters
764+
);
744765
}
745-
$group_info = [
746-
'group_info' => $group_info,
747-
'user_info' => $sender_info,
748-
];
749-
$notification->saveNotification(
750-
$messageId,
751-
Notification::NOTIFICATION_TYPE_GROUP,
752-
$new_user_list,
753-
$subject,
754-
$content,
755-
$group_info,
756-
$attachmentAddedByMail,
757-
$smsParameters
758-
);
759766
}
760767

761768
return $messageId;

plugin/pausetraining/PauseTraining.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,10 @@ public function runCron()
166166
continue;
167167
}
168168

169-
// Check if user wants a notification or not
170-
$notification = $extraFieldValue->get_values_by_handler_and_field_variable($userId, 'allow_notifications');
171-
if ($notification && isset($notification['value']) && 1 === (int) $notification['value']) {
172-
$template->assign('days', $day);
173-
$template->assign('user', $userInfo);
174-
$content = $template->fetch('pausetraining/view/notification_content.tpl');
175-
MessageManager::send_message_simple($userId, $title, $content, $senderId);
176-
}
169+
$template->assign('days', $day);
170+
$template->assign('user', $userInfo);
171+
$content = $template->fetch('pausetraining/view/notification_content.tpl');
172+
MessageManager::send_message_simple($userId, $title, $content, $senderId);
177173
}
178174
}
179175
}

0 commit comments

Comments
 (0)