user_manager.ajax.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. use Doctrine\Common\Collections\Criteria;
  5. use 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. if (api_is_platform_admin() || api_is_drh()) {
  14. $query = $_REQUEST['q'];
  15. $conditions = [
  16. 'username' => $query,
  17. 'firstname' => $query,
  18. 'lastname' => $query,
  19. ];
  20. $users = UserManager::getUserListLike($conditions, [], false, 'OR');
  21. $result = [];
  22. if (!empty($users)) {
  23. foreach ($users as $user) {
  24. $result[] = ['id' => $user['id'], 'text' => $user['complete_name'].' ('.$user['username'].')'];
  25. }
  26. $result['items'] = $result;
  27. }
  28. echo json_encode($result);
  29. }
  30. break;
  31. case 'get_user_popup':
  32. $courseId = isset($_REQUEST['course_id']) ? (int) $_REQUEST['course_id'] : 0;
  33. $sessionId = isset($_REQUEST['session_id']) ? (int) $_REQUEST['session_id'] : 0;
  34. $user_info = api_get_user_info($_REQUEST['user_id']);
  35. $isAnonymous = api_is_anonymous();
  36. echo '<div class="row">';
  37. echo '<div class="col-sm-5">';
  38. echo '<div class="thumbnail">';
  39. echo '<img src="'.$user_info['avatar'].'" /> ';
  40. echo '</div>';
  41. echo '</div>';
  42. echo '<div class="col-sm-7">';
  43. if (api_get_setting('show_email_addresses') == 'false') {
  44. $user_info['mail'] = ' ';
  45. } else {
  46. $user_info['mail'] = ' '.$user_info['mail'].' ';
  47. }
  48. if ($isAnonymous) {
  49. $user_info['mail'] = ' ';
  50. }
  51. $userData = '<h3>'.$user_info['complete_name'].'</h3>'.$user_info['mail'].$user_info['official_code'];
  52. if ($isAnonymous) {
  53. // Only allow anonymous users to see user popup if the popup user
  54. // is a teacher (which might be necessary to illustrate a course)
  55. if ($user_info['status'] === COURSEMANAGER) {
  56. echo $userData;
  57. } else {
  58. echo '<h3>-</h3>';
  59. }
  60. } else {
  61. echo Display::url(
  62. $userData,
  63. api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user_info['user_id']
  64. );
  65. }
  66. echo '</div>';
  67. echo '</div>';
  68. $url = api_get_path(WEB_AJAX_PATH).'message.ajax.php?a=send_message&user_id='.$user_info['user_id'].'&course_id='.$courseId.'&session_id='.$sessionId;
  69. if ($isAnonymous === false &&
  70. api_get_setting('allow_message_tool') == 'true'
  71. ) {
  72. echo '<script>';
  73. echo '
  74. $("#send_message_link").on("click", function() {
  75. var url = "'.$url.'";
  76. var params = $("#send_message").serialize();
  77. $.ajax({
  78. url: url+"&"+params,
  79. success:function(data) {
  80. $("#subject_id").val("");
  81. $("#content_id").val("");
  82. $("#send_message").html(data);
  83. $("#send_message_link").hide();
  84. }
  85. });
  86. });';
  87. echo '</script>';
  88. echo MessageManager::generate_message_form();
  89. echo '
  90. <div class="row">
  91. <div class="col-sm-10 col-sm-offset-2">
  92. <a class="btn btn-primary" id="send_message_link">
  93. <em class="fa fa-envelope"></em> '.get_lang('Send').'
  94. </a>
  95. </div>
  96. </div>
  97. ';
  98. }
  99. break;
  100. case 'user_id_exists':
  101. if (api_is_anonymous()) {
  102. echo '';
  103. } else {
  104. if (UserManager::is_user_id_valid($_GET['user_id'])) {
  105. echo 1;
  106. } else {
  107. echo 0;
  108. }
  109. }
  110. break;
  111. case 'search_tags':
  112. header('Content-Type: application/json');
  113. $result = ['items' => []];
  114. if (api_is_anonymous()) {
  115. echo json_encode($result);
  116. break;
  117. }
  118. if (!isset($_GET['q'], $_GET['field_id'])) {
  119. echo json_encode($result);
  120. break;
  121. }
  122. $result['items'] = UserManager::get_tags($_GET['q'], $_GET['field_id'], null, '10');
  123. echo json_encode($result);
  124. break;
  125. case 'generate_api_key':
  126. if (api_is_anonymous()) {
  127. echo '';
  128. } else {
  129. $array_list_key = [];
  130. $user_id = api_get_user_id();
  131. $api_service = 'dokeos';
  132. $num = UserManager::update_api_key($user_id, $api_service);
  133. $array_list_key = UserManager::get_api_keys($user_id, $api_service); ?>
  134. <div class="form-group">
  135. <label class="col-sm-2 control-label"><?php echo get_lang('MyApiKey'); ?></label>
  136. <div class="col-sm-8">
  137. <input type="text" name="api_key_generate" id="id_api_key_generate" class="form-control" value="<?php echo $array_list_key[$num]; ?>"/>
  138. </div>
  139. </div>
  140. <?php
  141. }
  142. break;
  143. case 'active_user':
  144. $allow = api_get_configuration_value('allow_disable_user_for_session_admin');
  145. if ((api_is_platform_admin() && api_global_admin_can_edit_admin($_GET['user_id'])) ||
  146. (
  147. $allow &&
  148. api_is_session_admin() &&
  149. api_global_admin_can_edit_admin($_GET['user_id'], null, true)
  150. )
  151. ) {
  152. $user_id = intval($_GET['user_id']);
  153. $status = intval($_GET['status']);
  154. if (!empty($user_id)) {
  155. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  156. $sql = "UPDATE $user_table
  157. SET active = '".$status."'
  158. WHERE user_id = '".$user_id."'";
  159. $result = Database::query($sql);
  160. // Send and email if account is active
  161. if ($status == 1) {
  162. $user_info = api_get_user_info($user_id);
  163. $recipientName = api_get_person_name(
  164. $user_info['firstname'],
  165. $user_info['lastname'],
  166. null,
  167. PERSON_NAME_EMAIL_ADDRESS
  168. );
  169. $subject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
  170. $emailAdmin = api_get_setting('emailAdministrator');
  171. $sender_name = api_get_person_name(
  172. api_get_setting('administratorName'),
  173. api_get_setting('administratorSurname'),
  174. null,
  175. PERSON_NAME_EMAIL_ADDRESS
  176. );
  177. $body = get_lang('Dear')." ".stripslashes($recipientName).",\n\n";
  178. $body .= sprintf(
  179. get_lang('YourAccountOnXHasJustBeenApprovedByOneOfOurAdministrators'),
  180. api_get_setting('siteName')
  181. )."\n";
  182. $body .= sprintf(
  183. get_lang('YouCanNowLoginAtXUsingTheLoginAndThePasswordYouHaveProvided'),
  184. api_get_path(WEB_PATH)
  185. ).",\n\n";
  186. $body .= get_lang('HaveFun')."\n\n";
  187. //$body.=get_lang('Problem'). "\n\n". get_lang('SignatureFormula');
  188. $body .= api_get_person_name(
  189. api_get_setting('administratorName'),
  190. api_get_setting('administratorSurname')
  191. )."\n".
  192. get_lang('Manager')." ".
  193. api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n".
  194. get_lang('Email')." : ".api_get_setting('emailAdministrator');
  195. $additionalParameters = [
  196. 'smsType' => SmsPlugin::ACCOUNT_APPROVED_CONNECT,
  197. 'userId' => $user_id,
  198. ];
  199. MessageManager::send_message_simple(
  200. $user_id,
  201. $subject,
  202. $body,
  203. null,
  204. false,
  205. false,
  206. $additionalParameters
  207. );
  208. /*$result = api_mail_html(
  209. $recipientName,
  210. $user_info['mail'],
  211. $subject,
  212. $body,
  213. $sender_name,
  214. $emailAdmin,
  215. null,
  216. null,
  217. $additionalParameters
  218. );*/
  219. Event::addEvent(LOG_USER_ENABLE, LOG_USER_ID, $user_id);
  220. } else {
  221. Event::addEvent(LOG_USER_DISABLE, LOG_USER_ID, $user_id);
  222. }
  223. echo $status;
  224. }
  225. } else {
  226. echo '-1';
  227. }
  228. break;
  229. case 'user_by_role':
  230. api_block_anonymous_users(false);
  231. $criteria = new Criteria();
  232. $criteria
  233. ->where(
  234. Criteria::expr()->orX(
  235. Criteria::expr()->contains('username', $_REQUEST['q']),
  236. Criteria::expr()->contains('firstname', $_REQUEST['q']),
  237. Criteria::expr()->contains('lastname', $_REQUEST['q'])
  238. )
  239. )
  240. ->andWhere(
  241. Criteria::expr()->eq('status', DRH)
  242. );
  243. $users = UserManager::getRepository()->matching($criteria);
  244. if (!$users->count()) {
  245. echo json_encode([]);
  246. break;
  247. }
  248. $items = [];
  249. /** @var User $user */
  250. foreach ($users as $user) {
  251. $items[] = [
  252. 'id' => $user->getId(),
  253. 'text' => UserManager::formatUserFullName($user, true),
  254. ];
  255. }
  256. header('Content-Type: application/json');
  257. echo json_encode(['items' => $items]);
  258. break;
  259. case 'teacher_to_basis_course':
  260. api_block_anonymous_users(false);
  261. $sortByFirstName = api_sort_by_first_name();
  262. $urlId = api_get_current_access_url_id();
  263. $qb = UserManager::getRepository()->createQueryBuilder('u');
  264. $qb->where(
  265. $qb->expr()->orX(
  266. $qb->expr()->like('u.username', ':q'),
  267. $qb->expr()->like('u.firstname', ':q'),
  268. $qb->expr()->like('u.lastname', ':q')
  269. )
  270. );
  271. if (api_is_multiple_url_enabled()) {
  272. $qb
  273. ->innerJoin('ChamiloCoreBundle:AccessUrlRelUser', 'uru', Join::WITH, 'u.userId = uru.userId')
  274. ->andWhere('uru.accessUrlId = '.$urlId);
  275. }
  276. $qb
  277. ->andWhere('u.status != '.DRH.' AND u.status != '.ANONYMOUS)
  278. ->orderBy(
  279. $sortByFirstName
  280. ? 'u.firstname, u.firstname'
  281. : 'u.firstname, u.lastname'
  282. )
  283. ->setParameter('q', '%'.$_REQUEST['q'].'%');
  284. $users = $qb->getQuery()->getResult();
  285. if (!$users) {
  286. echo json_encode([]);
  287. break;
  288. }
  289. $items = [];
  290. /** @var User $user */
  291. foreach ($users as $user) {
  292. $items[] = [
  293. 'id' => $user->getId(),
  294. 'text' => UserManager::formatUserFullName($user, true),
  295. ];
  296. }
  297. header('Content-Type: application/json');
  298. echo json_encode(['items' => $items]);
  299. break;
  300. default:
  301. echo '';
  302. }
  303. exit;