group_category.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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?'.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('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'), ' ', 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'), '', 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($group, '', Display::return_icon('user.png', get_lang('GroupSelfRegistration'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupSelfRegistration'), '', false);
  141. // Documents settings.
  142. $group = array(
  143. $form->createElement('radio', 'doc_state', get_lang('GroupDocument'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  144. $form->createElement('radio', 'doc_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  145. $form->createElement('radio', 'doc_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  146. );
  147. $form->addGroup($group, '', Display::return_icon('folder.png', get_lang('GroupDocument'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupDocument'), '', false);
  148. // Work settings.
  149. $group = array(
  150. $form->createElement('radio', 'work_state', get_lang('GroupWork'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  151. $form->createElement('radio', 'work_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  152. $form->createElement('radio', 'work_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  153. );
  154. $form->addGroup($group, '', Display::return_icon('work.png', get_lang('GroupWork'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWork'), '', false);
  155. // Calendar settings.
  156. $group = array(
  157. $form->createElement('radio', 'calendar_state', get_lang('GroupCalendar'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  158. $form->createElement('radio', 'calendar_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  159. $form->createElement('radio', 'calendar_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  160. );
  161. $form->addGroup($group, '', Display::return_icon('agenda.png', get_lang('GroupCalendar'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupCalendar'), '', false);
  162. $form->addElement('html', '</div>');
  163. $form->addElement('html', '<div class="col-md-6">');
  164. // Announcements settings.
  165. $group = array(
  166. $form->createElement('radio', 'announcements_state', get_lang('GroupAnnouncements'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  167. $form->createElement('radio', 'announcements_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  168. $form->createElement('radio', 'announcements_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  169. );
  170. $form->addGroup($group, '', Display::return_icon('announce.png', get_lang('GroupAnnouncements'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupAnnouncements'), '', false);
  171. // Forum settings.
  172. $group = array(
  173. $form->createElement('radio', 'forum_state', get_lang('GroupForum'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  174. $form->createElement('radio', 'forum_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  175. $form->createElement('radio', 'forum_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  176. );
  177. $form->addGroup($group, '', Display::return_icon('forum.png', get_lang('GroupForum'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupForum'), '', false);
  178. // Wiki settings.
  179. $group = array(
  180. $form->createElement('radio', 'wiki_state', get_lang('GroupWiki'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  181. $form->createElement('radio', 'wiki_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  182. $form->createElement('radio', 'wiki_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  183. );
  184. $form->addGroup($group, '', Display::return_icon('wiki.png', get_lang('GroupWiki'), array(), ICON_SIZE_SMALL).' '.get_lang('GroupWiki'), '', false);
  185. // Chat settings.
  186. $group = array(
  187. $form->createElement('radio', 'chat_state', get_lang('Chat'), get_lang('NotAvailable'), GroupManager::TOOL_NOT_AVAILABLE),
  188. $form->createElement('radio', 'chat_state', null, get_lang('Public'), GroupManager::TOOL_PUBLIC),
  189. $form->createElement('radio', 'chat_state', null, get_lang('Private'), GroupManager::TOOL_PRIVATE)
  190. );
  191. $form->addGroup($group, '', Display::return_icon('chat.png', get_lang('Chat'), array(), ICON_SIZE_SMALL).' '.get_lang('Chat'), '', false);
  192. $form->addElement('html', '</div>');
  193. $form->addElement('html', '<div class="col-md-12">');
  194. // Submit
  195. $form->addButtonSave(get_lang('PropModify'), 'submit');
  196. $currentUrl = api_get_path(WEB_CODE_PATH).'group/group.php?'.api_get_cidreq();
  197. // If form validates -> save data
  198. if ($form->validate()) {
  199. $values = $form->exportValues();
  200. if ($values['max_member_no_limit'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  201. $max_member = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  202. } else {
  203. $max_member = $values['max_member'];
  204. }
  205. $self_reg_allowed = isset($values['self_reg_allowed']) ? $values['self_reg_allowed'] : 0;
  206. $self_unreg_allowed = isset($values['self_unreg_allowed']) ? $values['self_unreg_allowed'] : 0;
  207. switch ($values['action']) {
  208. case 'update_settings':
  209. GroupManager::update_category(
  210. $values['id'],
  211. $values['title'],
  212. $values['description'],
  213. $values['doc_state'],
  214. $values['work_state'],
  215. $values['calendar_state'],
  216. $values['announcements_state'],
  217. $values['forum_state'],
  218. $values['wiki_state'],
  219. $values['chat_state'],
  220. $self_reg_allowed,
  221. $self_unreg_allowed,
  222. $max_member,
  223. $values['groups_per_user']
  224. );
  225. Display::addFlash(Display::return_message(get_lang('GroupPropertiesModified')));
  226. header("Location: ".$currentUrl."&category=".$values['id']);
  227. exit;
  228. case 'add_category':
  229. GroupManager :: create_category(
  230. $values['title'],
  231. $values['description'],
  232. $values['doc_state'],
  233. $values['work_state'],
  234. $values['calendar_state'],
  235. $values['announcements_state'],
  236. $values['forum_state'],
  237. $values['wiki_state'],
  238. $values['chat_state'],
  239. $self_reg_allowed,
  240. $self_unreg_allowed,
  241. $max_member,
  242. $values['groups_per_user']
  243. );
  244. Display::addFlash(Display::return_message(get_lang('CategoryCreated')));
  245. header("Location: ".$currentUrl);
  246. exit;
  247. break;
  248. }
  249. }
  250. // Else display the form
  251. Display :: display_header($nameTools, 'Group');
  252. // actions bar
  253. echo '<div class="actions">';
  254. echo '<a href="group.php">'.
  255. Display::return_icon('back.png', get_lang('BackToGroupList'),'',ICON_SIZE_MEDIUM).'</a>';
  256. echo '</div>';
  257. $defaults = $category;
  258. $defaults['action'] = $action;
  259. if ($defaults['max_student'] == GroupManager::MEMBER_PER_GROUP_NO_LIMIT) {
  260. $defaults['max_member_no_limit'] = GroupManager::MEMBER_PER_GROUP_NO_LIMIT;
  261. } else {
  262. $defaults['max_member_no_limit'] = 1;
  263. $defaults['max_member'] = $defaults['max_student'];
  264. }
  265. $form->setDefaults($defaults);
  266. $form->display();
  267. Display :: display_footer();