usergroup_users.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. $this_section = SECTION_PLATFORM_ADMIN;
  8. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  9. $usergroup = new UserGroup();
  10. $userGroupInfo = $usergroup->get($id);
  11. if (empty($userGroupInfo)) {
  12. api_not_allowed(true);
  13. }
  14. $usergroup->protectScript($userGroupInfo, true, true);
  15. $allowEdit = api_is_platform_admin() || isset($userGroupInfo['author_id']) && $userGroupInfo['author_id'] == api_get_user_id();
  16. $calendarPlugin = null;
  17. if ($allowEdit && api_get_plugin_setting('learning_calendar', 'enabled') === 'true') {
  18. $calendarPlugin = LearningCalendarPlugin::create();
  19. }
  20. // Add the JS needed to use the jqgrid
  21. $htmlHeadXtra[] = api_get_jqgrid_js();
  22. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : null;
  23. $userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0;
  24. $calendarId = isset($_REQUEST['calendar_id']) ? (int) $_REQUEST['calendar_id'] : 0;
  25. $courseInfo = api_get_course_info();
  26. if (empty($courseInfo)) {
  27. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  28. } else {
  29. $interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'user/class.php?'.api_get_cidreq(), 'name' => get_lang('Classes')];
  30. }
  31. $interbreadcrumb[] = ['url' => '#', 'name' => $userGroupInfo['name']];
  32. if (!empty($action)) {
  33. $usergroup->protectScript($userGroupInfo);
  34. }
  35. switch ($action) {
  36. case 'add_calendar':
  37. $form = new FormValidator(
  38. 'add_calendar',
  39. 'post',
  40. api_get_self().'?id='.$id.'&user_id='.$userId.'&action=add_calendar'
  41. );
  42. $userInfo = api_get_user_info($userId);
  43. $form->addHeader($userInfo['complete_name']);
  44. $calendarPlugin->getAddUserToCalendarForm($form);
  45. $form->addButtonSave(get_lang('Add'));
  46. $form->display();
  47. if ($form->validate()) {
  48. $calendarId = $form->getSubmitValue('calendar_id');
  49. if (!empty($calendarId)) {
  50. $calendarPlugin->addUserToCalendar($calendarId, $userId);
  51. Display::addFlash(Display::return_message(get_lang('Added'), 'confirmation'));
  52. header('Location: '.api_get_self().'?id='.$id);
  53. exit;
  54. }
  55. }
  56. exit;
  57. break;
  58. case 'edit_calendar':
  59. $form = new FormValidator(
  60. 'add_calendar',
  61. 'post',
  62. api_get_self().'?id='.$id.'&user_id='.$userId.'&action=edit_calendar&calendar_id='.$calendarId
  63. );
  64. $userInfo = api_get_user_info($userId);
  65. $form->addHeader($userInfo['complete_name']);
  66. $calendarPlugin->getAddUserToCalendarForm($form);
  67. $form->setDefaults(['calendar_id' => $calendarId]);
  68. $form->addButtonSave(get_lang('Update'));
  69. $form->display();
  70. if ($form->validate()) {
  71. $calendarId = $form->getSubmitValue('calendar_id');
  72. if (!empty($calendarId)) {
  73. $calendarPlugin->updateUserToCalendar($calendarId, $userId);
  74. Display::addFlash(Display::return_message(get_lang('Added'), 'confirmation'));
  75. header('Location: '.api_get_self().'?id='.$id);
  76. exit;
  77. }
  78. }
  79. exit;
  80. break;
  81. case 'delete':
  82. $res = $usergroup->delete_user_rel_group($_GET['user_id'], $_GET['id']);
  83. Display::addFlash(Display::return_message(get_lang('Deleted'), 'confirmation'));
  84. header('Location: '.api_get_self().'?id='.$id);
  85. exit;
  86. break;
  87. case 'create_control_point':
  88. $value = isset($_GET['value']) ? (int) $_GET['value'] : 0;
  89. $calendarPlugin->addControlPoint($userId, $value);
  90. Display::addFlash(
  91. Display::return_message($calendarPlugin->get_lang('ControlPointAdded'), 'confirmation')
  92. );
  93. header('Location: '.api_get_self().'?id='.$id);
  94. exit;
  95. case 'add_multiple_users_to_calendar':
  96. $userList = isset($_REQUEST['user_list']) ? explode(',', $_REQUEST['user_list']) : 0;
  97. foreach ($userList as $userId) {
  98. $isAdded = $calendarPlugin->addUserToCalendar($calendarId, $userId);
  99. if (!$isAdded) {
  100. $isAdded = $calendarPlugin->updateUserToCalendar($calendarId, $userId);
  101. }
  102. }
  103. Display::addFlash(
  104. Display::return_message(get_lang('Added'), 'confirmation')
  105. );
  106. header('Location: '.api_get_self().'?id='.$id);
  107. exit;
  108. break;
  109. }
  110. Display::display_header();
  111. // jqgrid will use this URL to do the selects
  112. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups_users&id='.$id;
  113. // The order is important you need to check the the $column variable in the model.ajax.php file
  114. $columns = [
  115. get_lang('Name'),
  116. get_lang('Actions'),
  117. ];
  118. // Column config
  119. $column_model = [
  120. ['name' => 'name', 'index' => 'name', 'width' => '35', 'align' => 'left', 'sortable' => 'false'],
  121. [
  122. 'name' => 'actions',
  123. 'index' => 'actions',
  124. 'width' => '20',
  125. 'align' => 'center',
  126. 'sortable' => 'false',
  127. 'formatter' => 'action_formatter',
  128. ],
  129. ];
  130. if ($calendarPlugin) {
  131. $columns = [
  132. get_lang('Name'),
  133. get_lang('Calendar'),
  134. get_lang('ClassroomActivity'),
  135. get_lang('TimeSpentByStudentsInCourses'),
  136. $calendarPlugin->get_lang('NumberDaysAccumulatedInCalendar'),
  137. $calendarPlugin->get_lang('DifferenceOfDaysAndCalendar'),
  138. get_lang('Actions'),
  139. ];
  140. // Column config
  141. $column_model = [
  142. ['name' => 'name', 'index' => 'name', 'width' => '35', 'align' => 'left', 'sortable' => 'false'],
  143. [
  144. 'name' => 'calendar',
  145. 'index' => 'calendar',
  146. 'width' => '35',
  147. 'align' => 'left',
  148. 'sortable' => 'false',
  149. 'formatter' => 'extra_formatter',
  150. ],
  151. [
  152. 'name' => 'gradebook_items',
  153. 'index' => 'gradebook_items',
  154. 'width' => '35',
  155. 'align' => 'left',
  156. 'sortable' => 'false',
  157. ],
  158. ['name' => 'time_spent', 'index' => 'time_spent', 'width' => '35', 'align' => 'left', 'sortable' => 'false'],
  159. [
  160. 'name' => 'lp_day_completed',
  161. 'index' => 'lp_day_completed',
  162. 'width' => '35',
  163. 'align' => 'left',
  164. 'sortable' => 'false',
  165. ],
  166. ['name' => 'days_diff', 'index' => 'days_diff', 'width' => '35', 'align' => 'left', 'sortable' => 'false'],
  167. [
  168. 'name' => 'actions',
  169. 'index' => 'actions',
  170. 'width' => '20',
  171. 'align' => 'center',
  172. 'sortable' => 'false',
  173. 'formatter' => 'action_formatter',
  174. ],
  175. ];
  176. }
  177. // Autowidth
  178. $extraParams['autowidth'] = 'true';
  179. // height auto
  180. $extraParams['height'] = 'auto';
  181. $extraParams['sortname'] = 'name';
  182. $extraParams['sortorder'] = 'desc';
  183. $extraParams['multiselect'] = $allowEdit;
  184. $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), null, ICON_SIZE_SMALL);
  185. $urlStats = api_get_path(WEB_CODE_PATH);
  186. $reportingIcon = Display::return_icon('statistics.png', get_lang('Reporting'), '', ICON_SIZE_SMALL);
  187. $controlPoint = Display::return_icon('add.png', get_lang('ControlPoint'), '', ICON_SIZE_SMALL);
  188. $link = '';
  189. if ($calendarPlugin) {
  190. $link = '<a href="'.$urlStats.'admin/usergroup_users.php?action=create_control_point&value=\'+value+\'&id='.$id.'&user_id=\'+options.rowId+\'">'.$controlPoint.'</a>';
  191. }
  192. $deleteButton = '';
  193. if ($allowEdit) {
  194. $deleteButton = '<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."\'".')) return false;" href="?id='.$id.'&action=delete&user_id=\'+options.rowId+\'">'.$deleteIcon.'</a>';
  195. }
  196. //return \'<a href="session_edit.php?page=resume_session.php&id=\'+options.rowId+\'">'.Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>'.
  197. // With this function we can add actions to the jgrid
  198. $action_links = '
  199. function action_formatter(cellvalue, options, rowObject) {
  200. var value = rowObject[5];
  201. return \''.
  202. '&nbsp;'.$link.
  203. '&nbsp;<a href="'.$urlStats.'mySpace/myStudents.php?student=\'+options.rowId+\'">'.$reportingIcon.'</a>'.
  204. ' '.$deleteButton.' \';
  205. }
  206. function extra_formatter(cellvalue, options, rowObject) {
  207. var calendarName = rowObject[1];
  208. var calendarId = rowObject[7];
  209. if (calendarName == "") {
  210. return \'<a href="'.
  211. api_get_path(WEB_CODE_PATH).'admin/usergroup_users.php?user_id=\'+options.rowId+\'&id='.$id.'&action=add_calendar&width=700" class="btn btn-primary ajax">'.get_lang('Add').'</a>\';
  212. } else {
  213. return \' \'+calendarName+\' <a href="'.
  214. api_get_path(WEB_CODE_PATH).'admin/usergroup_users.php?calendar_id=\'+calendarId+\'&user_id=\'+options.rowId+\'&id='.$id.'&action=edit_calendar&width=700" class="btn btn-primary ajax"> '.get_lang('Edit').'</a>\';
  215. }
  216. return calendarName;
  217. return \''.
  218. '&nbsp;<a href="'.$urlStats.'mySpace/myStudents.php?student=\'+options.rowId+\'">'.Display::return_icon('statistics.png', get_lang('Reporting'), '', ICON_SIZE_SMALL).'</a>'.
  219. ' <a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES))."\'".')) return false;" href="?id='.$id.'&action=delete&user_id=\'+options.rowId+\'">'.$deleteIcon.'</a>\';
  220. }';
  221. $deleteUrl = api_get_path(WEB_AJAX_PATH).'usergroup.ajax.php?a=delete_user_in_usergroup&group_id='.$id;
  222. if ($calendarPlugin) {
  223. $form = new FormValidator(
  224. 'add_multiple_calendar',
  225. 'post',
  226. api_get_self().'?id='.$id.'&action=add_multiple_users_to_calendar'
  227. );
  228. $calendarPlugin->getAddUserToCalendarForm($form);
  229. $form->addHidden('user_list', '');
  230. $form->addButtonSave(get_lang('Add'));
  231. }
  232. ?>
  233. <script>
  234. $(function() {
  235. <?php
  236. // grid definition see the $usergroup>display() function
  237. echo Display::grid_js(
  238. 'usergroups',
  239. $url,
  240. $columns,
  241. $column_model,
  242. $extraParams,
  243. [],
  244. $action_links,
  245. true
  246. );
  247. ?>
  248. $("#usergroups").jqGrid(
  249. "navGrid",
  250. "#usergroups_pager",
  251. { edit: false, add: false, del: <?php echo $allowEdit ? 'true' : 'false'; ?>, search: false},
  252. { height:280, reloadAfterSubmit:false }, // edit options
  253. { height:280, reloadAfterSubmit:false }, // add options
  254. { reloadAfterSubmit:false, url: "<?php echo $deleteUrl; ?>" }, // del options
  255. { width:500 } // search options
  256. )
  257. <?php if ($calendarPlugin) { ?>
  258. .navButtonAdd('#usergroups_pager',{
  259. caption:"<?php echo addslashes($calendarPlugin->get_lang('UpdateCalendar')); ?>",
  260. buttonicon:"ui-icon ui-icon-plus",
  261. onClickButton: function(a) {
  262. var userIdList = $("#usergroups").jqGrid('getGridParam', 'selarrrow');
  263. if (userIdList.length) {
  264. $(".modal-body #add_multiple_calendar_user_list").val(userIdList);
  265. $('#myModal').modal();
  266. } else {
  267. alert("<?php echo addslashes(get_lang('SelectStudents')); ?>");
  268. }
  269. },
  270. position:"last"
  271. })
  272. <?php } ?>
  273. ;
  274. });
  275. </script>
  276. <?php if ($calendarPlugin) { ?>
  277. <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  278. <div class="modal-dialog" role="document">
  279. <div class="modal-content">
  280. <div class="modal-header">
  281. <button type="button" class="close" data-dismiss="modal" aria-label="Close">
  282. <span aria-hidden="true">&times;</span></button>
  283. <h4 class="modal-title" id="myModalLabel">
  284. <?php echo $calendarPlugin->get_lang('AddMultipleUsersToCalendar'); ?>
  285. </h4>
  286. </div>
  287. <div class="modal-body">
  288. <?php echo $form->display(); ?>
  289. </div>
  290. </div>
  291. </div>
  292. </div>
  293. <?php } ?>
  294. <?php
  295. $usergroup->showGroupTypeSetting = true;
  296. // Action handling: Adding a note
  297. if ($allowEdit && $action === 'delete' && is_numeric($_GET['id'])) {
  298. $res = $usergroup->delete_user_rel_group($_GET['user_id'], $_GET['id']);
  299. Display::addFlash(Display::return_message(get_lang('Deleted'), 'confirmation'));
  300. header('Location: '.api_get_self().'?id='.$id);
  301. exit;
  302. }
  303. $usergroup->displayToolBarUserGroupUsers();
  304. Display::display_footer();