user_manager.ajax.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Doctrine\Common\Collections\Criteria,
  4. Chamilo\UserBundle\Entity\User,
  5. Doctrine\ORM\Query\Expr\Join;
  6. /**
  7. * Responses to AJAX calls
  8. */
  9. require_once __DIR__.'/../global.inc.php';
  10. $action = $_GET['a'];
  11. switch ($action) {
  12. case 'get_user_like':
  13. $query = $_REQUEST['q'];
  14. $conditions = [
  15. 'username' => $query,
  16. 'firstname' => $query,
  17. 'lastname' => $query,
  18. ];
  19. $users = UserManager::get_user_list_like($conditions, [], false, 'OR');
  20. $result = [];
  21. if (!empty($users)) {
  22. foreach ($users as $user) {
  23. $result[] = ['id' => $user['id'], 'text' => $user['complete_name'].' ('.$user['username'].')'];
  24. }
  25. $result['items'] = $result;
  26. }
  27. echo json_encode($result);
  28. break;
  29. case 'get_user_popup':
  30. $user_info = api_get_user_info($_REQUEST['user_id']);
  31. $ajax_url = api_get_path(WEB_AJAX_PATH).'message.ajax.php';
  32. echo '<div class="row">';
  33. echo '<div class="col-sm-5">';
  34. echo '<div class="thumbnail">';
  35. echo '<img src="'.$user_info['avatar'].'" /> ';
  36. echo '</div>';
  37. echo '</div>';
  38. echo '<div class="col-sm-7">';
  39. if (api_get_setting('show_email_addresses') == 'false') {
  40. $user_info['mail'] = ' ';
  41. } else {
  42. $user_info['mail'] = ' '.$user_info['mail'].' ';
  43. }
  44. echo '<a href="'.api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user_info['user_id'].'">';
  45. echo '<h3>'.$user_info['complete_name'].'</h3>'.$user_info['mail'].$user_info['official_code'];
  46. echo '</a>';
  47. echo '</div>';
  48. echo '</div>';
  49. if (api_get_setting('allow_message_tool') == 'true') {
  50. echo '<script>';
  51. echo '
  52. $("#send_message_link").on("click", function() {
  53. var url = "'.$ajax_url.'?a=send_message&user_id='.$user_info['user_id'].'";
  54. var params = $("#send_message").serialize();
  55. $.ajax({
  56. url: url+"&"+params,
  57. success:function(data) {
  58. $("#subject_id").val("");
  59. $("#content_id").val("");
  60. $("#send_message").html(data);
  61. $("#send_message_link").hide();
  62. }
  63. });
  64. });';
  65. echo '</script>';
  66. echo MessageManager::generate_message_form();
  67. echo '
  68. <div class="row">
  69. <div class="col-sm-10 col-sm-offset-2">
  70. <a class="btn btn-primary" id="send_message_link">
  71. <em class="fa fa-envelope"></em> ' . get_lang('Send').'
  72. </a>
  73. </div>
  74. </div>
  75. ';
  76. }
  77. break;
  78. case 'user_id_exists':
  79. if (api_is_anonymous()) {
  80. echo '';
  81. } else {
  82. if (UserManager::is_user_id_valid($_GET['user_id'])) {
  83. echo 1;
  84. } else {
  85. echo 0;
  86. }
  87. }
  88. break;
  89. case 'search_tags':
  90. header('Content-Type: application/json');
  91. $result = ['items' => []];
  92. if (api_is_anonymous()) {
  93. echo json_encode($result);
  94. break;
  95. }
  96. if (!isset($_GET['q'], $_GET['field_id'])) {
  97. echo json_encode($result);
  98. break;
  99. }
  100. $result['items'] = UserManager::get_tags($_GET['q'], $_GET['field_id'], null, '10');
  101. echo json_encode($result);
  102. break;
  103. case 'generate_api_key':
  104. if (api_is_anonymous()) {
  105. echo '';
  106. } else {
  107. $array_list_key = array();
  108. $user_id = api_get_user_id();
  109. $api_service = 'dokeos';
  110. $num = UserManager::update_api_key($user_id, $api_service);
  111. $array_list_key = UserManager::get_api_keys($user_id, $api_service);
  112. ?>
  113. <div class="form-group">
  114. <label class="col-sm-2 control-label"><?php echo get_lang('MyApiKey'); ?></label>
  115. <div class="col-sm-8">
  116. <input type="text" name="api_key_generate" id="id_api_key_generate" class="form-control" value="<?php echo $array_list_key[$num]; ?>"/>
  117. </div>
  118. </div>
  119. <?php
  120. }
  121. break;
  122. case 'active_user':
  123. if (api_is_platform_admin() && api_global_admin_can_edit_admin($_GET['user_id'])) {
  124. $user_id = intval($_GET['user_id']);
  125. $status = intval($_GET['status']);
  126. if (!empty($user_id)) {
  127. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  128. $sql = "UPDATE $user_table
  129. SET active='".$status."'
  130. WHERE user_id='".$user_id."'";
  131. $result = Database::query($sql);
  132. //Send and email if account is active
  133. if ($status == 1) {
  134. $user_info = api_get_user_info($user_id);
  135. $recipient_name = api_get_person_name($user_info['firstname'], $user_info['lastname'], null, PERSON_NAME_EMAIL_ADDRESS);
  136. $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  137. $email_admin = api_get_setting('emailAdministrator');
  138. $sender_name = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
  139. $emailbody = get_lang('Dear')." ".stripslashes($recipient_name).",\n\n";
  140. $emailbody .= sprintf(get_lang('YourAccountOnXHasJustBeenApprovedByOneOfOurAdministrators'), api_get_setting('siteName'))."\n";
  141. $emailbody .= sprintf(get_lang('YouCanNowLoginAtXUsingTheLoginAndThePasswordYouHaveProvided'), api_get_path(WEB_PATH)).",\n\n";
  142. $emailbody .= get_lang('HaveFun')."\n\n";
  143. //$emailbody.=get_lang('Problem'). "\n\n". get_lang('SignatureFormula');
  144. $emailbody .= api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n".get_lang('Manager')." ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".get_lang('Email')." : ".api_get_setting('emailAdministrator');
  145. $additionalParameters = array(
  146. 'smsType' => SmsPlugin::ACCOUNT_APPROVED_CONNECT,
  147. 'userId' => $user_id
  148. );
  149. $result = api_mail_html(
  150. $recipient_name,
  151. $user_info['mail'],
  152. $emailsubject,
  153. $emailbody,
  154. $sender_name,
  155. $email_admin,
  156. null,
  157. null,
  158. $additionalParameters
  159. );
  160. Event::addEvent(LOG_USER_ENABLE, LOG_USER_ID, $user_id);
  161. } else {
  162. Event::addEvent(LOG_USER_DISABLE, LOG_USER_ID, $user_id);
  163. }
  164. echo $status;
  165. }
  166. } else {
  167. echo '-1';
  168. }
  169. break;
  170. case 'user_by_role':
  171. api_block_anonymous_users(false);
  172. $criteria = new Criteria();
  173. $criteria
  174. ->where(
  175. Criteria::expr()->orX(
  176. Criteria::expr()->contains('username', $_REQUEST['q']),
  177. Criteria::expr()->contains('firstname', $_REQUEST['q']),
  178. Criteria::expr()->contains('lastname', $_REQUEST['q'])
  179. )
  180. )
  181. ->andWhere(
  182. Criteria::expr()->eq('status', DRH)
  183. );
  184. $users = UserManager::getRepository()->matching($criteria);
  185. if (!$users->count()) {
  186. echo json_encode([]);
  187. break;
  188. }
  189. $items = [];
  190. /** @var User $user */
  191. foreach ($users as $user) {
  192. $items[] = [
  193. 'id' => $user->getId(),
  194. 'text' => $user->getCompleteNameWithUsername()
  195. ];
  196. }
  197. header('Content-Type: application/json');
  198. echo json_encode(['items' => $items]);
  199. break;
  200. case 'teacher_to_basis_course':
  201. api_block_anonymous_users(false);
  202. $sortByFirstName = api_sort_by_first_name();
  203. $urlId = api_get_current_access_url_id();
  204. $qb = UserManager::getRepository()->createQueryBuilder('u');
  205. $qb->where(
  206. $qb->expr()->orX(
  207. $qb->expr()->like('u.username', ':q'),
  208. $qb->expr()->like('u.firstname', ':q'),
  209. $qb->expr()->like('u.lastname', ':q')
  210. )
  211. );
  212. if (api_is_multiple_url_enabled()) {
  213. $qb
  214. ->innerJoin('ChamiloCoreBundle:AccessUrlRelUser', 'uru', Join::WITH, 'u.userId = uru.userId')
  215. ->andWhere('uru.accessUrlId = '.$urlId);
  216. }
  217. $qb
  218. ->andWhere('u.status != '.DRH.' AND u.status != '.ANONYMOUS)
  219. ->orderBy(
  220. $sortByFirstName
  221. ? 'u.firstname, u.firstname'
  222. : 'u.firstname, u.lastname'
  223. )
  224. ->setParameter('q', '%'.$_REQUEST['q'].'%');
  225. $users = $qb->getQuery()->getResult();
  226. if (!$users) {
  227. echo json_encode([]);
  228. break;
  229. }
  230. $items = [];
  231. /** @var User $user */
  232. foreach ($users as $user) {
  233. $items[] = [
  234. 'id' => $user->getId(),
  235. 'text' => $user->getCompleteNameWithUsername()
  236. ];
  237. }
  238. header('Content-Type: application/json');
  239. echo json_encode(['items' => $items]);
  240. break;
  241. default:
  242. echo '';
  243. }
  244. exit;