group_category.php 13 KB

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