Serve potencial users to send message in Api REST - refs #8366

remotes/angel/1.11.x
Angel Fernando Quiroz Campos 8 years ago
parent 9216257e00
commit 562b03e240
  1. 118
      main/inc/ajax/message.ajax.php
  2. 58
      main/inc/lib/message.lib.php
  3. 40
      main/inc/lib/webservices/Rest.php
  4. 12
      main/webservices/api/v2.php
  5. 64
      src/Chamilo/UserBundle/Entity/Repository/UserRepository.php

@ -4,6 +4,9 @@
* Responses to AJAX calls
*/
use Chamilo\UserBundle\Entity\User;
use Chamilo\UserBundle\Entity\Repository\UserRepository;
require_once '../global.inc.php';
$action = $_GET['a'];
@ -36,107 +39,34 @@ switch ($action) {
echo '';
break;
}
$track_online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
$tbl_my_user = Database::get_main_table(TABLE_MAIN_USER);
$tbl_my_user_friend = Database::get_main_table(TABLE_MAIN_USER_REL_USER);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
$tbl_access_url_rel_user = Database:: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
$search = Database::escape_string($_REQUEST['q']);
$access_url_id = api_get_multiple_access_url() == 'true' ? api_get_current_access_url_id() : 1;
$user_id = api_get_user_id();
$is_western_name_order = api_is_western_name_order();
/** @var UserRepository $repo */
$repo = Database::getManager()
->getRepository('ChamiloUserBundle:User');
$likeCondition = " AND (firstname LIKE '%$search%' OR lastname LIKE '%$search%' OR email LIKE '%$search%') ";
$users = $repo->findUsersToSendMessage(
api_get_user_id(),
$_REQUEST['q'],
$_REQUEST['page_limit']
);
if (api_get_setting('allow_social_tool') === 'true' && api_get_setting('allow_message_tool') === 'true') {
// All users
if (api_get_setting('allow_send_message_to_all_platform_users') === 'true' || api_is_platform_admin()) {
if ($access_url_id != 0) {
$sql = "SELECT DISTINCT u.user_id as id, u.firstname, u.lastname, u.email
FROM $tbl_user u LEFT JOIN $tbl_access_url_rel_user r ON u.user_id = r.user_id
WHERE
u.status <> 6 AND
u.user_id <> $user_id AND
r.access_url_id = $access_url_id
$likeCondition ";
$showEmail = api_get_setting('show_email_addresses') === 'true';
$return = ['items' => []];
} else {
$sql = "SELECT DISTINCT u.user_id as id, u.firstname, u.lastname, u.email
FROM $tbl_user u
WHERE
u.status <> 6 AND
u.user_id <> $user_id
$likeCondition ";
}
} else {
//only my contacts
if ($access_url_id != 0) {
$sql = "SELECT DISTINCT u.user_id as id, u.firstname, u.lastname, u.email
FROM $tbl_access_url_rel_user r, $tbl_my_user_friend uf
INNER JOIN $tbl_my_user AS u
ON uf.friend_user_id = u.user_id
WHERE
u.status <> 6 AND
relation_type NOT IN(".USER_RELATION_TYPE_DELETED.", ".USER_RELATION_TYPE_RRHH.") AND
uf.user_id = $user_id AND
friend_user_id <> $user_id AND
u.user_id = r.user_id AND
r.access_url_id = $access_url_id
$likeCondition";
} else {
$sql = "SELECT DISTINCT u.user_id as id, u.firstname, u.lastname, u.email
FROM $tbl_my_user_friend uf
INNER JOIN $tbl_my_user AS u
ON uf.friend_user_id = u.user_id
WHERE
u.status <> 6 AND
relation_type NOT IN(".USER_RELATION_TYPE_DELETED.", ".USER_RELATION_TYPE_RRHH.") AND
uf.user_id = $user_id AND
friend_user_id <> $user_id
$likeCondition";
}
}
} elseif (
api_get_setting('allow_social_tool') === 'false' &&
api_get_setting('allow_message_tool') === 'true'
) {
if (api_get_setting('allow_send_message_to_all_platform_users') === 'true') {
$sql = "SELECT DISTINCT u.user_id as id, u.firstname, u.lastname, u.email
FROM $tbl_user u LEFT JOIN $tbl_access_url_rel_user r ON u.user_id = r.user_id
WHERE
u.status <> 6 AND
u.user_id <> $user_id AND
r.access_url_id = $access_url_id
$likeCondition ";
} else {
$time_limit = api_get_setting('time_limit_whosonline');
$online_time = time() - $time_limit*60;
$limit_date = api_get_utc_datetime($online_time);
$sql = "SELECT SELECT DISTINCT u.user_id as id, u.firstname, u.lastname, u.email
FROM $tbl_my_user u INNER JOIN $track_online_table t
ON u.user_id=t.login_user_id
WHERE login_date >= '".$limit_date."' AND
$likeCondition";
}
}
$sql .=' LIMIT 20';
$result = Database::query($sql);
/** @var User $user */
foreach ($users as $user) {
$userName = $user->getCompleteName();
$showEmail = api_get_setting('show_email_addresses');
$return = array();
if (Database::num_rows($result) > 0) {
while ($row = Database::fetch_array($result, 'ASSOC')) {
$name = api_get_person_name($row['firstname'], $row['lastname']);
if ($showEmail == 'true') {
$name .= ' ('.$row['email'].')';
}
$return['items'][] = array(
'text' => $name,
'id' => $row['id']
);
if ($showEmail) {
$userName .= " ({$user->getEmail()})";
}
$return['items'][] = [
'text' => $userName,
'id' => $user->getId()
];
}
echo json_encode($return);
break;
default:

@ -1924,4 +1924,62 @@ class MessageManager
return array_reverse($mail_queue);
}
public static function findUsersToSendMessage($userId, $search)
{
$accessUrlId = api_get_multiple_access_url() ? api_get_current_access_url_id() : 1;
if (api_get_setting('allow_social_tool') === 'true' && api_get_setting('allow_message_tool') === 'true') {
// All users
if (api_get_setting('allow_send_message_to_all_platform_users') === 'true' || api_is_platform_admin()) {
$dql = "SELECT DISTINCT U
FROM ChamiloUserBundle:User U
LEFT JOIN ChamiloCoreBundle:AccessUrlRelUser R WITH U = R.user
WHERE
U.status != 6 AND
U.id != $userId AND
R.portal = $accessUrlId";
} else {
$dql = "SELECT DISTINCT U
FROM ChamiloCoreBundle:AccessUrlRelUser R, ChamiloCoreBundle:UserRelUser UF
INNER JOIN ChamiloUserBundle:User AS U WITH UF.friendUserId = U
WHERE
U.status != 6 AND
UF.relationType NOT IN(" . USER_RELATION_TYPE_DELETED . ", " . USER_RELATION_TYPE_RRHH . ") AND
UF.userId = $userId AND
UF.friendUserId != $userId AND
U = R.user AND
R.portal = $accessUrlId";
}
} elseif (
api_get_setting('allow_social_tool') === 'false' &&
api_get_setting('allow_message_tool') === 'true'
) {
if (api_get_setting('allow_send_message_to_all_platform_users') === 'true') {
$dql = "SELECT DISTINCT U
FROM ChamiloUserBundle:User U
LEFT JOIN ChamiloCoreBundle:AccessUrlRelUser R WITH U = R.user
WHERE
U.status != 6 AND
U.id != $userId AND
R.portal = $accessUrlId";
} else {
$time_limit = api_get_setting('time_limit_whosonline');
$online_time = time() - $time_limit * 60;
$limit_date = api_get_utc_datetime($online_time);
$dql = "SELECT DISTINCT U
FROM ChamiloUserBundle:User U
INNER JOIN ChamiloCoreBundle:TrackEOnline T WITH U.id = T.loginUserId
WHERE T.loginDate >= '" . $limit_date . "'";
}
}
$dql .= ' AND (U.firstname LIKE :search OR U.lastname LIKE :search OR U.email LIKE :search)';
return Database::getManager()
->createQuery($dql)
->setMaxResults(20)
->setParameters(['search' => "%$search%"])
->getResult();
}
}

