cm_webservice_user.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. require_once __DIR__.'/cm_webservice.php';
  8. /**
  9. * Description of cm_soap_user.
  10. *
  11. * @author marcosousa
  12. */
  13. class WSCMUser extends WSCM
  14. {
  15. public function find_id_user($username, $password, $name)
  16. {
  17. if ($this->verifyUserPass($username, $password) == "valid") {
  18. $listResult = "#";
  19. $listArrayResult = [];
  20. $listArray = [];
  21. $list = $this->get_user_list_like_start(
  22. ['firstname' => $name],
  23. ['firstname']
  24. );
  25. foreach ($list as $userData) {
  26. $listArray[] = $userData['user_id'];
  27. }
  28. $list = $this->get_user_list_like_start(
  29. ['lastname' => $name],
  30. ['firstname']
  31. );
  32. foreach ($list as $userData) {
  33. $listArray[] = $userData['user_id'];
  34. }
  35. $list = $this->get_user_list_like_start(
  36. ['email' => $name],
  37. ['firstname']
  38. );
  39. foreach ($list as $userData) {
  40. $listArray[] = $userData['user_id'];
  41. }
  42. $listArrayResult = array_unique($listArray);
  43. foreach ($listArrayResult as $result) {
  44. $listResult .= $result."#";
  45. }
  46. return $listResult;
  47. }
  48. return "0";
  49. }
  50. public function get_link_user_picture($username, $password, $id)
  51. {
  52. if ($this->verifyUserPass($username, $password) == "valid") {
  53. $userPic = UserManager::getUserPicture($id);
  54. if (empty($userPic)) {
  55. return "0";
  56. }
  57. return $userPic;
  58. }
  59. return "0";
  60. }
  61. public function get_user_name($username, $password, $id, $field)
  62. {
  63. if ($this->verifyUserPass($username, $password) == "valid") {
  64. $userInfo = api_get_user_info($id);
  65. switch ($field) {
  66. case 'firstname':
  67. return $userInfo['firstname'];
  68. break;
  69. case 'lastname':
  70. return $userInfo['lastname'];
  71. break;
  72. case 'bothfl':
  73. return $userInfo['firstname']." ".$userInfo['lastname'];
  74. break;
  75. case 'bothlf':
  76. return $userInfo['lastname']." ".$userInfo['firstname'];
  77. break;
  78. default:
  79. return $userInfo['firstname'];
  80. }
  81. return "0";
  82. }
  83. return "0";
  84. }
  85. public function send_invitation(
  86. $username,
  87. $password,
  88. $userfriend_id,
  89. $content_message = ''
  90. ) {
  91. global $charset;
  92. if ($this->verifyUserPass($username, $password) == "valid") {
  93. $user_id = UserManager::get_user_id_from_username($username);
  94. $message_title = get_lang('Invitation');
  95. $count_is_true = SocialManager::send_invitation_friend(
  96. $user_id,
  97. $userfriend_id,
  98. $message_title,
  99. $content_message
  100. );
  101. if ($count_is_true) {
  102. return Display::return_message(
  103. api_htmlentities(
  104. get_lang('InvitationHasBeenSent'),
  105. ENT_QUOTES,
  106. $charset
  107. ),
  108. 'normal',
  109. false
  110. );
  111. } else {
  112. return Display::return_message(
  113. api_htmlentities(
  114. get_lang('YouAlreadySentAnInvitation'),
  115. ENT_QUOTES,
  116. $charset
  117. ),
  118. 'error',
  119. false
  120. );
  121. }
  122. }
  123. return get_lang('InvalidId');
  124. }
  125. public function accept_friend($username, $password, $userfriend_id)
  126. {
  127. if ($this->verifyUserPass($username, $password) == "valid") {
  128. $user_id = UserManager::get_user_id_from_username($username);
  129. UserManager::relate_users(
  130. $userfriend_id,
  131. $user_id,
  132. USER_RELATION_TYPE_FRIEND
  133. );
  134. SocialManager::invitation_accepted($userfriend_id, $user_id);
  135. return get_lang('AddedContactToList');
  136. }
  137. return get_lang('InvalidId');
  138. }
  139. public function denied_invitation($username, $password, $userfriend_id)
  140. {
  141. if ($this->verifyUserPass($username, $password) == "valid") {
  142. $user_id = UserManager::get_user_id_from_username($username);
  143. SocialManager::invitation_denied($userfriend_id, $user_id);
  144. return get_lang('InvitationDenied');
  145. }
  146. return get_lang('InvalidId');
  147. }
  148. /**
  149. * Get a list of users of which the given conditions match with a LIKE '%cond%'.
  150. *
  151. * @param array $conditions a list of condition (exemple : status=>STUDENT)
  152. * @param array $order_by a list of fields on which sort
  153. *
  154. * @return array an array with all users of the platform
  155. *
  156. * @todo optional course code parameter, optional sorting parameters...
  157. *@todo Use the UserManager class
  158. * @todo security filter order by
  159. */
  160. private static function get_user_list_like_start($conditions = [], $order_by = [])
  161. {
  162. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  163. $return_array = [];
  164. $sql_query = "SELECT * FROM $user_table";
  165. if (count($conditions) > 0) {
  166. $sql_query .= ' WHERE ';
  167. foreach ($conditions as $field => $value) {
  168. $field = Database::escape_string($field);
  169. $value = Database::escape_string($value);
  170. $sql_query .= $field.' LIKE \''.$value.'%\'';
  171. }
  172. }
  173. $order = '';
  174. foreach ($order_by as $orderByItem) {
  175. $order .= Database::escape_string($orderByItem, null, false).', ';
  176. }
  177. $order = substr($order, 0, -2);
  178. if (count($order_by) > 0) {
  179. $sql_query .= ' ORDER BY '.$order;
  180. }
  181. $sql_result = Database::query($sql_query);
  182. while ($result = Database::fetch_array($sql_result)) {
  183. $return_array[] = $result;
  184. }
  185. return $return_array;
  186. }
  187. }