group_category.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. $sessionId = api_get_session_id();
  12. if (!api_is_allowed_to_edit(false, true) ||
  13. !(isset($_GET['id']) ||
  14. isset($_POST['id']) ||
  15. isset($_GET['action']) ||
  16. isset($_POST['action']))
  17. ) {
  18. api_not_allowed(true);
  19. }
  20. if (!empty($sessionId)) {
  21. api_not_allowed(true);
  22. }
  23. /**
  24. * Function to check the given max number of members per group
  25. */
  26. function check_max_number_of_members($value)
  27. {
  28. $max_member_no_limit = $value['max_member_no_limit'];
  29. if ($max_member_no_limit == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  30. return true;
  31. }
  32. $max_member = $value['max_member'];
  33. return is_numeric($max_member);
  34. }
  35. /**
  36. * Function to check the number of groups per user
  37. *
  38. * @param $value
  39. * @return bool
  40. */
  41. function check_groups_per_user($value)
  42. {
  43. $groups_per_user = $value['groups_per_user'];
  44. if (isset($_POST['id']) &&
  45. intval($groups_per_user) != GroupManager::GROUP_PER_MEMBER_NO_LIMIT &&
  46. GroupManager::get_current_max_groups_per_user($_POST['id']) > intval($groups_per_user)) {
  47. return false;
  48. }
  49. return true;
  50. }
  51. if (api_get_setting('allow_group_categories') === 'true') {
  52. if (isset($_GET['id'])) {
  53. $category = GroupManager::get_category($_GET['id']);
  54. $nameTools = get_lang('EditGroupCategory').': '.$category['title'];
  55. } else {
  56. $nameTools = get_lang('AddCategory');
  57. // Default values for new category
  58. $category = array(
  59. 'groups_per_user' => 1,
  60. 'doc_state' => GroupManager::TOOL_PRIVATE,
  61. 'work_state' => GroupManager::TOOL_PRIVATE,
  62. 'wiki_state' => GroupManager::TOOL_PRIVATE,
  63. 'chat_state' => GroupManager::TOOL_PRIVATE,
  64. 'calendar_state' => GroupManager::TOOL_PRIVATE,
  65. 'announcements_state'=> GroupManager::TOOL_PRIVATE,
  66. 'forum_state' => GroupManager::TOOL_PRIVATE,
  67. 'max_student' => 0
  68. );
  69. }
  70. } else {
  71. $category = GroupManager::get_category($_GET['id']);
  72. $nameTools = get_lang('PropModify');
  73. }
  74. $htmlHeadXtra[] = '<script>
  75. $(document).ready( function() {
  76. $("#max_member").on("focus", function() {
  77. $("#max_member_selected").attr("checked", true);
  78. });
  79. });
  80. </script>';
  81. $interbreadcrumb[] = array('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. Database::query("ALTER TABLE $table_category ADD wiki_state tinyint(3) UNSIGNED NOT NULL default '1' WHERE c_id = $course_id");
  98. }
  99. // Create a new category
  100. $action = 'add_category';
  101. $form = new FormValidator('group_category');
  102. }
  103. // If categories allowed, show title & description field
  104. if (api_get_setting('allow_group_categories') == 'true') {
  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 = array();
  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 = array(
  115. $form->createElement('select', 'groups_per_user', null, $possible_values),
  116. $form->createElement('static', null, null, get_lang('QtyOfUserCanSubscribe_PartAfterNumber'))
  117. );
  118. $form->addGroup($group, 'limit_group', get_lang('QtyOfUserCanSubscribe_PartBeforeNumber'), null, false);
  119. $form->addRule('limit_group', get_lang('MaxGroupsPerUserInvalid'), 'callback', 'check_groups_per_user');
  120. // Members per group
  121. $group = array(
  122. $form->createElement('radio', 'max_member_no_limit', get_lang('GroupLimit'), get_lang('NoLimit'), GroupManager::MEMBER_PER_GROUP_NO_LIMIT),
  123. $form->createElement('radio', 'max_member_no_limit', null, get_lang('MaximumOfParticipants'), 1, array('id' => 'max_member_selected')),
  124. $form->createElement('text', 'max_member', null, array('class' => 'span1', 'id' => 'max_member')),
  125. $form->createElement('static', null, null, ' '.get_lang('GroupPlacesThis'))
  126. );
  127. $form->addGroup($group, 'max_member_group', get_lang('GroupLimit'), null, false);
  128. $form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
  129. $form->addElement('html', '</div>');
  130. $form->addElement('html', '<div class="col-md-6">');
  131. // Description
  132. $form->addElement('textarea', 'description', get_lang('Description'), array('rows' => 6));
  133. $form->addElement('html', '</div>');
  134. $form->addElement('html', '</div>');
  135. } else {
  136. $form->addElement('hidden', 'title');
  137. $form->addElement('hidden', 'description');
  138. }
  139. $form->addElement('header', get_lang('DefaultSettingsForNewGroups'));
  140. $form->addElement('hidden', 'action');
  141. $form->addElement('html', '<div class="col-md-6">');
  142. // Self registration
  143. $group = array(
  144. $form->createElement('checkbox', 'self_reg_allowed', get_lang('GroupSelfRegistration'), get_lang('GroupAllowStudentRegistration'), 1),
  145. $form->createElement('checkbox', 'self_unreg_allowed', null, get_lang('GroupAllowStudentUnregistration'), 1)
  146. );
  147. $form->addGroup(
  148. $group,
  149. '',
  150. Display::return_icon('user.png', get_lang('GroupSelfRegistration')).' '.get_lang('GroupSelfRegistration'),
  151. null,
  152. false
  153. );
  154. // Documents settings.
  155. $group = array(
  156. $form->createElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  157. $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  158. $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  159. );
  160. $form->addGroup(
  161. $group,
  162. '',
  163. Display::return_icon('folder.png', get_lang('GroupDocument')).' '.get_lang('GroupDocument'),
  164. null,
  165. false
  166. );
  167. // Work settings.
  168. $group = array(
  169. $form->createElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  170. $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  171. $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  172. );
  173. $form->addGroup(
  174. $group,
  175. '',
  176. Display::return_icon('work.png', get_lang('GroupWork'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWork'),
  177. '',
  178. false
  179. );
  180. // Calendar settings.
  181. $group = array(
  182. $form->createElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  183. $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  184. $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  185. );
  186. $form->addGroup(
  187. $group,
  188. '',
  189. Display::return_icon('agenda.png', get_lang('GroupCalendar')).' '.get_lang('GroupCalendar'),
  190. null,
  191. false
  192. );
  193. $form->addElement('html', '</div>');
  194. $form->addElement('html', '<div class="col-md-6">');
  195. // Announcements settings.
  196. $group = array(
  197. $form->createElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  198. $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  199. $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  200. );
  201. $form->addGroup(
  202. $group,
  203. '',
  204. Display::return_icon('announce.png', get_lang('GroupAnnouncements')).' '.get_lang('GroupAnnouncements'),
  205. null,
  206. false
  207. );
  208. // Forum settings.
  209. $group = array(
  210. $form->createElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  211. $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  212. $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  213. );
  214. $form->addGroup(
  215. $group,
  216. '',
  217. Display::return_icon('forum.png', get_lang('GroupForum')).' '.get_lang('GroupForum'),
  218. null,
  219. false
  220. );
  221. // Wiki settings.
  222. $group = array(
  223. $form->createElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  224. $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  225. $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  226. );
  227. $form->addGroup(
  228. $group,
  229. '',
  230. Display::return_icon('wiki.png', get_lang('GroupWiki')).' '.get_lang('GroupWiki'),
  231. null,
  232. false
  233. );
  234. // Chat settings.
  235. $group = array(
  236. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  237. $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  238. $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  239. );
  240. $form->addGroup(
  241. $group,
  242. '',
  243. Display::return_icon('chat.png', get_lang('Chat')).' '.get_lang('Chat'),
  244. null,
  245. false
  246. );
  247. $form->addElement('html', '</div>');
  248. $form->addElement('html', '<div class="col-md-12">');
  249. // Submit
  250. if (isset($_GET['id'])) {
  251. $form->addButtonUpdate(get_lang('Edit'), 'submit');
  252. } else {
  253. $form->addButtonSave(get_lang('Add'), 'submit');
  254. }
  255. $currentUrl = api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq();
  256. // If form validates -> save data
  257. if ($form->validate()) {
  258. $values = $form->exportValues();
  259. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  260. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  261. } else {
  262. $max_member = $values['max_member'];
  263. }
  264. $self_reg_allowed = isset($values['self_reg_allowed']) ? $values['self_reg_allowed'] : 0;
  265. $self_unreg_allowed = isset($values['self_unreg_allowed']) ? $values['self_unreg_allowed'] : 0;
  266. switch ($values['action']) {
  267. case 'update_settings':
  268. GroupManager::update_category(
  269. $values['id'],
  270. $values['title'],
  271. $values['description'],
  272. $values['doc_state'],
  273. $values['work_state'],
  274. $values['calendar_state'],
  275. $values['announcements_state'],
  276. $values['forum_state'],
  277. $values['wiki_state'],
  278. $values['chat_state'],
  279. $self_reg_allowed,
  280. $self_unreg_allowed,
  281. $max_member,
  282. $values['groups_per_user']
  283. );
  284. Display::addFlash(Display::return_message(get_lang('GroupPropertiesModified')));
  285. header("Location: ".$currentUrl."&category=".$values['id']);
  286. exit;
  287. case 'add_category':
  288. GroupManager :: create_category(
  289. $values['title'],
  290. $values['description'],
  291. $values['doc_state'],
  292. $values['work_state'],
  293. $values['calendar_state'],
  294. $values['announcements_state'],
  295. $values['forum_state'],
  296. $values['wiki_state'],
  297. $values['chat_state'],
  298. $self_reg_allowed,
  299. $self_unreg_allowed,
  300. $max_member,
  301. $values['groups_per_user']
  302. );
  303. Display::addFlash(Display::return_message(get_lang('CategoryCreated')));
  304. header("Location: ".$currentUrl);
  305. exit;
  306. break;
  307. }
  308. }
  309. // Else display the form
  310. Display :: display_header($nameTools, 'Group');
  311. // actions bar
  312. echo '<div class="actions">';
  313. echo '<a href="group.php">'.
  314. Display::return_icon('back.png', get_lang('BackToGroupList'), '', ICON_SIZE_MEDIUM).'</a>';
  315. echo '</div>';
  316. $defaults = $category;
  317. $defaults['action'] = $action;
  318. if ($defaults['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  319. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  320. } else {
  321. $defaults['max_member_no_limit'] = 1;
  322. $defaults['max_member'] = $defaults['max_student'];
  323. }
  324. $form->setDefaults($defaults);
  325. $form->display();
  326. Display :: display_footer();