@ -7,6 +7,7 @@ use Chamilo\CourseBundle\Entity\Repository\CAnnouncementRepository;
use Chamilo\CourseBundle\Entity\Repository\CNotebookRepository;
use Chamilo\CourseBundle\Entity\CLpCategory;
use Chamilo\CoreBundle\Entity\Session;
use Chamilo\UserBundle\Entity\User;
/**
* Class RestApi
@ -36,6 +37,7 @@ class Rest extends WebService
const ACTION_SAVE_FORUM_POST = 'save_forum_post';
const ACTION_USER_SESSIONS = 'user_sessions';
const SAVE_USER_MESSAGE = 'save_user_message';
const GET_MESSAGE_USERS = 'message_users';
const EXTRAFIELD_GCM_ID = 'gcm_registration_id';
@ -890,6 +892,12 @@ class Rest extends WebService
return $data;
}
/**
* @param string $subject
* @param string $text
* @param array $receivers
* @return array
*/
public function saveUserMessage($subject, $text, array $receivers)
{
foreach ($receivers as $userId) {
@ -900,4 +908,36 @@ class Rest extends WebService
'sent' => true
];
}
/**
* @param string $search
* @return array
*/
public function getMessageUsers($search)
{
/** @var UserRepository $repo */
$repo = Database::getManager()
->getRepository('ChamiloUserBundle:User');
$users = $repo->findUsersToSendMessage($this->user->getId(), $search);
$showEmail = api_get_setting('show_email_addresses') === 'true';
$data = [];
/** @var User $user */
foreach ($users as $user) {
$userName = $user->getCompleteName();
if ($showEmail) {
$userName .= " ({$user->getEmail()})";
}
$data[] = [
'id' => $user->getId(),
'name' => $userName,
];
}
return $data;
}
}

