group_list.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. @author Bart Mollet
  5. * @package chamilo.admin
  6. */
  7. $cidReset = true;
  8. require_once '../inc/global.inc.php';
  9. $this_section = SECTION_PLATFORM_ADMIN;
  10. api_protect_admin_script(true);
  11. /**
  12. * Get the total number of users on the platform
  13. * @see SortableTable#get_total_number_of_items()
  14. */
  15. function get_number_of_groups() {
  16. $group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
  17. $sql = "SELECT COUNT(g.id) AS total_number_of_items FROM $group_table g";
  18. // adding the filter to see the user's only of the current access_url
  19. /*
  20. global $_configuration;
  21. if ((api_is_platform_admin() || api_is_session_admin()) && $_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  22. $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  23. $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
  24. }
  25. */
  26. if (isset($_GET['keyword'])) {
  27. $keyword = Database::escape_string(trim($_GET['keyword']));
  28. $sql .= " WHERE (g.name LIKE '%".$keyword."%' OR g.description LIKE '%".$keyword."%' OR g.url LIKE '%".$keyword."%' )";
  29. }
  30. // adding the filter to see the user's only of the current access_url
  31. /*
  32. if ((api_is_platform_admin() || api_is_session_admin()) && $_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  33. $sql.= " AND url_rel_user.access_url_id=".api_get_current_access_url_id();
  34. } */
  35. $res = Database::query($sql);
  36. $obj = Database::fetch_object($res);
  37. return $obj->total_number_of_items;
  38. }
  39. /**
  40. * Get the users to display on the current page (fill the sortable-table)
  41. * @param int offset of first user to recover
  42. * @param int Number of users to get
  43. * @param int Column to sort on
  44. * @param string Order (ASC,DESC)
  45. * @see SortableTable#get_table_data($from)
  46. */
  47. function get_group_data($from, $number_of_items, $column, $direction) {
  48. $group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
  49. $sql = "SELECT
  50. g.id AS col0,
  51. g.name AS col1,
  52. g.description AS col2,
  53. g.visibility AS col3,
  54. g.id AS col4
  55. FROM $group_table g ";
  56. // adding the filter to see the user's only of the current access_url
  57. /* global $_configuration;
  58. if ((api_is_platform_admin() || api_is_session_admin()) && $_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  59. $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  60. $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
  61. } */
  62. if (isset($_GET['keyword'])) {
  63. $keyword = Database::escape_string(trim($_GET['keyword']));
  64. $sql .= " WHERE (g.name LIKE '%".$keyword."%' OR g.description LIKE '%".$keyword."%' OR g.url LIKE '%".$keyword."%' )";
  65. }
  66. /*
  67. // adding the filter to see the user's only of the current access_url
  68. if ((api_is_platform_admin() || api_is_session_admin()) && $_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  69. $sql.= " AND url_rel_user.access_url_id=".api_get_current_access_url_id();
  70. } */
  71. if (!in_array($direction, array('ASC', 'DESC'))) {
  72. $direction = 'ASC';
  73. }
  74. $column = intval($column);
  75. $from = intval($from);
  76. $number_of_items = intval($number_of_items);
  77. $sql .= " ORDER BY col$column $direction ";
  78. $sql .= " LIMIT $from,$number_of_items";
  79. $res = Database::query($sql);
  80. $users = array();
  81. $t = time();
  82. // Status
  83. $status = array();
  84. $status[GROUP_PERMISSION_OPEN] = get_lang('Open');
  85. $status[GROUP_PERMISSION_CLOSED] = get_lang('Closed');
  86. $result = Database::select(
  87. 'tGroupRelGroup.group_id, tGroup.id, tGroup.name',
  88. Database::get_main_table(TABLE_MAIN_GROUP_REL_GROUP).
  89. " AS tGroupRelGroup RIGHT JOIN ".Database::get_main_table(TABLE_MAIN_GROUP).
  90. " AS tGroup ON tGroupRelGroup.subgroup_id = tGroup.id"
  91. );
  92. $groupRelations = array();
  93. foreach ($result as $row) {
  94. $groupRelations[$row['id']] = $row;
  95. }
  96. $groups = array();
  97. while ($group = Database::fetch_row($res)) {
  98. $name = null;
  99. $id = $group[0];
  100. // Loops while the current group is a subgroup
  101. while (isset($groupRelations[$id]['group_id'])) {
  102. $name = $name ?
  103. $groupRelations[$id]['name'] . " > " . $name :
  104. $groupRelations[$id]['name'];
  105. $id = $groupRelations[$id]['group_id'];
  106. }
  107. // The base group
  108. $name = $name ?
  109. $groupRelations[$id]['name'] . " > " . $name :
  110. $groupRelations[$id]['name'];
  111. $group[3] = $status[$group[3]];
  112. $group['1'] = '<a href="'.api_get_path(WEB_CODE_PATH).'social/group_view.php?id='.$group['0'].'">'.$name.'</a>';
  113. $groups[] = $group;
  114. }
  115. return $groups;
  116. }
  117. function get_recent_group_data($from = 0, $number_of_items = 5, $column, $direction) {
  118. $group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
  119. $sql = "SELECT
  120. g.id AS col0,
  121. g.name AS col1,
  122. g.description AS col2,
  123. g.visibility AS col3,
  124. g.id AS col4
  125. FROM $group_table g ";
  126. // adding the filter to see the user's only of the current access_url
  127. /* global $_configuration;
  128. if ((api_is_platform_admin() || api_is_session_admin()) && $_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  129. $access_url_rel_user_table= Database :: get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  130. $sql.= " INNER JOIN $access_url_rel_user_table url_rel_user ON (u.user_id=url_rel_user.user_id)";
  131. } */
  132. if (isset($_GET['keyword'])) {
  133. $keyword = Database::escape_string(trim($_GET['keyword']));
  134. $sql .= " WHERE (g.name LIKE '%".$keyword."%' OR g.description LIKE '%".$keyword."%' OR g.url LIKE '%".$keyword."%' )";
  135. }
  136. /*
  137. // adding the filter to see the user's only of the current access_url
  138. if ((api_is_platform_admin() || api_is_session_admin()) && $_configuration['multiple_access_urls'] && api_get_current_access_url_id()!=-1) {
  139. $sql.= " AND url_rel_user.access_url_id=".api_get_current_access_url_id();
  140. } */
  141. if (!in_array($direction, array('ASC', 'DESC'))) {
  142. $direction = 'ASC';
  143. }
  144. $column = intval($column);
  145. $from = intval($from);
  146. $number_of_items = intval($number_of_items);
  147. $sql .= " ORDER BY col$column $direction ";
  148. $sql .= " LIMIT $from,$number_of_items";
  149. $res = Database::query($sql);
  150. $users = array();
  151. $t = time();
  152. while ($group = Database::fetch_row($res)) {
  153. // forget about the expiration date field
  154. $groups[] = $group;
  155. }
  156. return $groups;
  157. }
  158. /**
  159. * Build the modify-column of the table
  160. * @param int The user id
  161. * @param string URL params to add to table links
  162. * @param array Row of elements to alter
  163. * @return string Some HTML-code with modify-buttons
  164. */
  165. function modify_filter($group_id, $url_params, $row) {
  166. global $charset;
  167. $result = null;
  168. if (api_is_platform_admin()) {
  169. $result .= '<a href="'.api_get_path(WEB_CODE_PATH).'admin/add_users_to_group.php?id='.$group_id.'">'.Display::return_icon('subscribe_users_social_network.png', get_lang('AddUsersToGroup'), '', ICON_SIZE_SMALL).'</a>';
  170. $result .= '<a href="group_edit.php?id='.$group_id.'">'.Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>&nbsp;&nbsp;';
  171. $result .= '<a href="group_list.php?action=delete_group&group_id='.$group_id.'&'.$url_params.'&sec_token='.$_SESSION['sec_token'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset))."'".')) return false;">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Build the active-column of the table to lock or unlock a certain user
  177. * lock = the user can no longer use this account
  178. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  179. * @param int $active the current state of the account
  180. * @param int $user_id The user id
  181. * @param string $url_params
  182. * @return string Some HTML-code with the lock/unlock button
  183. */
  184. function active_filter($active, $url_params, $row)
  185. {
  186. $_user = api_get_user_info();
  187. if ($active == '1') {
  188. $action = 'lock';
  189. $image = 'right';
  190. } elseif ($active == '-1') {
  191. $action = 'edit';
  192. $image = 'expired';
  193. } elseif ($active == '0') {
  194. $action = 'unlock';
  195. $image = 'wrong';
  196. }
  197. if ($action == 'edit') {
  198. $result = Display::return_icon($image.'.gif', get_lang('AccountExpired'));
  199. } elseif ($row['0'] <> $_user['user_id']) { // you cannot lock yourself out otherwise you could disable all the accounts including your own => everybody is locked out and nobody can change it anymore.
  200. $result = '<a href="user_list.php?action='.$action.'&user_id='.$row['0'].'&'.$url_params.'&sec_token='.$_SESSION['sec_token'].'">'.Display::return_icon($image.'.gif', get_lang(ucfirst($action))).'</a>';
  201. }
  202. return $result;
  203. }
  204. /**
  205. * Lock or unlock a user
  206. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  207. * @param int $status, do we want to lock the user ($status=lock) or unlock it ($status=unlock)
  208. * @param int $user_id The user id
  209. * @return language variable
  210. */
  211. function lock_unlock_user($status, $user_id) {
  212. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  213. if ($status == 'lock') {
  214. $status_db = '0';
  215. $return_message = get_lang('UserLocked');
  216. }
  217. if ($status == 'unlock') {
  218. $status_db = '1';
  219. $return_message = get_lang('UserUnlocked');
  220. }
  221. if (($status_db == '1' OR $status_db == '0') AND is_numeric($user_id)) {
  222. $sql = "UPDATE $user_table SET active=".intval($status_db)."
  223. WHERE user_id=".intval($user_id)."";
  224. $result = Database::query($sql);
  225. }
  226. if ($result) {
  227. return $return_message;
  228. }
  229. }
  230. /**
  231. * Instead of displaying the integer of the status, we give a translation for the status
  232. *
  233. * @param integer $status
  234. * @return string translation
  235. *
  236. * @version march 2008
  237. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  238. */
  239. function status_filter($status) {
  240. $statusname = api_get_status_langvars();
  241. return $statusname[$status];
  242. }
  243. // INIT SECTION
  244. $action = isset($_GET["action"]) ? $_GET["action"] : null;
  245. if (isset($_GET['search']) && $_GET['search'] == 'advanced') {
  246. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  247. $interbreadcrumb[] = array("url" => 'group_list.php', "name" => get_lang('GroupList'));
  248. $tool_name = get_lang('SearchAUser');
  249. Display :: display_header($tool_name);
  250. //api_display_tool_title($tool_name);
  251. $form = new FormValidator('advanced_search', 'get');
  252. $form->addElement('header', '', $tool_name);
  253. $form->addText('keyword_firstname', get_lang('FirstName'), false);
  254. $form->addText('keyword_lastname', get_lang('LastName'), false);
  255. $form->addText('keyword_username', get_lang('LoginName'), false);
  256. $form->addText('keyword_email', get_lang('Email'), false);
  257. $form->addText('keyword_officialcode', get_lang('OfficialCode'), false);
  258. $status_options = array();
  259. $status_options['%'] = get_lang('All');
  260. $status_options[STUDENT] = get_lang('Student');
  261. $status_options[COURSEMANAGER] = get_lang('Teacher');
  262. $status_options[SESSIONADMIN] = get_lang('Administrator'); //
  263. $form->addElement('select', 'keyword_status', get_lang('Status'), $status_options);
  264. $active_group = array();
  265. $active_group[] = $form->createElement('checkbox', 'keyword_active', '', get_lang('Active'));
  266. $active_group[] = $form->createElement('checkbox', 'keyword_inactive', '', get_lang('Inactive'));
  267. $form->addGroup($active_group, '', get_lang('ActiveAccount'), '<br/>', false);
  268. $form->addButtonSearch(get_lang('SearchUsers'));
  269. $defaults['keyword_active'] = 1;
  270. $defaults['keyword_inactive'] = 1;
  271. $form->setDefaults($defaults);
  272. $form->display();
  273. } else {
  274. $interbreadcrumb[] = array("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  275. $tool_name = get_lang('GroupList');
  276. Display :: display_header($tool_name, "");
  277. //api_display_tool_title($tool_name);
  278. if (isset($_GET['action'])) {
  279. $check = Security::check_token('get');
  280. if ($check) {
  281. switch ($_GET['action']) {
  282. case 'show_message' :
  283. if (!empty($_GET['warn'])) {
  284. // to prevent too long messages
  285. if ($_GET['warn'] == 'session_message') {
  286. $_GET['warn'] = $_SESSION['session_message_import_users'];
  287. }
  288. Display::display_warning_message(urldecode($_GET['warn']), false);
  289. }
  290. if (!empty($_GET['message'])) {
  291. Display :: display_confirmation_message(stripslashes($_GET['message']));
  292. }
  293. break;
  294. case 'delete_group' :
  295. if (api_is_platform_admin()) {
  296. if (GroupPortalManager :: delete($_GET['group_id'])) {
  297. Display :: display_confirmation_message(get_lang('GroupDeleted'));
  298. } else {
  299. Display :: display_error_message(get_lang('CannotDeleteGroup'));
  300. }
  301. }
  302. break;
  303. case 'lock' :
  304. $message = lock_unlock_user('lock', $_GET['user_id']);
  305. Display :: display_normal_message($message);
  306. break;
  307. case 'unlock';
  308. $message = lock_unlock_user('unlock', $_GET['user_id']);
  309. Display :: display_normal_message($message);
  310. break;
  311. }
  312. Security::clear_token();
  313. }
  314. }
  315. if (isset($_POST['action'])) {
  316. $check = Security::check_token('get');
  317. if ($check) {
  318. switch ($_POST['action']) {
  319. case 'delete' :
  320. if (api_is_platform_admin()) {
  321. $number_of_selected_groups = count($_POST['id']);
  322. $number_of_deleted_groups = 0;
  323. foreach ($_POST['id'] as $index => $group_id) {
  324. if (GroupPortalManager :: delete($group_id)) {
  325. $number_of_deleted_groups++;
  326. }
  327. }
  328. }
  329. if ($number_of_selected_groups == $number_of_deleted_groups) {
  330. Display :: display_confirmation_message(get_lang('SelectedGroupsDeleted'));
  331. } else {
  332. Display :: display_error_message(get_lang('SomeGroupsNotDeleted'));
  333. }
  334. break;
  335. }
  336. Security::clear_token();
  337. }
  338. }
  339. // Create a search-box
  340. $form = new FormValidator('search_simple', 'get', '', '', null, false);
  341. $renderer = & $form->defaultRenderer();
  342. $renderer->setCustomElementTemplate('<span>{element}</span> ');
  343. $form->addElement('text', 'keyword', get_lang('Keyword'));
  344. $form->addButtonSearch(get_lang('Search'));
  345. echo '<div class="actions" style="width:100%;">';
  346. if (api_is_platform_admin()) {
  347. echo '<span style="float:right;">'.
  348. '<a href="'.api_get_path(WEB_CODE_PATH).'admin/group_add.php">'.Display::return_icon('create_group_social_network.png', get_lang('AddGroups'), '', ICON_SIZE_MEDIUM).'</a>'.
  349. '</span>';
  350. }
  351. $form->display();
  352. echo '</div>';
  353. if (isset($_GET['keyword'])) {
  354. $parameters = array('keyword' => Security::remove_XSS($_GET['keyword']));
  355. }
  356. // Create a sortable table with user-data
  357. $parameters['sec_token'] = Security::get_token();
  358. // get the list of all admins to mark them in the users list
  359. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  360. $sql_admin = "SELECT user_id FROM $admin_table";
  361. $res_admin = Database::query($sql_admin);
  362. $_admins_list = array();
  363. while ($row_admin = Database::fetch_row($res_admin)) {
  364. $_admins_list[] = $row_admin[0];
  365. }
  366. $table = new SortableTable('group_list', 'get_number_of_groups', 'get_group_data', 2);
  367. $table->set_additional_parameters($parameters);
  368. $table->set_header(0, '', false);
  369. $table->set_header(1, get_lang('Name'));
  370. $table->set_header(2, get_lang('Description'));
  371. $table->set_header(3, get_lang('Visibility'));
  372. $table->set_header(4, '', false);
  373. $table->set_column_filter(4, 'modify_filter');
  374. //$table->set_column_filter(6, 'status_filter');
  375. //$table->set_column_filter(7, 'active_filter');
  376. //$table->set_column_filter(8, 'modify_filter');
  377. if (api_is_platform_admin())
  378. $table->set_form_actions(array('delete' => get_lang('DeleteFromPlatform')));
  379. $table->display();
  380. }
  381. Display :: display_footer();