group_category.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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('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', '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('group_category', 'post', '?id='.$category['id']);
  81. $form->addElement('hidden', 'id');
  82. } else {
  83. // Checks if the field was created in the table Category. It creates it if is neccesary
  84. $table_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  85. if (!Database::query("SELECT wiki_state FROM $table_category WHERE c_id = $course_id")) {
  86. Database::query("ALTER TABLE $table_category ADD wiki_state tinyint(3) UNSIGNED NOT NULL default '1' WHERE c_id = $course_id");
  87. }
  88. // Create a new category
  89. $action = 'add_category';
  90. $form = new FormValidator('group_category');
  91. }
  92. // If categories allowed, show title & description field
  93. if (api_get_setting('allow_group_categories') == 'true') {
  94. $form->addElement('header', $nameTools);
  95. $form->addElement('html', '<div class="row"><div class="col-md-6">');
  96. $form->addText('title', get_lang('Title'));
  97. // Groups per user
  98. $possible_values = array();
  99. for ($i = 1; $i <= 10; $i ++) {
  100. $possible_values[$i] = $i;
  101. }
  102. $possible_values[GroupManager::GROUP_PER_MEMBER_NO_LIMIT] = get_lang('All');
  103. $group = array(
  104. $form->createElement('select', 'groups_per_user', null, $possible_values),
  105. $form->createElement('static', null, null, get_lang('QtyOfUserCanSubscribe_PartAfterNumber'))
  106. );
  107. $form->addGroup($group, 'limit_group', get_lang('QtyOfUserCanSubscribe_PartBeforeNumber'), ' ', false);
  108. $form->addRule('limit_group', get_lang('MaxGroupsPerUserInvalid'), 'callback', 'check_groups_per_user');
  109. // Members per group
  110. $group = array(
  111. $form->createElement('radio', 'max_member_no_limit', get_lang('GroupLimit'), get_lang('NoLimit'), GroupManager::MEMBER_PER_GROUP_NO_LIMIT),
  112. $form->createElement('radio', 'max_member_no_limit', null, get_lang('MaximumOfParticipants'), 1, array('id' => 'max_member_selected')),
  113. $form->createElement('text', 'max_member', null, array('class' => 'span1', 'id' => 'max_member')),
  114. $form->createElement('static', null, null, ' '.get_lang('GroupPlacesThis'))
  115. );
  116. $form->addGroup($group, 'max_member_group', get_lang('GroupLimit'), '', false);
  117. $form->addRule('max_member_group', get_lang('InvalidMaxNumberOfMembers'), 'callback', 'check_max_number_of_members');
  118. $form->addElement('html', '</div>');
  119. $form->addElement('html', '<div class="col-md-6">');
  120. // Description
  121. $form->addElement('textarea', 'description', get_lang('Description'), array ('rows' => 6));
  122. $form->addElement('html', '</div>');
  123. $form->addElement('html', '</div>');
  124. } else {
  125. $form->addElement('hidden', 'title');
  126. $form->addElement('hidden', 'description');
  127. }
  128. $form->addElement('header', get_lang('DefaultSettingsForNewGroups'));
  129. $form->addElement('hidden', 'action');
  130. $form->addElement('html', '<div class="col-md-6">');
  131. // Self registration
  132. $group = array(
  133. $form->createElement('checkbox', 'self_reg_allowed', get_lang('GroupSelfRegistration'), get_lang('GroupAllowStudentRegistration'), 1),
  134. $form->createElement('checkbox', 'self_unreg_allowed', null, get_lang('GroupAllowStudentUnregistration'), 1)
  135. );
  136. $form->addGroup($group, '', Display::return_icon('user.png', get_lang('GroupSelfRegistration'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupSelfRegistration'), '', false);
  137. // Documents settings.
  138. $group = array(
  139. $form->createElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  140. $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  141. $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  142. );
  143. $form->addGroup($group, '', Display::return_icon('folder.png', get_lang('GroupDocument'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupDocument'), '', false);
  144. // Work settings.
  145. $group = array(
  146. $form->createElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  147. $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  148. $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  149. );
  150. $form->addGroup($group, '', Display::return_icon('work.png', get_lang('GroupWork'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWork'), '', false);
  151. // Calendar settings.
  152. $group = array(
  153. $form->createElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  154. $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  155. $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  156. );
  157. $form->addGroup($group, '', Display::return_icon('agenda.png', get_lang('GroupCalendar'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupCalendar'), '', false);
  158. $form->addElement('html', '</div>');
  159. $form->addElement('html', '<div class="col-md-6">');
  160. // Announcements settings.
  161. $group = array(
  162. $form->createElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  163. $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  164. $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  165. );
  166. $form->addGroup($group, '', Display::return_icon('announce.png', get_lang('GroupAnnouncements'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupAnnouncements'), '', false);
  167. // Forum settings.
  168. $group = array(
  169. $form->createElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  170. $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  171. $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  172. );
  173. $form->addGroup($group, '', Display::return_icon('forum.png', get_lang('GroupForum'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupForum'), '', false);
  174. // Wiki settings.
  175. $group = array(
  176. $form->createElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  177. $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  178. $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  179. );
  180. $form->addGroup($group, '', Display::return_icon('wiki.png', get_lang('GroupWiki'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWiki'), '', false);
  181. // Chat settings.
  182. $group = array(
  183. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  184. $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  185. $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  186. );
  187. $form->addGroup($group, '', Display::return_icon('chat.png', get_lang('Chat'), array(), ICON_SIZE_SMALL).' '.get_lang('Chat'), '', false);
  188. $form->addElement('html', '</div>');
  189. $form->addElement('html', '<div class="col-md-12">');
  190. // Submit
  191. $form->addButtonSave(get_lang('PropModify'), 'submit');
  192. $currentUrl = api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq();
  193. // If form validates -> save data
  194. if ($form->validate()) {
  195. $values = $form->exportValues();
  196. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  197. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  198. } else {
  199. $max_member = $values['max_member'];
  200. }
  201. $self_reg_allowed = isset($values['self_reg_allowed']) ? $values['self_reg_allowed'] : 0;
  202. $self_unreg_allowed = isset($values['self_unreg_allowed']) ? $values['self_unreg_allowed'] : 0;
  203. switch ($values['action']) {
  204. case 'update_settings':
  205. GroupManager::update_category(
  206. $values['id'],
  207. $values['title'],
  208. $values['description'],
  209. $values['doc_state'],
  210. $values['work_state'],
  211. $values['calendar_state'],
  212. $values['announcements_state'],
  213. $values['forum_state'],
  214. $values['wiki_state'],
  215. $values['chat_state'],
  216. $self_reg_allowed,
  217. $self_unreg_allowed,
  218. $max_member,
  219. $values['groups_per_user']
  220. );
  221. Display::addFlash(Display::return_message(get_lang('GroupPropertiesModified')));
  222. header("Location: ".$currentUrl."&category=".$values['id']);
  223. exit;
  224. case 'add_category':
  225. GroupManager :: create_category(
  226. $values['title'],
  227. $values['description'],
  228. $values['doc_state'],
  229. $values['work_state'],
  230. $values['calendar_state'],
  231. $values['announcements_state'],
  232. $values['forum_state'],
  233. $values['wiki_state'],
  234. $values['chat_state'],
  235. $self_reg_allowed,
  236. $self_unreg_allowed,
  237. $max_member,
  238. $values['groups_per_user']
  239. );
  240. Display::addFlash(Display::return_message(get_lang('CategoryCreated')));
  241. header("Location: ".$currentUrl);
  242. exit;
  243. break;
  244. }
  245. }
  246. // Else display the form
  247. Display :: display_header($nameTools, 'Group');
  248. // actions bar
  249. echo '<div class="actions">';
  250. echo '<a href="group.php">'.
  251. Display::return_icon('back.png', get_lang('BackToGroupList'),'',ICON_SIZE_MEDIUM).'</a>';
  252. echo '</div>';
  253. $defaults = $category;
  254. $defaults['action'] = $action;
  255. if ($defaults['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  256. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  257. } else {
  258. $defaults['max_member_no_limit'] = 1;
  259. $defaults['max_member'] = $defaults['max_student'];
  260. }
  261. $form->setDefaults($defaults);
  262. $form->display();
  263. Display :: display_footer();