@ -197,6 +197,18 @@ try {
$restResponse->setData($data);
break;
case Rest::GET_MESSAGE_USERS:
$search = !empty($_REQUEST['q']) ? $_REQUEST['q'] : null;
if (!$search || strlen($search) < 2) {
throw new Exception(get_lang('TooShort'));
}
$data = $restApi->getMessageUsers($search);
$restResponse->setData($data);
break;
default:
throw new Exception(get_lang('InvalidAction'));
}

@ -230,4 +230,68 @@ class UserRepository extends EntityRepository
return $queryBuilder->getQuery()->getResult();
}
/**
* Find potencial users to send a message
* @param int $currentUserId The current user ID
* @param string $search The search text to filter the user list
* @param int $limit Optional. Sets the maximum number of results to retrieve
* @return mixed
*/
public function findUsersToSendMessage($currentUserId, $search, $limit = 10)
{
$accessUrlId = api_get_multiple_access_url() ? api_get_current_access_url_id() : 1;
if (api_get_setting('allow_social_tool') === 'true' && api_get_setting('allow_message_tool') === 'true') {
// All users
if (api_get_setting('allow_send_message_to_all_platform_users') === 'true' || api_is_platform_admin()) {
$dql = "SELECT DISTINCT U
FROM ChamiloUserBundle:User U
LEFT JOIN ChamiloCoreBundle:AccessUrlRelUser R WITH U = R.user
WHERE
U.status != 6 AND
U.id != $currentUserId AND
R.portal = $accessUrlId";
} else {
$dql = "SELECT DISTINCT U
FROM ChamiloCoreBundle:AccessUrlRelUser R, ChamiloCoreBundle:UserRelUser UF
INNER JOIN ChamiloUserBundle:User AS U WITH UF.friendUserId = U
WHERE
U.status != 6 AND
UF.relationType NOT IN(" . USER_RELATION_TYPE_DELETED . ", " . USER_RELATION_TYPE_RRHH . ") AND
UF.userId = $currentUserId AND
UF.friendUserId != $currentUserId AND
U = R.user AND
R.portal = $accessUrlId";
}
} elseif (
api_get_setting('allow_social_tool') === 'false' && api_get_setting('allow_message_tool') === 'true'
) {
if (api_get_setting('allow_send_message_to_all_platform_users') === 'true') {
$dql = "SELECT DISTINCT U
FROM ChamiloUserBundle:User U
LEFT JOIN ChamiloCoreBundle:AccessUrlRelUser R WITH U = R.user
WHERE
U.status != 6 AND
U.id != $currentUserId AND
R.portal = $accessUrlId";
} else {
$time_limit = api_get_setting('time_limit_whosonline');
$online_time = time() - $time_limit * 60;
$limit_date = api_get_utc_datetime($online_time);
$dql = "SELECT DISTINCT U
FROM ChamiloUserBundle:User U
INNER JOIN ChamiloCoreBundle:TrackEOnline T WITH U.id = T.loginUserId
WHERE T.loginDate >= '" . $limit_date . "'";
}
}
$dql .= ' AND (U.firstname LIKE :search OR U.lastname LIKE :search OR U.email LIKE :search)';
return $this->getEntityManager()
->createQuery($dql)
->setMaxResults($limit)
->setParameters(['search' => "%$search%"])
->getResult();
}
}

Loading…
Cancel
Save