usergroups.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. $cidReset = true;
  7. require_once __DIR__.'/../inc/global.inc.php';
  8. $this_section = SECTION_PLATFORM_ADMIN;
  9. $usergroup = new UserGroup();
  10. $usergroup->protectScript();
  11. // Add the JS needed to use the jqgrid
  12. $htmlHeadXtra[] = api_get_jqgrid_js();
  13. // setting breadcrumbs
  14. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  15. $userGroupId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  16. // jqgrid will use this URL to do the selects
  17. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_usergroups';
  18. //The order is important you need to check the the $column variable in the model.ajax.php file
  19. $columns = [
  20. get_lang('Name'),
  21. get_lang('Users'),
  22. get_lang('Courses'),
  23. get_lang('Course sessions'),
  24. get_lang('Type'),
  25. get_lang('Detail'),
  26. ];
  27. //Column config
  28. $column_model = [
  29. ['name' => 'name', 'index' => 'name', 'width' => '35', 'align' => 'left'],
  30. ['name' => 'users', 'index' => 'users', 'width' => '15', 'align' => 'left'],
  31. ['name' => 'courses', 'index' => 'courses', 'width' => '15', 'align' => 'left'],
  32. ['name' => 'sessions', 'index' => 'sessions', 'width' => '15', 'align' => 'left'],
  33. ['name' => 'group_type', 'index' => 'group_type', 'width' => '15', 'align' => 'center'],
  34. [
  35. 'name' => 'actions',
  36. 'index' => 'actions',
  37. 'width' => '20',
  38. 'align' => 'center',
  39. 'sortable' => 'false',
  40. 'formatter' => 'action_formatter',
  41. ],
  42. ];
  43. // Autowidth
  44. $extra_params['autowidth'] = 'true';
  45. // Height auto
  46. $extra_params['height'] = 'auto';
  47. $extra_params['sortname'] = 'name';
  48. $extra_params['sortorder'] = 'desc';
  49. // With this function we can add actions to the jgrid
  50. $action_links = 'function action_formatter (cellvalue, options, rowObject) {
  51. return \''
  52. .' <a href="add_users_to_usergroup.php?id=\'+options.rowId+\'">'.Display::return_icon('user_to_class.png', get_lang('Subscribe users to class'), null, ICON_SIZE_MEDIUM).'</a>'
  53. .' <a href="add_courses_to_usergroup.php?id=\'+options.rowId+\'">'.Display::return_icon('course_to_class.png', get_lang('Subscribe class to courses'), null, ICON_SIZE_MEDIUM).'</a>'
  54. .' <a href="add_sessions_to_usergroup.php?id=\'+options.rowId+\'">'.Display::return_icon('sessions_to_class.png', get_lang('SubscribeClassToCourse sessions'), null, ICON_SIZE_MEDIUM).'</a>'
  55. .' <a href="?action=edit&id=\'+options.rowId+\'">'.Display::return_icon('edit.png', get_lang('Edit'), null, ICON_SIZE_SMALL).'</a>'
  56. .' <a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("Please confirm your choice"), ENT_QUOTES))."\'".')) return false;" href="?action=delete&id=\'+options.rowId+\'">'.Display::return_icon('delete.png', get_lang('Delete'), null, ICON_SIZE_SMALL).'</a>\';
  57. }';
  58. $usergroup->showGroupTypeSetting = true;
  59. $content = '';
  60. // Action handling: Adding a note
  61. switch ($action) {
  62. case 'add':
  63. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  64. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Add')];
  65. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  66. api_not_allowed();
  67. }
  68. $form = new FormValidator(
  69. 'usergroup',
  70. 'post',
  71. api_get_self().'?action='.$action
  72. );
  73. $usergroup->setForm($form, 'add');
  74. // Setting the defaults
  75. $form->setDefaults(['visibility' => 2]);
  76. // The validation or display
  77. if ($form->validate()) {
  78. $values = $form->exportValues();
  79. $res = $usergroup->save($values);
  80. if ($res) {
  81. Display::addFlash(Display::return_message(get_lang('Item added'), 'confirmation'));
  82. } else {
  83. Display::addFlash(Display::return_message(
  84. Security::remove_XSS($values['name']).': '.
  85. get_lang('Already exists'),
  86. 'warning'
  87. ));
  88. }
  89. header('Location: '.api_get_self());
  90. exit;
  91. } else {
  92. $content .= '<div class="actions">';
  93. $content .= '<a href="'.api_get_self().'">'.
  94. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM).'</a>';
  95. $content .= '</div>';
  96. $token = Security::get_token();
  97. $form->addElement('hidden', 'sec_token');
  98. $form->setConstants(['sec_token' => $token]);
  99. $content .= $form->returnForm();
  100. }
  101. break;
  102. case 'edit':
  103. $interbreadcrumb[] = ['url' => 'usergroups.php', 'name' => get_lang('Classes')];
  104. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Edit')];
  105. $defaults = $usergroup->get($userGroupId);
  106. $usergroup->protectScript($defaults);
  107. $form = new FormValidator(
  108. 'usergroup',
  109. 'post',
  110. api_get_self().'?action='.$action.'&id='.$userGroupId
  111. );
  112. $usergroup->setForm($form, 'edit', $defaults);
  113. // Setting the form elements
  114. $form->addElement('hidden', 'id', $userGroupId);
  115. // Setting the defaults
  116. $form->setDefaults($defaults);
  117. // The validation or display.
  118. if ($form->validate()) {
  119. $values = $form->getSubmitValues();
  120. $res = $usergroup->update($values);
  121. if ($res) {
  122. Display::addFlash(Display::return_message(get_lang('Update successful'), 'confirmation'));
  123. } else {
  124. Display::addFlash(Display::return_message(
  125. Security::remove_XSS($values['name']).': '.
  126. get_lang('Already exists'),
  127. 'warning'
  128. ));
  129. }
  130. header('Location: '.api_get_self());
  131. exit;
  132. } else {
  133. $content .= '<div class="actions">';
  134. $content .= '<a href="'.api_get_self().'">'.Display::return_icon(
  135. 'back.png',
  136. get_lang('Back'),
  137. '',
  138. ICON_SIZE_MEDIUM
  139. ).'</a>';
  140. $content .= '</div>';
  141. $content .= $form->returnForm();
  142. }
  143. break;
  144. case 'delete':
  145. $defaults = $usergroup->get($userGroupId);
  146. $usergroup->protectScript($defaults);
  147. $res = $usergroup->delete($userGroupId);
  148. if ($res) {
  149. Display::addFlash(Display::return_message(get_lang('Deleted'), 'confirmation'));
  150. }
  151. header('Location: '.api_get_self());
  152. exit;
  153. break;
  154. default:
  155. $interbreadcrumb[] = ['url' => '#', 'name' => get_lang('Classes')];
  156. $content = $usergroup->returnGrid();
  157. break;
  158. }
  159. // The header.
  160. Display::display_header();
  161. ?>
  162. <script>
  163. $(function() {
  164. <?php
  165. // grid definition see the $usergroup>display() function
  166. echo Display::grid_js(
  167. 'usergroups',
  168. $url,
  169. $columns,
  170. $column_model,
  171. $extra_params,
  172. [],
  173. $action_links,
  174. true
  175. );
  176. ?>
  177. });
  178. </script>
  179. <?php
  180. echo $content;
  181. Display::display_footer();