group_category.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.group
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. $this_section = SECTION_COURSES;
  8. $current_course_tool = TOOL_GROUP;
  9. // Notice for unauthorized people.
  10. api_protect_course_script(true);
  11. if (api_get_setting('allow_group_categories') == 'false') {
  12. api_not_allowed(true);
  13. }
  14. $sessionId = api_get_session_id();
  15. if (!api_is_allowed_to_edit(false, true) ||
  16. !(isset($_GET['id']) ||
  17. isset($_POST['id']) ||
  18. isset($_GET['action']) ||
  19. isset($_POST['action']))
  20. ) {
  21. api_not_allowed(true);
  22. }
  23. if (!empty($sessionId)) {
  24. api_not_allowed(true);
  25. }
  26. /**
  27. * Function to check the given max number of members per group.
  28. */
  29. function check_max_number_of_members($value)
  30. {
  31. $max_member_no_limit = $value['max_member_no_limit'];
  32. if ($max_member_no_limit == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  33. return true;
  34. }
  35. $max_member = $value['max_member'];
  36. return is_numeric($max_member);
  37. }
  38. /**
  39. * Function to check the number of groups per user.
  40. *
  41. * @param $value
  42. *
  43. * @return bool
  44. */
  45. function check_groups_per_user($value)
  46. {
  47. $groups_per_user = (int) $value['groups_per_user'];
  48. if (isset($_POST['id']) &&
  49. $groups_per_user != GroupManager::GROUP_PER_MEMBER_NO_LIMIT &&
  50. GroupManager::get_current_max_groups_per_user($_POST['id']) > $groups_per_user) {
  51. return false;
  52. }
  53. return true;
  54. }
  55. if (isset($_GET['id'])) {
  56. $category = GroupManager::get_category($_GET['id']);
  57. $nameTools = get_lang('Edit group category').': '.$category['title'];
  58. } else {
  59. $nameTools = get_lang('Add category');
  60. // Default values for new category
  61. $category = [
  62. 'groups_per_user' => 1,
  63. 'doc_state' => GroupManager::TOOL_PRIVATE,
  64. 'work_state' => GroupManager::TOOL_PRIVATE,
  65. 'wiki_state' => GroupManager::TOOL_PRIVATE,
  66. 'chat_state' => GroupManager::TOOL_PRIVATE,
  67. 'calendar_state' => GroupManager::TOOL_PRIVATE,
  68. 'announcements_state' => GroupManager::TOOL_PRIVATE,
  69. 'forum_state' => GroupManager::TOOL_PRIVATE,
  70. 'max_student' => 0,
  71. 'document_access' => 0,
  72. ];
  73. }
  74. $htmlHeadXtra[] = '<script>
  75. $(function() {
  76. $("#max_member").on("focus", function() {
  77. $("#max_member_selected").attr("checked", true);
  78. });
  79. });
  80. </script>';
  81. $interbreadcrumb[] = ['url' => 'group.php?'.api_get_cidreq(), 'name' => get_lang('Groups')];
  82. $course_id = api_get_course_int_id();
  83. // Build the form
  84. if (isset($_GET['id'])) {
  85. // Update settings of existing category
  86. $action = 'update_settings';
  87. $form = new FormValidator(
  88. 'group_category',
  89. 'post',
  90. api_get_self().'?id='.$category['id'].'&'.api_get_cidreq()
  91. );
  92. $form->addElement('hidden', 'id');
  93. } else {
  94. // Checks if the field was created in the table Category. It creates it if is neccesary
  95. $table_category = Database::get_course_table(TABLE_GROUP_CATEGORY);
  96. if (!Database::query("SELECT wiki_state FROM $table_category WHERE c_id = $course_id")) {
  97. $sql = "ALTER TABLE $table_category ADD wiki_state tinyint(3) UNSIGNED NOT NULL default '1'
  98. WHERE c_id = $course_id";
  99. Database::query($sql);
  100. }
  101. // Create a new category
  102. $action = 'add_category';
  103. $form = new FormValidator('group_category');
  104. }
  105. $form->addElement('header', $nameTools);
  106. $form->addElement('html', '<div class="row"><div class="col-md-6">');
  107. $form->addText('title', get_lang('Title'));
  108. // Groups per user
  109. $possible_values = [];
  110. for ($i = 1; $i <= 10; $i++) {
  111. $possible_values[$i] = $i;
  112. }
  113. $possible_values[GroupManager::GROUP_PER_MEMBER_NO_LIMIT] = get_lang('All');
  114. $group = [
  115. $form->createElement('select', 'groups_per_user', null, $possible_values, ['id' => 'groups_per_user']),
  116. $form->createElement('static', null, null, get_lang(' groups')),
  117. ];
  118. $form->addGroup($group, 'limit_group', get_lang('A user can be member of maximum'), null, false);
  119. $form->addRule('limit_group', get_lang('The maximum number of groups per user you submitted is invalid. There are now users who are subscribed in more groups than the number you propose.'), 'callback', 'check_groups_per_user');
  120. $form->addElement('html', '</div>');
  121. $form->addElement('html', '<div class="col-md-6">');
  122. $form->addElement('textarea', 'description', get_lang('Description'), ['rows' => 6]);
  123. $form->addElement('html', '</div>');
  124. $form->addElement('html', '<div class="col-md-12">');
  125. $form->addElement('header', '');
  126. $form->addElement('html', '</div>');
  127. $form->addElement('html', '<div class="col-md-6">');
  128. // Members per group
  129. $group = [
  130. $form->createElement(
  131. 'radio',
  132. 'max_member_no_limit',
  133. get_lang('Limit'),
  134. get_lang('No limitation'),
  135. GroupManager::MEMBER_PER_GROUP_NO_LIMIT
  136. ),
  137. $form->createElement(
  138. 'radio',
  139. 'max_member_no_limit',
  140. null,
  141. get_lang('Maximum number of members'),
  142. 1,
  143. ['id' => 'max_member_selected']
  144. ),
  145. $form->createElement('text', 'max_member', null, ['class' => 'span1', 'id' => 'max_member']),
  146. $form->createElement('static', null, null, ' '.get_lang('seats (optional)')),
  147. ];
  148. $form->addGroup($group, 'max_member_group', get_lang('Limit'), null, false);
  149. $form->addRule('max_member_group', get_lang('Please enter a valid number for the maximum number of members.'), 'callback', 'check_max_number_of_members');
  150. $form->addElement('html', '</div>');
  151. $form->addElement('html', '<div class="col-md-6">');
  152. // Self registration
  153. $group = [
  154. $form->createElement(
  155. 'checkbox',
  156. 'self_reg_allowed',
  157. get_lang('Registration'),
  158. get_lang('Learners are allowed to self-register in groups'),
  159. 1
  160. ),
  161. $form->createElement('checkbox', 'self_unreg_allowed', null, get_lang('Learners are allowed to unregister themselves from groups'), 1),
  162. ];
  163. $form->addGroup(
  164. $group,
  165. '',
  166. Display::return_icon('user.png', get_lang('Registration')).' '.get_lang('Registration'),
  167. null,
  168. false
  169. );
  170. $form->addElement('html', '</div>');
  171. $form->addElement('hidden', 'action');
  172. $form->addElement('html', '<div class="col-md-12">');
  173. $form->addElement('header', get_lang('Default settings for new groups'));
  174. $form->addElement('html', '</div>');
  175. $form->addElement('html', '<div class="col-md-6">');
  176. // Documents settings.
  177. $group = [
  178. $form->createElement('radio', 'doc_state', get_lang('Documents'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  179. $form->createElement('radio', 'doc_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  180. $form->createElement('radio', 'doc_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  181. ];
  182. $form->addGroup(
  183. $group,
  184. '',
  185. Display::return_icon('folder.png', get_lang('Documents')).' '.get_lang('Documents'),
  186. null,
  187. false
  188. );
  189. $allowDocumentGroupAccess = api_get_configuration_value('group_category_document_access');
  190. if ($allowDocumentGroupAccess) {
  191. $form->addElement('html', '</div>');
  192. $form->addElement('html', '<div class="col-md-6">');
  193. $group = [
  194. $form->createElement(
  195. 'radio',
  196. 'document_access',
  197. null,
  198. get_lang('Share mode'),
  199. GroupManager::DOCUMENT_MODE_SHARE
  200. ),
  201. $form->createElement(
  202. 'radio',
  203. 'document_access',
  204. get_lang('Documents'),
  205. get_lang('Collaboration mode'),
  206. GroupManager::DOCUMENT_MODE_COLLABORATION
  207. ),
  208. $form->createElement(
  209. 'radio',
  210. 'document_access',
  211. null,
  212. get_lang('Read only mode'),
  213. GroupManager::DOCUMENT_MODE_READ_ONLY
  214. ),
  215. ];
  216. $form->addGroup(
  217. $group,
  218. '',
  219. Display::return_icon(
  220. 'folder.png',
  221. get_lang('DocumentsAccess')
  222. ).'<span>'.get_lang('DocumentsAccess').'</span>',
  223. null,
  224. false
  225. );
  226. $form->addElement('html', '</div>');
  227. }
  228. $form->addElement('html', '</div>');
  229. $form->addElement('html', '<div class="col-md-12">');
  230. $form->addElement('header', '');
  231. $form->addElement('html', '</div>');
  232. $form->addElement('html', '<div class="col-md-6">');
  233. // Work settings.
  234. $group = [
  235. $form->createElement('radio', 'work_state', get_lang('Assignments'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  236. $form->createElement('radio', 'work_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  237. $form->createElement('radio', 'work_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  238. ];
  239. $form->addGroup(
  240. $group,
  241. '',
  242. Display::return_icon('work.png', get_lang('Assignments'), [], ICON_SIZE_SMALL).' '.get_lang('Assignments'),
  243. '',
  244. false
  245. );
  246. $form->addElement('html', '</div>');
  247. $form->addElement('html', '<div class="col-md-6">');
  248. // Calendar settings.
  249. $group = [
  250. $form->createElement('radio', 'calendar_state', get_lang('Agenda'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  251. $form->createElement('radio', 'calendar_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  252. $form->createElement('radio', 'calendar_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  253. ];
  254. $form->addGroup(
  255. $group,
  256. '',
  257. Display::return_icon('agenda.png', get_lang('Agenda')).' '.get_lang('Agenda'),
  258. null,
  259. false
  260. );
  261. $form->addElement('html', '</div>');
  262. $form->addElement('html', '<div class="col-md-12">');
  263. $form->addElement('header', '');
  264. $form->addElement('html', '</div>');
  265. $form->addElement('html', '<div class="col-md-6">');
  266. // Announcements settings.
  267. $group = [
  268. $form->createElement('radio', 'announcements_state', get_lang('Announcements'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  269. $form->createElement('radio', 'announcements_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  270. $form->createElement('radio', 'announcements_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  271. ];
  272. $form->addGroup(
  273. $group,
  274. '',
  275. Display::return_icon('announce.png', get_lang('Announcements')).' '.get_lang('Announcements'),
  276. null,
  277. false
  278. );
  279. $form->addElement('html', '</div>');
  280. $form->addElement('html', '<div class="col-md-6">');
  281. // Forum settings.
  282. $group = [
  283. $form->createElement('radio', 'forum_state', get_lang('Group Forum'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  284. $form->createElement('radio', 'forum_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  285. $form->createElement('radio', 'forum_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  286. ];
  287. $form->addGroup(
  288. $group,
  289. '',
  290. Display::return_icon('forum.png', get_lang('Group Forum')).' '.get_lang('Group Forum'),
  291. null,
  292. false
  293. );
  294. $form->addElement('html', '</div>');
  295. $form->addElement('html', '<div class="col-md-12">');
  296. $form->addElement('header', '');
  297. $form->addElement('html', '</div>');
  298. $form->addElement('html', '<div class="col-md-6">');
  299. // Wiki settings.
  300. $group = [
  301. $form->createElement('radio', 'wiki_state', get_lang('Wiki'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  302. $form->createElement('radio', 'wiki_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  303. $form->createElement('radio', 'wiki_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  304. ];
  305. $form->addGroup(
  306. $group,
  307. '',
  308. Display::return_icon('wiki.png', get_lang('Wiki')).' '.get_lang('Wiki'),
  309. null,
  310. false
  311. );
  312. $form->addElement('html', '</div>');
  313. $form->addElement('html', '<div class="col-md-6">');
  314. // Chat settings.
  315. $group = [
  316. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('Not available'), GroupManager::TOOL_NOT_AVAILABLE),
  317. $form->createElement('radio', 'chat_state', null, get_lang('Public access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course) access (access authorized to any member of the course)'), GroupManager::TOOL_PUBLIC),
  318. $form->createElement('radio', 'chat_state', null, get_lang('Private access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only) access (access authorized to group members only)'), GroupManager::TOOL_PRIVATE),
  319. ];
  320. $form->addGroup(
  321. $group,
  322. '',
  323. Display::return_icon('chat.png', get_lang('Chat')).' '.get_lang('Chat'),
  324. null,
  325. false
  326. );
  327. $form->addElement('html', '</div>');
  328. // Submit
  329. if (isset($_GET['id'])) {
  330. $form->addButtonUpdate(get_lang('Edit'), 'submit');
  331. } else {
  332. $form->addButtonSave(get_lang('Add'), 'submit');
  333. }
  334. $currentUrl = api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq();
  335. // If form validates -> save data
  336. if ($form->validate()) {
  337. $values = $form->exportValues();
  338. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  339. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  340. } else {
  341. $max_member = $values['max_member'];
  342. }
  343. $self_reg_allowed = isset($values['self_reg_allowed']) ? $values['self_reg_allowed'] : 0;
  344. $self_unreg_allowed = isset($values['self_unreg_allowed']) ? $values['self_unreg_allowed'] : 0;
  345. switch ($values['action']) {
  346. case 'update_settings':
  347. GroupManager::update_category(
  348. $values['id'],
  349. $values['title'],
  350. $values['description'],
  351. $values['doc_state'],
  352. $values['work_state'],
  353. $values['calendar_state'],
  354. $values['announcements_state'],
  355. $values['forum_state'],
  356. $values['wiki_state'],
  357. $values['chat_state'],
  358. $self_reg_allowed,
  359. $self_unreg_allowed,
  360. $max_member,
  361. $values['groups_per_user'],
  362. isset($values['document_access']) ? $values['document_access'] : 0
  363. );
  364. Display::addFlash(Display::return_message(get_lang('Group settings have been modified')));
  365. header("Location: ".$currentUrl."&category=".$values['id']);
  366. exit;
  367. case 'add_category':
  368. GroupManager::create_category(
  369. $values['title'],
  370. $values['description'],
  371. $values['doc_state'],
  372. $values['work_state'],
  373. $values['calendar_state'],
  374. $values['announcements_state'],
  375. $values['forum_state'],
  376. $values['wiki_state'],
  377. $values['chat_state'],
  378. $self_reg_allowed,
  379. $self_unreg_allowed,
  380. $max_member,
  381. $values['groups_per_user'],
  382. isset($values['document_access']) ? $values['document_access'] : 0
  383. );
  384. Display::addFlash(Display::return_message(get_lang('Category created')));
  385. header("Location: ".$currentUrl);
  386. exit;
  387. break;
  388. }
  389. }
  390. // Else display the form
  391. Display::display_header($nameTools, 'Group');
  392. // actions bar
  393. echo '<div class="actions">';
  394. echo '<a href="group.php">'.
  395. Display::return_icon('back.png', get_lang('Back to Groups list'), '', ICON_SIZE_MEDIUM).'</a>';
  396. echo '</div>';
  397. $defaults = $category;
  398. $defaults['action'] = $action;
  399. if ($defaults['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  400. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  401. } else {
  402. $defaults['max_member_no_limit'] = 1;
  403. $defaults['max_member'] = $defaults['max_student'];
  404. }
  405. $form->setDefaults($defaults);
  406. $form->display();
  407. Display::display_footer();