group_category.php 13 KB

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