usergroup_users.php 12 KB

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