Skip to content

Commit

Permalink
Pausetraining: Update send mail conditions BT#16298
Browse files Browse the repository at this point in the history
Format code + remove unused function
  • Loading branch information
jmontoyaa committed Jul 22, 2020
1 parent 07b8e96 commit 4e0e2be
Showing 1 changed file with 25 additions and 60 deletions.
85 changes: 25 additions & 60 deletions main/inc/lib/message.lib.php
Expand Up @@ -128,7 +128,7 @@ public static function getWhereConditions($extraParams)
*
* @param int $from
* @param int $numberOfItems
* @param string $column
* @param int $column
* @param string $direction
* @param array $extraParams
*
Expand Down Expand Up @@ -438,7 +438,7 @@ public static function messageWasAlreadySent($senderId, $receiverId, $subject, $
/**
* Sends a message to a user/group.
*
* @param int $receiver_user_id
* @param int $receiverUserId
* @param string $subject
* @param string $content
* @param array $attachments files array($_FILES) (optional)
Expand All @@ -457,7 +457,7 @@ public static function messageWasAlreadySent($senderId, $receiverId, $subject, $
* @return bool
*/
public static function send_message(
$receiver_user_id,
$receiverUserId,
$subject,
$content,
array $attachments = [],
Expand All @@ -475,15 +475,15 @@ public static function send_message(
$status = 0
) {
$group_id = (int) $group_id;
$receiver_user_id = (int) $receiver_user_id;
$receiverUserId = (int) $receiverUserId;
$parent_id = (int) $parent_id;
$editMessageId = (int) $editMessageId;
$topic_id = (int) $topic_id;
$status = empty($status) ? MESSAGE_STATUS_UNREAD : (int) $status;

$sendEmail = true;
if (!empty($receiver_user_id)) {
$receiverUserInfo = api_get_user_info($receiver_user_id);
if (!empty($receiverUserId)) {
$receiverUserInfo = api_get_user_info($receiverUserId);
if (empty($receiverUserInfo)) {
return false;
}
Expand All @@ -495,8 +495,8 @@ public static function send_message(

// Disabling messages depending the pausetraining plugin.
$allowPauseFormation =
api_get_plugin_setting('pausetraining', 'tool_enable') === 'true' &&
api_get_plugin_setting('pausetraining', 'allow_users_to_edit_pause_formation') === 'true';
'true' === api_get_plugin_setting('pausetraining', 'tool_enable') &&
'true' === api_get_plugin_setting('pausetraining', 'allow_users_to_edit_pause_formation');

if ($allowPauseFormation) {
$extraFieldValue = new ExtraFieldValue('user');
Expand All @@ -505,13 +505,19 @@ public static function send_message(
'allow_notifications'
);

// User doesn't want email notifications but chamilo inbox still available (Option was not set)
if (empty($allowEmailNotifications)) {
$sendEmail = false;
}

// User doesn't want email notifications but chamilo inbox still available. (Option was set to "No")
if (!empty($allowEmailNotifications) &&
isset($allowEmailNotifications['value']) && 1 === (int) $allowEmailNotifications['value']
isset($allowEmailNotifications['value']) && 0 === (int) $allowEmailNotifications['value']
) {
$sendEmail = false;
}

if ($sendEmail) {
$startDate = $extraFieldValue->get_values_by_handler_and_field_variable(
$receiverUserInfo['user_id'],
'start_pause_date'
Expand Down Expand Up @@ -607,7 +613,7 @@ public static function send_message(
$now = api_get_utc_datetime();
$table = Database::get_main_table(TABLE_MESSAGE);

if (!empty($receiver_user_id) || !empty($group_id)) {
if (!empty($receiverUserId) || !empty($group_id)) {
// message for user friend
//@todo it's possible to edit a message? yes, only for groups
if (!empty($editMessageId)) {
Expand All @@ -620,7 +626,7 @@ public static function send_message(
} else {
$params = [
'user_sender_id' => $user_sender_id,
'user_receiver_id' => $receiver_user_id,
'user_receiver_id' => $receiverUserId,
'msg_status' => $status,
'send_date' => $now,
'title' => $subject,
Expand Down Expand Up @@ -661,7 +667,7 @@ public static function send_message(
$comment,
$messageId,
null,
$receiver_user_id,
$receiverUserId,
$group_id
);
}
Expand All @@ -672,7 +678,7 @@ public static function send_message(
if (empty($group_id) && MESSAGE_STATUS_UNREAD == $status) {
$params = [
'user_sender_id' => $user_sender_id,
'user_receiver_id' => $receiver_user_id,
'user_receiver_id' => $receiverUserId,
'msg_status' => MESSAGE_STATUS_OUTBOX,
'send_date' => $now,
'title' => $subject,
Expand Down Expand Up @@ -720,7 +726,7 @@ public static function send_message(
$notification->saveNotification(
$messageId,
$type,
[$receiver_user_id],
[$receiverUserId],
$subject,
$content,
$sender_info,
Expand Down Expand Up @@ -772,7 +778,7 @@ public static function send_message(
}

/**
* @param int $receiver_user_id
* @param int $receiverUserId
* @param int $subject
* @param string $message
* @param int $sender_id
Expand All @@ -785,7 +791,7 @@ public static function send_message(
* @return bool
*/
public static function send_message_simple(
$receiver_user_id,
$receiverUserId,
$subject,
$message,
$sender_id = 0,
Expand All @@ -804,7 +810,7 @@ public static function send_message_simple(
$files = $attachmentList;
}
$result = self::send_message(
$receiver_user_id,
$receiverUserId,
$subject,
$message,
$files,
Expand All @@ -820,8 +826,8 @@ public static function send_message_simple(
);

if ($sendCopyToDrhUsers) {
$userInfo = api_get_user_info($receiver_user_id);
$drhList = UserManager::getDrhListFromUser($receiver_user_id);
$userInfo = api_get_user_info($receiverUserId);
$drhList = UserManager::getDrhListFromUser($receiverUserId);
if (!empty($drhList)) {
foreach ($drhList as $drhInfo) {
$message = sprintf(
Expand All @@ -844,47 +850,6 @@ public static function send_message_simple(
return $result;
}

/**
* Update parent ids for other receiver user from current message in groups.
*
* @author Christian Fasanando Flores
*
* @param int $parent_id
* @param int $receiver_user_id
* @param int $messageId
*/
public static function update_parent_ids_from_reply(
$parent_id,
$receiver_user_id,
$messageId
) {
$table = Database::get_main_table(TABLE_MESSAGE);
$parent_id = intval($parent_id);
$receiver_user_id = intval($receiver_user_id);
$messageId = intval($messageId);

// first get data from message id (parent)
$sql = "SELECT * FROM $table WHERE id = '$parent_id'";
$rs_message = Database::query($sql);
$row_message = Database::fetch_array($rs_message);

// get message id from data found early for other receiver user
$sql = "SELECT id FROM $table
WHERE
user_sender_id ='{$row_message['user_sender_id']}' AND
title='{$row_message['title']}' AND
content='{$row_message['content']}' AND
group_id='{$row_message['group_id']}' AND
user_receiver_id='$receiver_user_id'";
$result = Database::query($sql);
$row = Database::fetch_array($result);

// update parent_id for other user receiver
$sql = "UPDATE $table SET parent_id = ".$row['id']."
WHERE id = $messageId";
Database::query($sql);
}

/**
* @param int $user_receiver_id
* @param int $id
Expand Down

0 comments on commit 4e0e2be

Please sign in to comment.