/** * Sends a message to a user/group * * @param int $receiver_user_id * @param string $subject * @param string $content * @param array $file_attachments files array($_FILES) (optional) * @param array $file_comments about attachment files (optional) * @param int $group_id (optional) * @param int $parent_id (optional) * @param int $edit_message_id id for updating the message (optional) * @param int $topic_id (optional) the default value is the current user_id * @param int $sender_id * @return bool */ public static function send_message($receiver_user_id, $subject, $content, $file_attachments = array(), $file_comments = array(), $group_id = 0, $parent_id = 0, $edit_message_id = 0, $topic_id = 0, $sender_id = null, $directMessage = false) { $table_message = Database::get_main_table(TABLE_MESSAGE); $group_id = intval($group_id); $receiver_user_id = intval($receiver_user_id); $parent_id = intval($parent_id); $edit_message_id = intval($edit_message_id); $topic_id = intval($topic_id); if (!empty($receiver_user_id)) { $receiverUserInfo = api_get_user_info($receiver_user_id); // Disabling messages for inactive users. if ($receiverUserInfo['active'] == 0) { return false; } } if (empty($sender_id)) { $user_sender_id = api_get_user_id(); } else { $user_sender_id = intval($sender_id); } $total_filesize = 0; if (is_array($file_attachments)) { foreach ($file_attachments as $file_attach) { $total_filesize += $file_attach['size']; } } // Validating fields if (empty($subject) && empty($group_id)) { Display::addFlash(Display::return_message(get_lang('YouShouldWriteASubject'), 'warning')); return false; } else { if ($total_filesize > intval(api_get_setting('message.message_max_upload_filesize'))) { $warning = sprintf(get_lang("FilesSizeExceedsX"), format_file_size(api_get_setting('message.message_max_upload_filesize'))); Display::addFlash(Display::return_message($warning, 'warning')); return false; } } $inbox_last_id = null; //Just in case we replace the and \n and \n\r while saving in the DB $content = str_replace(array("\n", "\n\r"), '<br />', $content); $now = api_get_utc_datetime(); if (!empty($receiver_user_id) || !empty($group_id)) { // message for user friend $clean_subject = Database::escape_string($subject); $clean_content = Database::escape_string($content); //message in inbox for user friend //@todo it's possible to edit a message? yes, only for groups if ($edit_message_id) { $query = " UPDATE {$table_message} SET\n update_date = '" . $now . "',\n content = '{$clean_content}'\n WHERE id = '{$edit_message_id}' "; Database::query($query); $inbox_last_id = $edit_message_id; } else { $params = ['user_sender_id' => $user_sender_id, 'user_receiver_id' => $receiver_user_id, 'msg_status' => '1', 'send_date' => $now, 'title' => $subject, 'content' => $content, 'group_id' => $group_id, 'parent_id' => $parent_id, 'update_date' => $now]; $inbox_last_id = Database::insert($table_message, $params); } // Save attachment file for inbox messages if (is_array($file_attachments)) { $i = 0; foreach ($file_attachments as $file_attach) { if ($file_attach['error'] == 0) { self::save_message_attachment_file($file_attach, $file_comments[$i], $inbox_last_id, null, $receiver_user_id, $group_id); } $i++; } } if (empty($group_id)) { // message in outbox for user friend or group $params = ['user_sender_id' => $user_sender_id, 'user_receiver_id' => $receiver_user_id, 'msg_status' => '4', 'send_date' => $now, 'title' => $subject, 'content' => $content, 'group_id' => $group_id, 'parent_id' => $parent_id, 'update_date' => $now]; $outbox_last_id = Database::insert($table_message, $params); // save attachment file for outbox messages if (is_array($file_attachments)) { $o = 0; foreach ($file_attachments as $file_attach) { if ($file_attach['error'] == 0) { self::save_message_attachment_file($file_attach, $file_comments[$o], $outbox_last_id, $user_sender_id); } $o++; } } } // Load user settings. $notification = new Notification(); $sender_info = api_get_user_info($user_sender_id); if (empty($group_id)) { $type = Notification::NOTIFICATION_TYPE_MESSAGE; if ($directMessage) { $type = Notification::NOTIFICATION_TYPE_DIRECT_MESSAGE; } $notification->save_notification($type, array($receiver_user_id), $subject, $content, $sender_info); } else { $usergroup = new UserGroup(); $group_info = $usergroup->get($group_id); $group_info['topic_id'] = $topic_id; $group_info['msg_id'] = $inbox_last_id; $user_list = $usergroup->get_users_by_group($group_id, false, array(), 0, 1000); // Adding more sense to the message group $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']); $new_user_list = array(); foreach ($user_list as $user_data) { $new_user_list[] = $user_data['user_id']; } $group_info = array('group_info' => $group_info, 'user_info' => $sender_info); $notification->save_notification(Notification::NOTIFICATION_TYPE_GROUP, $new_user_list, $subject, $content, $group_info); } return $inbox_last_id; } return false; }
$social_right_content .= get_lang('YouNeedToHaveFriendsInYourSocialNetwork'); } else { $social_right_content .= get_lang('YouAlreadyInviteAllYourContacts'); } $social_right_content .= '<div>'; $social_right_content .= '<a href="search.php">' . get_lang('TryAndFindSomeFriends') . '</a>'; $social_right_content .= '</div>'; } $form = new FormValidator('invitation', 'post', api_get_self() . '?id=' . $group_id); $form->addHidden('form_sent', 1); $form->addHidden('id', $group_id); $group_members_element = $form->addElement('advmultiselect', 'invitation', get_lang('Friends'), $nosessionUsersList, 'style="width: 280px;"'); $form->addButtonSave(get_lang('InviteUsersToGroup')); $social_right_content .= $form->returnForm(); // Current group members $members = $usergroup->get_users_by_group($group_id, false, array(GROUP_USER_PERMISSION_PENDING_INVITATION)); if (is_array($members) && count($members) > 0) { foreach ($members as &$member) { $image = UserManager::getUserPicture($member['user_id']); $member['image'] = '<img src="' . $image . '" width="50px" height="50px" />'; } $social_right_content .= '<h3>' . get_lang('UsersAlreadyInvited') . '</h3>'; $social_right_content .= Display::return_sortable_grid('invitation_profile', array(), $members, array('hide_navigation' => true, 'per_page' => 100), array(), false, array(true, false, true, true)); } $tpl = \Chamilo\CoreBundle\Framework\Container::getTwig(); SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'groups', $group_id); $social_menu_block = SocialManager::show_social_menu('member_list', $group_id); $tpl->addGlobal('social_menu_block', $social_menu_block); //$tpl->setHelp('Groups'); $tpl->addGlobal('social_right_content', $social_right_content); echo $tpl->render('@template_style/social/add_groups.html.twig');
//if i'm the admin if ($usergroup->is_group_admin($group_id)) { $usergroup->update_user_role($user_moderator, $group_id, GROUP_USER_PERMISSION_MODERATOR); $show_message = Display::return_message(get_lang('UserChangeToModerator')); } } if (isset($_GET['action']) && $_GET['action'] == 'delete_moderator') { // we add a user only if is a open group $user_moderator = intval($_GET['u']); //only group admins can do that if ($usergroup->is_group_admin($group_id)) { $usergroup->update_user_role($user_moderator, $group_id, GROUP_USER_PERMISSION_READER); $show_message = Display::return_message(get_lang('UserChangeToReader')); } } $users = $usergroup->get_users_by_group($group_id, false, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_READER, GROUP_USER_PERMISSION_MODERATOR), 0, 1000); $new_member_list = array(); $social_left_content = SocialManager::show_social_menu('member_list', $group_id); $social_right_content = '<h2>' . $group_info['name'] . '</h2>'; foreach ($users as $user) { switch ($user['relation_type']) { case GROUP_USER_PERMISSION_ADMIN: $user['link'] = Display::return_icon('social_group_admin.png', get_lang('Admin')); break; case GROUP_USER_PERMISSION_READER: if (in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_MODERATOR))) { $user['link'] = '<a href="group_members.php?id=' . $group_id . '&u=' . $user['user_id'] . '&action=delete">' . Display::return_icon('delete.png', get_lang('DeleteFromGroup')) . '</a>' . '<a href="group_members.php?id=' . $group_id . '&u=' . $user['user_id'] . '&action=set_moderator">' . Display::return_icon('social_moderator_add.png', get_lang('AddModerator')) . '</a>'; } break; case GROUP_USER_PERMISSION_PENDING_INVITATION: $user['link'] = '<a href="group_members.php?id=' . $group_id . '&u=' . $user['user_id'] . '&action=add">' . Display::return_icon('pending_invitation.png', get_lang('PendingInvitation')) . '</a>';
$my_group_list = array(); if (is_array($results) && count($results) > 0) { foreach ($results as $result) { $id = $result['id']; $result['name'] = Security::remove_XSS($result['name'], STUDENT, true); $result['description'] = Security::remove_XSS($result['description'], STUDENT, true); $my_group_list[] = $id; $url_open = '<a href="group_view.php?id=' . $id . '">'; $url_close = '</a>'; $name = cut($result['name'], GROUP_TITLE_LENGTH, true); if ($result['relation_type'] == GROUP_USER_PERMISSION_ADMIN) { $name .= ' ' . Display::return_icon('social_group_admin.png', get_lang('Admin'), array('style' => 'vertical-align:middle')); } elseif ($result['relation_type'] == GROUP_USER_PERMISSION_MODERATOR) { $name .= ' ' . Display::return_icon('social_group_moderator.png', get_lang('Moderator'), array('style' => 'vertical-align:middle')); } $count_users_group = count($usergroup->get_users_by_group($id, false, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_READER, GROUP_USER_PERMISSION_MODERATOR), 0, 1000)); if ($count_users_group == 1) { $count_users_group = $count_users_group . ' ' . get_lang('Member'); } else { $count_users_group = $count_users_group . ' ' . get_lang('Members'); } $picture = $usergroup->get_picture_group($result['id'], $result['picture'], 80); $result['picture'] = '<img class="social-groups-image" src="' . $picture['file'] . '" />'; $item_0 = Display::div($result['picture'], array('class' => 'box_description_group_image')); $members = Display::span($count_users_group, array('class' => 'box_description_group_member')); $item_1 = Display::div(Display::tag('h4', $url_open . $name . $url_close) . $members, array('class' => 'box_description_group_title')); $item_2 = ''; $item_3 = ''; if ($result['description'] != '') { $item_3 = '<div class="box_description_group_content" >' . cut($result['description'], 100, true) . '</div>'; } else {
} elseif ($role == GROUP_USER_PERMISSION_PENDING_INVITATION) { $social_right_content .= '<a class="btn" href="group_view.php?id=' . $group_id . '&action=join&u=' . api_get_user_id() . '">' . get_lang('YouHaveBeenInvitedJoinNow') . '</a>'; } $social_right_content .= '<br /><br />'; } $content = MessageManager::display_messages_for_group($group_id); if ($is_group_member) { if (empty($content)) { $createThreadUrl = api_get_path(WEB_CODE_PATH) . 'social/message_for_group_form.inc.php?' . http_build_query(['view_panel' => 1, 'user_friend' => api_get_user_id(), 'group_id' => $group_id, 'action' => 'add_message_group']); $create_thread_link = Display::url(get_lang('YouShouldCreateATopic'), $createThreadUrl, ['class' => 'ajax btn btn-default', 'title' => get_lang('ComposeMessage'), 'data-title' => get_lang('ComposeMessage')]); } else { $createThreadUrl = api_get_path(WEB_CODE_PATH) . 'social/message_for_group_form.inc.php?' . http_build_query(['view_panel' => 1, 'user_friend' => api_get_user_id(), 'group_id' => $group_id, 'action' => add_message_group]); $create_thread_link = Display::url(get_lang('NewTopic'), $createThreadUrl, ['class' => 'ajax btn btn-default', 'title' => get_lang('ComposeMessage'), 'data-title' => get_lang('ComposeMessage')]); } } $members = $usergroup->get_users_by_group($group_id, true); $member_content = ''; // Members if (count($members) > 0) { if ($role == GROUP_USER_PERMISSION_ADMIN) { $member_content .= Display::url(Display::return_icon('edit.gif', get_lang('EditMembersList')) . ' ' . get_lang('EditMembersList'), 'group_members.php?id=' . $group_id); } foreach ($members as $member) { // if is a member if (in_array($member['relation_type'], array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_READER, GROUP_USER_PERMISSION_MODERATOR))) { //add icons if ($member['relation_type'] == GROUP_USER_PERMISSION_ADMIN) { $icon = Display::return_icon('social_group_admin.png', get_lang('Admin')); } elseif ($member['relation_type'] == GROUP_USER_PERMISSION_MODERATOR) { $icon = Display::return_icon('social_group_moderator.png', get_lang('Moderator')); } else {
} else { $group_info = $usergroup->get($group_id); if (empty($group_info)) { api_not_allowed(); } //only admin or moderator can do that $user_role = $usergroup->get_user_group_role(api_get_user_id(), $group_id); if (!in_array($user_role, array(GROUP_USER_PERMISSION_ADMIN, GROUP_USER_PERMISSION_MODERATOR))) { api_not_allowed(); } } $interbreadcrumb[] = array('url' => 'groups.php', 'name' => get_lang('Groups')); $interbreadcrumb[] = array('url' => 'group_view.php?id=' . $group_id, 'name' => $group_info['name']); $interbreadcrumb[] = array('url' => '#', 'name' => get_lang('WaitingList')); // Group information $admins = $usergroup->get_users_by_group($group_id, true, array(GROUP_USER_PERMISSION_ADMIN), 0, 1000); $show_message = ''; if (isset($_GET['action']) && $_GET['action'] == 'accept') { // we add a user only if is a open group $user_join = intval($_GET['u']); //if i'm a moderator if ($usergroup->is_group_moderator($group_id)) { $usergroup->update_user_role($user_join, $group_id); Display::addFlash(Display::return_message(get_lang('UserAdded'))); } } if (isset($_GET['action']) && $_GET['action'] == 'deny') { // we add a user only if is a open group $user_join = intval($_GET['u']); //if i'm a moderator if ($usergroup->is_group_moderator($group_id)) {
/** * Sends a message to a user/group * * @param int receiver user id * @param string subject * @param string content * @param array attachment files array($_FILES) (optional) * @param array comments about attachment files (optional) * @param int group id (optional) * @param int parent id (optional) * @param int message id for updating the message (optional) * @param int sender id (optional) the default value is the current user_id * @return bool */ public static function send_message($receiver_user_id, $subject, $content, $file_attachments = array(), $file_comments = array(), $group_id = 0, $parent_id = 0, $edit_message_id = 0, $topic_id = 0, $sender_id = null, $text_content = null) { $table_message = Database::get_main_table(TABLE_MESSAGE); $group_id = intval($group_id); $receiver_user_id = intval($receiver_user_id); $parent_id = intval($parent_id); $edit_message_id = intval($edit_message_id); $topic_id = intval($topic_id); /* Saving the user id for the chamilo inbox, if the sender is null we asume that the current user is the one that sent the message */ if (empty($sender_id)) { $user_sender_id = api_get_user_id(); } else { $user_sender_id = intval($sender_id); } $total_filesize = 0; if (is_array($file_attachments)) { foreach ($file_attachments as $file_attach) { $total_filesize += $file_attach['size']; } } // Validating fields if (empty($subject) && empty($group_id)) { return get_lang('YouShouldWriteASubject'); } else { if ($total_filesize > intval(api_get_setting('message_max_upload_filesize'))) { return sprintf(get_lang("FilesSizeExceedsX"), Text::format_file_size(api_get_setting('message_max_upload_filesize'))); } } $inbox_last_id = null; // Just in case we replace the and \n and \n\r while saving in the DB. $content = str_replace(array("\n", "\n\r"), '<br />', $content); $now = api_get_utc_datetime(); if (!empty($receiver_user_id) || !empty($group_id)) { // message for user friend $clean_subject = Database::escape_string($subject); $clean_content = Database::escape_string($content); //message in inbox for user friend //@todo it's possible to edit a message? yes, only for groups if ($edit_message_id) { $query = " UPDATE {$table_message} SET update_date = '" . $now . "', content = '{$clean_content}' WHERE id = '{$edit_message_id}' "; Database::query($query); $inbox_last_id = $edit_message_id; } else { $query = "INSERT INTO {$table_message}(user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) " . "VALUES ('{$user_sender_id}', '{$receiver_user_id}', '1', '" . $now . "','{$clean_subject}','{$clean_content}','{$group_id}','{$parent_id}', '" . $now . "')"; Database::query($query); $inbox_last_id = Database::insert_id(); } // Save attachment file for inbox messages if (is_array($file_attachments)) { $i = 0; foreach ($file_attachments as $file_attach) { if ($file_attach['error'] == 0) { $comments = isset($file_comments[$i]) ? $file_comments[$i] : null; self::save_message_attachment_file($file_attach, $comments, $inbox_last_id, null, $receiver_user_id, $group_id); } $i++; } } if (empty($group_id)) { //message in outbox for user friend or group $sql = "INSERT INTO {$table_message} (user_sender_id, user_receiver_id, msg_status, send_date, title, content, group_id, parent_id, update_date ) " . " VALUES ('{$user_sender_id}', '{$receiver_user_id}', '4', '" . $now . "','{$clean_subject}','{$clean_content}', '{$group_id}', '{$parent_id}', '" . $now . "')"; Database::query($sql); $outbox_last_id = Database::insert_id(); // save attachment file for outbox messages if (is_array($file_attachments)) { $o = 0; foreach ($file_attachments as $file_attach) { if ($file_attach['error'] == 0) { self::save_message_attachment_file($file_attach, $file_comments[$o], $outbox_last_id, $user_sender_id); } $o++; } } } // Load user settings. $notification = new Notification(); $sender_info = array(); if (empty($group_id)) { if (!empty($user_sender_id)) { $sender_info = api_get_user_info($user_sender_id); } $notification->save_notification(Notification::NOTIFICATION_TYPE_MESSAGE, array($receiver_user_id), $subject, $content, $sender_info, $text_content); } else { $usergroup = new UserGroup(); $group_info = $usergroup->get($group_id); $group_info['topic_id'] = $topic_id; $group_info['msg_id'] = $inbox_last_id; $user_list = $usergroup->get_users_by_group($group_id, false, array(), 0, 1000); // Adding sense to the message group. $subject = sprintf(get_lang('ThereIsANewMessageInTheGroupX'), $group_info['name']); $new_user_list = array(); foreach ($user_list as $user_data) { $new_user_list[] = $user_data['user_id']; } $group_info = array('group_info' => $group_info, 'user_info' => $sender_info); $notification->save_notification(Notification::NOTIFICATION_TYPE_GROUP, $new_user_list, $subject, $content, $group_info, $text_content); } return $inbox_last_id; } return false; }