groupmanager.lib.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Code
  9. */
  10. /**
  11. * This library contains some functions for group-management.
  12. * @author Bart Mollet
  13. * @package chamilo.library
  14. * @todo Add $course_code parameter to all functions. So this GroupManager can
  15. * be used outside a session.
  16. */
  17. class GroupManager
  18. {
  19. //- VIRTUAL_COURSE_CATEGORY: in this category groups are created based on the virtual course of a course
  20. CONST VIRTUAL_COURSE_CATEGORY = 1;
  21. //DEFAULT_GROUP_CATEGORY: When group categories aren't available (platform-setting), all groups are created in this 'dummy'-category
  22. CONST DEFAULT_GROUP_CATEGORY = 2;
  23. /**
  24. * infinite
  25. */
  26. CONST INFINITE = 99999;
  27. /**
  28. * No limit on the number of users in a group
  29. */
  30. CONST MEMBER_PER_GROUP_NO_LIMIT = 0;
  31. /**
  32. * No limit on the number of groups per user
  33. */
  34. CONST GROUP_PER_MEMBER_NO_LIMIT = 0;
  35. /**
  36. * The tools of a group can have 3 states
  37. * - not available
  38. * - public
  39. * - private
  40. */
  41. CONST TOOL_NOT_AVAILABLE = 0;
  42. CONST TOOL_PUBLIC = 1;
  43. CONST TOOL_PRIVATE = 2;
  44. /**
  45. * Constants for the available group tools
  46. */
  47. CONST GROUP_TOOL_FORUM = 0;
  48. CONST GROUP_TOOL_DOCUMENTS = 1;
  49. CONST GROUP_TOOL_CALENDAR = 2;
  50. CONST GROUP_TOOL_ANNOUNCEMENT = 3;
  51. CONST GROUP_TOOL_WORK = 4;
  52. CONST GROUP_TOOL_WIKI = 5;
  53. CONST GROUP_TOOL_CHAT = 6;
  54. //GROUP FUNCTIONS
  55. private function __construct()
  56. {
  57. }
  58. public static function get_groups()
  59. {
  60. $table_group = Database :: get_course_table(TABLE_GROUP);
  61. $course_id = api_get_course_int_id();
  62. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id ";
  63. $result = Database::query($sql);
  64. return Database::store_result($result, 'ASSOC');
  65. }
  66. /**
  67. * Get list of groups for current course.
  68. * @param int $category The id of the category from which the groups are
  69. * requested
  70. * @param string $course_code Default is current course
  71. * @return array An array with all information about the groups.
  72. */
  73. public static function get_group_list($category = null, $course_code = null)
  74. {
  75. $my_user_id = api_get_user_id();
  76. $course_info = api_get_course_info($course_code);
  77. $course_id = $course_info['real_id'];
  78. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  79. $table_group = Database :: get_course_table(TABLE_GROUP);
  80. //condition for the session
  81. $session_id = api_get_session_id();
  82. $my_status_of_user_in_course = CourseManager::get_user_in_course_status($my_user_id, $course_info['code']);
  83. $is_student_in_session = false;
  84. // User is subscribed in session
  85. if (is_null($my_status_of_user_in_course) || $my_status_of_user_in_course == '') {
  86. if ($session_id > 0) {
  87. $is_student_in_session = true;
  88. }
  89. }
  90. $sql = null;
  91. // @todo fix the conditions below
  92. // COURSEMANAGER or STUDENT
  93. if ($my_status_of_user_in_course == COURSEMANAGER || api_is_allowed_to_edit(null, true) || api_is_drh()) {
  94. $sql = "SELECT g.id ,
  95. g.name ,
  96. g.description ,
  97. g.category_id,
  98. g.max_student maximum_number_of_members,
  99. g.secret_directory,
  100. g.self_registration_allowed,
  101. g.self_unregistration_allowed,
  102. g.session_id,
  103. ug.user_id is_member
  104. FROM $table_group g
  105. LEFT JOIN $table_group_user ug
  106. ON (ug.group_id = g.id AND ug.user_id = '".api_get_user_id(
  107. )."' AND ug.c_id = $course_id AND g.c_id = $course_id)";
  108. } elseif ($my_status_of_user_in_course == STUDENT || $is_student_in_session === true) {
  109. $sql = "SELECT g.id,
  110. g.name,
  111. g.description,
  112. g.category_id,
  113. g.max_student maximum_number_of_members,
  114. g.secret_directory,
  115. g.self_registration_allowed,
  116. g.self_unregistration_allowed,
  117. g.session_id,
  118. ug.user_id is_member
  119. FROM $table_group g
  120. LEFT JOIN $table_group_user ug
  121. ON (ug.group_id = g.id AND
  122. ug.user_id = '".api_get_user_id()."' AND
  123. ug.c_id = $course_id AND
  124. g.c_id = $course_id )";
  125. }
  126. if (empty($sql)) {
  127. return array();
  128. }
  129. $sql .= " WHERE 1=1 ";
  130. if ($category != null) {
  131. $sql .= " AND g.category_id = '".Database::escape_string($category)."' ";
  132. $session_condition = api_get_session_condition($session_id);
  133. if (!empty($session_condition)) {
  134. $sql .= $session_condition;
  135. }
  136. } else {
  137. $session_condition = api_get_session_condition($session_id, true);
  138. }
  139. $sql .= " AND g.c_id = $course_id ";
  140. if (!empty($session_condition)) {
  141. $sql .= $session_condition;
  142. }
  143. $sql .= " GROUP BY g.id ORDER BY UPPER(g.name)";
  144. if (!api_is_anonymous()) {
  145. $groupList = Database::query($sql);
  146. } else {
  147. return array();
  148. }
  149. $groups = array();
  150. while ($thisGroup = Database::fetch_array($groupList)) {
  151. $thisGroup['number_of_members'] = count(self::get_subscribed_users($thisGroup['id']));
  152. if ($thisGroup['session_id'] != 0) {
  153. $sql_session = 'SELECT name FROM '.Database::get_main_table(TABLE_MAIN_SESSION).'
  154. WHERE id='.$thisGroup['session_id'];
  155. $rs_session = Database::query($sql_session);
  156. if (Database::num_rows($rs_session) > 0) {
  157. $thisGroup['session_name'] = Database::result($rs_session, 0, 0);
  158. } else {
  159. //the session has probably been removed, so the group is now orphaned
  160. }
  161. }
  162. $groups[] = $thisGroup;
  163. }
  164. return $groups;
  165. }
  166. /**
  167. * Create a group
  168. * @param string $name The name for this group
  169. * @param int $tutor The user-id of the group's tutor
  170. * @param int $places How many people can subscribe to the new group
  171. */
  172. public static function create_group($name, $category_id, $tutor, $places)
  173. {
  174. $_course = api_get_course_info();
  175. $table_group = Database :: get_course_table(TABLE_GROUP);
  176. $session_id = api_get_session_id();
  177. $course_id = api_get_course_int_id();
  178. $currentCourseRepository = $_course['path'];
  179. $category = self :: get_category($category_id);
  180. if (intval($places) == 0) {
  181. //if the amount of users per group is not filled in, use the setting from the category
  182. $places = $category['max_student'];
  183. } else {
  184. if ($places > $category['max_student'] && $category['max_student'] != 0) {
  185. $places = $category['max_student'];
  186. }
  187. }
  188. $sql = "INSERT INTO ".$table_group." SET
  189. c_id = $course_id ,
  190. category_id='".Database::escape_string($category_id)."',
  191. max_student = '".$places."',
  192. doc_state = '".$category['doc_state']."',
  193. calendar_state = '".$category['calendar_state']."',
  194. work_state = '".$category['work_state']."',
  195. announcements_state = '".$category['announcements_state']."',
  196. forum_state = '".$category['forum_state']."',
  197. wiki_state = '".$category['wiki_state']."',
  198. chat_state = '".$category['chat_state']."',
  199. self_registration_allowed = '".$category['self_reg_allowed']."',
  200. self_unregistration_allowed = '".$category['self_unreg_allowed']."',
  201. session_id='".Database::escape_string($session_id)."'";
  202. Database::query($sql);
  203. $lastId = Database::insert_id();
  204. //Fixing Id
  205. Database::update($table_group, array('id' => $lastId), array('iid = ? ' => $lastId));
  206. if ($lastId) {
  207. $desired_dir_name = '/'.api_replace_dangerous_char($name, 'strict').'_groupdocs';
  208. $my_path = api_get_path(SYS_COURSE_PATH).$currentCourseRepository.'/document';
  209. $unique_name = FileManager::create_unexisting_directory(
  210. $_course,
  211. api_get_user_id(),
  212. $session_id,
  213. $lastId,
  214. null,
  215. $my_path,
  216. $desired_dir_name
  217. );
  218. /* Stores the directory path into the group table */
  219. $sql = "UPDATE ".$table_group." SET name = '".Database::escape_string(
  220. $name
  221. )."', secret_directory = '".$unique_name."'
  222. WHERE c_id = $course_id AND id ='".$lastId."'";
  223. Database::query($sql);
  224. // create a forum if needed
  225. if ($category['forum_state'] >= 0) {
  226. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  227. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  228. $forum_categories = get_forum_categories();
  229. $values = array();
  230. $values['forum_title'] = $name;
  231. $values['group_id'] = $lastId;
  232. $counter = 0;
  233. foreach ($forum_categories as $key => $value) {
  234. if ($counter == 0) {
  235. $forum_category_id = $key;
  236. }
  237. $counter++;
  238. }
  239. // A sanity check.
  240. if (empty($forum_category_id)) {
  241. $forum_category_id = 0;
  242. }
  243. $values['forum_category'] = $forum_category_id;
  244. $values['allow_anonymous_group']['allow_anonymous'] = 0;
  245. $values['students_can_edit_group']['students_can_edit'] = 0;
  246. $values['approval_direct_group']['approval_direct'] = 0;
  247. $values['allow_attachments_group']['allow_attachments'] = 1;
  248. $values['allow_new_threads_group']['allow_new_threads'] = 1;
  249. $values['default_view_type_group']['default_view_type'] = api_get_setting('default_forum_view');
  250. $values['group_forum'] = $lastId;
  251. if ($category['forum_state'] == '1') {
  252. $values['public_private_group_forum_group']['public_private_group_forum'] = 'public';
  253. } elseif ($category['forum_state'] == '2') {
  254. $values['public_private_group_forum_group']['public_private_group_forum'] = 'private';
  255. } elseif ($category['forum_state'] == '0') {
  256. $values['public_private_group_forum_group']['public_private_group_forum'] = 'unavailable';
  257. }
  258. store_forum($values);
  259. }
  260. }
  261. return $lastId;
  262. }
  263. /**
  264. * Create subgroups.
  265. * This function creates new groups based on an existing group. It will
  266. * create the specified number of groups and fill those groups with users
  267. * from the base group
  268. * @param int $group_id The group from which subgroups have to be created.
  269. * @param int $number_of_groups The number of groups that have to be created
  270. */
  271. public static function create_subgroups($group_id, $number_of_groups)
  272. {
  273. $course_id = api_get_course_int_id();
  274. $table_group = Database :: get_course_table(TABLE_GROUP);
  275. $category_id = self :: create_category('Subgroups', '', self::TOOL_PRIVATE, self::TOOL_PRIVATE, 0, 0, 1, 1);
  276. $users = self :: get_users($group_id);
  277. $group_ids = array();
  278. for ($group_nr = 1; $group_nr <= $number_of_groups; $group_nr++) {
  279. $group_ids[] = self :: create_group('SUBGROUP '.$group_nr, $category_id, 0, 0);
  280. }
  281. $members = array();
  282. foreach ($users as $index => $user_id) {
  283. self :: subscribe_users($user_id, $group_ids[$index % $number_of_groups]);
  284. $members[$group_ids[$index % $number_of_groups]]++;
  285. }
  286. foreach ($members as $group_id => $places) {
  287. $sql = "UPDATE $table_group SET max_student = $places WHERE c_id = $course_id AND id = $group_id";
  288. Database::query($sql);
  289. }
  290. }
  291. /**
  292. * Create a group for every class subscribed to the current course
  293. * @param int $category_id The category in which the groups should be
  294. * created
  295. */
  296. public static function create_class_groups($category_id)
  297. {
  298. $_course = api_get_course_info();
  299. $options['where'] = array(" usergroup.course_id = ? " => api_get_real_course_id());
  300. $obj = new UserGroup();
  301. $classes = $obj->get_usergroup_in_course($options);
  302. $group_ids = array();
  303. foreach ($classes as $index => $class) {
  304. $users_ids = $obj->get_users_by_usergroup($class['id']);
  305. $group_id = self::create_group($class['name'], $category_id, 0, count($users_ids));
  306. self::subscribe_users($users_ids, $group_id);
  307. $group_ids[] = $group_id;
  308. }
  309. return $group_ids;
  310. }
  311. /**
  312. * deletes groups and their data.
  313. * @author Christophe Gesche <christophe.gesche@claroline.net>
  314. * @author Hugues Peeters <hugues.peeters@claroline.net>
  315. * @author Bart Mollet
  316. * @param mixed $groupIdList - group(s) to delete. It can be a single id
  317. * (int) or a list of id (array).
  318. * @param string $course_code Default is current course
  319. * @return integer - number of groups deleted.
  320. */
  321. public static function delete_groups($group_ids, $course_code = null)
  322. {
  323. $course_info = api_get_course_info($course_code);
  324. $course_id = $course_info['real_id'];
  325. // Database table definitions
  326. $group_table = Database :: get_course_table(TABLE_GROUP);
  327. $forum_table = Database :: get_course_table(TABLE_FORUM);
  328. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  329. $group_ids = array_map('intval', $group_ids);
  330. if (api_is_course_coach()) {
  331. //a coach can only delete courses from his session
  332. for ($i = 0; $i < count($group_ids); $i++) {
  333. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  334. array_splice($group_ids, $i, 1);
  335. $i--;
  336. }
  337. }
  338. if (count($group_ids) == 0) {
  339. return 0;
  340. }
  341. }
  342. // Unsubscribe all users
  343. self :: unsubscribe_all_users($group_ids);
  344. $sql = "SELECT id, secret_directory, session_id FROM $group_table WHERE c_id = $course_id AND id IN (".implode(
  345. ' , ',
  346. $group_ids
  347. ).")";
  348. $db_result = Database::query($sql);
  349. $forum_ids = array();
  350. while ($group = Database::fetch_object($db_result)) {
  351. // move group-documents to garbage
  352. //$source_directory = api_get_path(SYS_COURSE_PATH).$course['path']."/group/".$group->secret_directory;
  353. $source_directory = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$group->secret_directory;
  354. //File to renamed
  355. $destination_dir = api_get_path(
  356. SYS_COURSE_PATH
  357. ).$course_info['path']."/document".$group->secret_directory.'_DELETED_'.$group->id;
  358. if (!empty($group->secret_directory)) {
  359. //Deleting from document tool
  360. DocumentManager::delete_document($course_info, $group->secret_directory, $source_directory);
  361. if (file_exists($source_directory)) {
  362. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  363. //Delete
  364. FileManager::my_delete($source_directory);
  365. } else {
  366. //Rename
  367. rename($source_directory, $destination_dir);
  368. }
  369. }
  370. }
  371. //$forum_ids[] = $group->forum_id;
  372. }
  373. // delete the groups
  374. $sql = "DELETE FROM ".$group_table." WHERE c_id = $course_id AND id IN ('".implode("' , '", $group_ids)."')";
  375. Database::query($sql);
  376. $sql2 = "DELETE FROM ".$forum_table." WHERE c_id = $course_id AND forum_of_group IN ('".implode(
  377. "' , '",
  378. $group_ids
  379. )."')";
  380. $result = Database::query($sql2);
  381. return Database::affected_rows($result);
  382. }
  383. /**
  384. * Get group properties
  385. * @param int $group_id The group from which properties are requested.
  386. * @return array All properties. Array-keys are name, tutor_id, description, maximum_number_of_students, directory and visibility of tools
  387. */
  388. public static function get_group_properties($group_id)
  389. {
  390. $course_id = api_get_course_int_id();
  391. if (empty($group_id) or !is_integer(intval($group_id))) {
  392. return null;
  393. }
  394. $result = array();
  395. $table_group = Database :: get_course_table(TABLE_GROUP);
  396. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id AND id = ".intval($group_id);
  397. $db_result = Database::query($sql);
  398. $db_object = Database::fetch_object($db_result);
  399. $result['id'] = $db_object->id;
  400. $result['name'] = $db_object->name;
  401. $result['tutor_id'] = isset($db_object->tutor_id) ? $db_object->tutor_id : null;
  402. $result['description'] = $db_object->description;
  403. $result['maximum_number_of_students'] = $db_object->max_student;
  404. $result['doc_state'] = $db_object->doc_state;
  405. $result['work_state'] = $db_object->work_state;
  406. $result['calendar_state'] = $db_object->calendar_state;
  407. $result['announcements_state'] = $db_object->announcements_state;
  408. $result['forum_state'] = $db_object->forum_state;
  409. $result['wiki_state'] = $db_object->wiki_state;
  410. $result['chat_state'] = $db_object->chat_state;
  411. $result['directory'] = $db_object->secret_directory;
  412. $result['self_registration_allowed'] = $db_object->self_registration_allowed;
  413. $result['self_unregistration_allowed'] = $db_object->self_unregistration_allowed;
  414. $result['count_users'] = count(self::get_subscribed_users($group_id));
  415. $result['count_tutor'] = count(self::get_subscribed_tutors($group_id));
  416. $result['count_all'] = $result['count_users'] + $result['count_tutor'];
  417. return $result;
  418. }
  419. /**
  420. * Set group properties
  421. * Changes the group's properties.
  422. * @param int Group Id
  423. * @param string Group name
  424. * @param string Group description
  425. * @param int Max number of students in group
  426. * @param int Document tool's visibility (0=none,1=private,2=public)
  427. * @param int Work tool's visibility (0=none,1=private,2=public)
  428. * @param int Calendar tool's visibility (0=none,1=private,2=public)
  429. * @param int Announcement tool's visibility (0=none,1=private,2=public)
  430. * @param int Forum tool's visibility (0=none,1=private,2=public)
  431. * @param int Wiki tool's visibility (0=none,1=private,2=public)
  432. * @param int Chat tool's visibility (0=none,1=private,2=public)
  433. * @param bool Whether self registration is allowed or not
  434. * @param bool Whether self unregistration is allowed or not
  435. * @return bool TRUE if properties are successfully changed, false otherwise
  436. */
  437. public static function set_group_properties(
  438. $group_id,
  439. $name,
  440. $description,
  441. $maximum_number_of_students,
  442. $doc_state,
  443. $work_state,
  444. $calendar_state,
  445. $announcements_state,
  446. $forum_state,
  447. $wiki_state,
  448. $chat_state,
  449. $self_registration_allowed,
  450. $self_unregistration_allowed
  451. ) {
  452. $table_group = Database :: get_course_table(TABLE_GROUP);
  453. $table_forum = Database :: get_course_table(TABLE_FORUM);
  454. //$forum_id = get_forums_of_group($group_id);
  455. $group_id = Database::escape_string($group_id);
  456. $category = self::get_category_from_group($group_id);
  457. if ($maximum_number_of_students > $category['max_student'] && $category['max_student'] != 0) {
  458. $maximum_number_of_students = $category['max_student'];
  459. }
  460. $course_id = api_get_course_int_id();
  461. $sql = "UPDATE ".$table_group."
  462. SET name='".Database::escape_string(trim($name))."',
  463. doc_state = '".Database::escape_string($doc_state)."',
  464. work_state = '".Database::escape_string($work_state)."',
  465. calendar_state = '".Database::escape_string($calendar_state)."',
  466. announcements_state = '".Database::escape_string($announcements_state)."',
  467. forum_state = '".Database::escape_string($forum_state)."',
  468. wiki_state = '".Database::escape_string($wiki_state)."',
  469. chat_state = '".Database::escape_string($chat_state)."',
  470. description='".Database::escape_string(trim($description))."',
  471. max_student=".Database::escape_string($maximum_number_of_students).",
  472. self_registration_allowed='".Database::escape_string($self_registration_allowed)."',
  473. self_unregistration_allowed='".Database::escape_string($self_unregistration_allowed)."'
  474. WHERE c_id = $course_id AND id=".$group_id;
  475. $result = Database::query($sql);
  476. //Here we are updating a field in the table forum_forum that perhaps duplicates the table group_info.forum_state cvargas
  477. $forum_state = (int)$forum_state;
  478. $sql2 = "UPDATE ".$table_forum." SET ";
  479. if ($forum_state === 1) {
  480. $sql2 .= " forum_group_public_private='public' ";
  481. } elseif ($forum_state === 2) {
  482. $sql2 .= " forum_group_public_private='private' ";
  483. } elseif ($forum_state === 0) {
  484. $sql2 .= " forum_group_public_private='unavailable' ";
  485. }
  486. $sql2 .= " WHERE c_id = $course_id AND forum_of_group=".$group_id;
  487. $result2 = Database::query($sql2);
  488. return $result;
  489. }
  490. /**
  491. * Get the total number of groups for the current course.
  492. * @return int The number of groups for the current course.
  493. */
  494. public static function get_number_of_groups()
  495. {
  496. $course_id = api_get_course_int_id();
  497. $table_group = Database :: get_course_table(TABLE_GROUP);
  498. $res = Database::query("SELECT COUNT(id) AS number_of_groups FROM $table_group WHERE c_id = $course_id ");
  499. $obj = Database::fetch_object($res);
  500. return $obj->number_of_groups;
  501. }
  502. //GROUPCATEGORY FUNCTIONS
  503. /**
  504. * Get all categories
  505. * @param string $course_code The cours (default = current course)
  506. */
  507. public static function get_categories($course_code = null)
  508. {
  509. $course_info = api_get_course_info($course_code);
  510. $course_id = $course_info['real_id'];
  511. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  512. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id ORDER BY display_order";
  513. $res = Database::query($sql);
  514. $cats = array();
  515. while ($cat = Database::fetch_array($res)) {
  516. $cats[] = $cat;
  517. }
  518. return $cats;
  519. }
  520. /**
  521. * Get a group category
  522. * @param int $id The category id
  523. * @param string $course_code The course (default = current course)
  524. */
  525. public static function get_category($id, $course_code = null)
  526. {
  527. if (empty($id)) {
  528. return array();
  529. }
  530. $course_info = api_get_course_info($course_code);
  531. $course_id = $course_info['real_id'];
  532. $id = Database::escape_string($id);
  533. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  534. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id AND id = $id LIMIT 1";
  535. $res = Database::query($sql);
  536. return Database::fetch_array($res);
  537. }
  538. /**
  539. * Get the unique category of a given group
  540. * @param int $group_id The id of the group
  541. * @param string $course_code The course in which the group is (default =
  542. * current course)
  543. * @return array The category
  544. */
  545. public static function get_category_from_group($group_id, $course_code = null)
  546. {
  547. $table_group = Database :: get_course_table(TABLE_GROUP);
  548. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  549. if (empty($group_id)) {
  550. return array();
  551. }
  552. $course_info = api_get_course_info($course_code);
  553. $course_id = $course_info['real_id'];
  554. $group_id = Database::escape_string($group_id);
  555. $sql = "SELECT gc.* FROM $table_group_cat gc, $table_group g
  556. WHERE gc.c_id = $course_id AND
  557. g.c_id = $course_id AND
  558. gc.id = g.category_id AND g.id= $group_id LIMIT 1";
  559. $res = Database::query($sql);
  560. $cat = array();
  561. if (Database::num_rows($res)) {
  562. $cat = Database::fetch_array($res);
  563. }
  564. return $cat;
  565. }
  566. /**
  567. * Delete a group category
  568. * @param int $cat_id The id of the category to delete
  569. * @param string $course_code The code in which the category should be
  570. * deleted (default = current course)
  571. */
  572. public static function delete_category($cat_id, $course_code = null)
  573. {
  574. $course_info = api_get_course_info($course_code);
  575. $course_id = $course_info['real_id'];
  576. $table_group = Database :: get_course_table(TABLE_GROUP);
  577. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  578. $cat_id = Database::escape_string($cat_id);
  579. $sql = "SELECT id FROM $table_group WHERE c_id = $course_id AND category_id='".$cat_id."'";
  580. $res = Database::query($sql);
  581. if (Database::num_rows($res) > 0) {
  582. $groups_to_delete = array();
  583. while ($group = Database::fetch_object($res)) {
  584. $groups_to_delete[] = $group->id;
  585. }
  586. self :: delete_groups($groups_to_delete);
  587. }
  588. $sql = "DELETE FROM $table_group_cat WHERE c_id = $course_id AND id='".$cat_id."'";
  589. Database::query($sql);
  590. }
  591. /**
  592. * Create group category
  593. * @param string $title The title of the new category
  594. * @param string $description The description of the new category
  595. * @param bool $self_registration_allowed
  596. * @param bool $self_unregistration_allowed
  597. * @param int $max_number_of_students
  598. * @param int $groups_per_user
  599. */
  600. public static function create_category(
  601. $title,
  602. $description,
  603. $doc_state,
  604. $work_state,
  605. $calendar_state,
  606. $announcements_state,
  607. $forum_state,
  608. $wiki_state,
  609. $chat_state = 1,
  610. $self_registration_allowed = 0,
  611. $self_unregistration_allowed = 0,
  612. $maximum_number_of_students = 8,
  613. $groups_per_user = 0
  614. ) {
  615. $table_group_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  616. $course_id = api_get_course_int_id();
  617. $sql = "SELECT MAX(display_order)+1 as new_order FROM $table_group_category WHERE c_id = $course_id ";
  618. $res = Database::query($sql);
  619. $obj = Database::fetch_object($res);
  620. if (!isset ($obj->new_order)) {
  621. $obj->new_order = 1;
  622. }
  623. $sql = "INSERT INTO ".$table_group_category." SET
  624. c_id = $course_id ,
  625. title='".Database::escape_string($title)."',
  626. display_order ='".$obj->new_order."',
  627. description='".Database::escape_string($description)."',
  628. doc_state = '".Database::escape_string($doc_state)."',
  629. work_state = '".Database::escape_string($work_state)."',
  630. calendar_state = '".Database::escape_string($calendar_state)."',
  631. announcements_state = '".Database::escape_string($announcements_state)."',
  632. forum_state = '".Database::escape_string($forum_state)."',
  633. wiki_state = '".Database::escape_string($wiki_state)."',
  634. chat_state = '".Database::escape_string($chat_state)."',
  635. groups_per_user = '".Database::escape_string($groups_per_user)."',
  636. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  637. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  638. max_student = '".Database::escape_string($maximum_number_of_students)."' ";
  639. Database::query($sql);
  640. $id = Database::insert_id();
  641. if ($id == self::VIRTUAL_COURSE_CATEGORY) {
  642. $sql = "UPDATE ".$table_group_category." SET id = ".($id + 1)." WHERE c_id = $course_id AND id = $id";
  643. Database::query($sql);
  644. return $id + 1;
  645. }
  646. return $id;
  647. }
  648. /**
  649. * Update group category
  650. * @param int $id The id of the category
  651. * @param string $title The title of the new category
  652. * @param string $description The description of the new category
  653. * @param bool $self_registration_allowed
  654. * @param bool $self_unregistration_allowed
  655. * @param int $max_number_of_students
  656. * @param int $groups_per_user
  657. */
  658. public static function update_category(
  659. $id,
  660. $title,
  661. $description,
  662. $doc_state,
  663. $work_state,
  664. $calendar_state,
  665. $announcements_state,
  666. $forum_state,
  667. $wiki_state,
  668. $chat_state,
  669. $self_registration_allowed,
  670. $self_unregistration_allowed,
  671. $maximum_number_of_students,
  672. $groups_per_user
  673. ) {
  674. $table_group_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  675. $id = Database::escape_string($id);
  676. $course_id = api_get_course_int_id();
  677. $sql = "UPDATE ".$table_group_category."
  678. SET title='".Database::escape_string($title)."',
  679. description='".Database::escape_string($description)."',
  680. doc_state = '".Database::escape_string($doc_state)."',
  681. work_state = '".Database::escape_string($work_state)."',
  682. calendar_state = '".Database::escape_string($calendar_state)."',
  683. announcements_state = '".Database::escape_string($announcements_state)."',
  684. forum_state = '".Database::escape_string($forum_state)."',
  685. wiki_state = '".Database::escape_string($wiki_state)."',
  686. chat_state = '".Database::escape_string($chat_state)."',
  687. groups_per_user = ".Database::escape_string($groups_per_user).",
  688. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  689. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  690. max_student = ".Database::escape_string($maximum_number_of_students)."
  691. WHERE c_id = $course_id AND id=$id";
  692. Database::query($sql);
  693. }
  694. /**
  695. * Returns the number of groups of the user with the greatest number of
  696. * subscribtions in the given category
  697. */
  698. public static function get_current_max_groups_per_user($category_id = null, $course_code = null)
  699. {
  700. $course_info = api_get_course_info($course_code);
  701. $group_table = Database :: get_course_table(TABLE_GROUP);
  702. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  703. $sql = 'SELECT COUNT(gu.group_id) AS current_max FROM '.$group_user_table.' gu, '.$group_table.' g
  704. WHERE g.c_id = '.$course_info['real_id'].' AND gu.group_id = g.id ';
  705. if ($category_id != null) {
  706. $category_id = Database::escape_string($category_id);
  707. $sql .= ' AND g.category_id = '.$category_id;
  708. }
  709. $sql .= ' GROUP BY gu.user_id ORDER BY current_max DESC LIMIT 1';
  710. $res = Database::query($sql);
  711. $obj = Database::fetch_object($res);
  712. return $obj->current_max;
  713. }
  714. /**
  715. * Swaps the display-order of two categories
  716. * @param int $id1 The id of the first category
  717. * @param int $id2 The id of the second category
  718. */
  719. public static function swap_category_order($id1, $id2)
  720. {
  721. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  722. $id1 = Database::escape_string($id1);
  723. $id2 = Database::escape_string($id2);
  724. $course_id = api_get_course_int_id();
  725. $sql = "SELECT id,display_order FROM $table_group_cat WHERE id IN ($id1,$id2) AND c_id = $course_id ";
  726. $res = Database::query($sql);
  727. $cat1 = Database::fetch_object($res);
  728. $cat2 = Database::fetch_object($res);
  729. $sql = "UPDATE $table_group_cat SET display_order=$cat2->display_order WHERE id = $cat1->id AND c_id = $course_id ";
  730. Database::query($sql);
  731. $sql = "UPDATE $table_group_cat SET display_order=$cat1->display_order WHERE id = $cat2->id AND c_id = $course_id ";
  732. Database::query($sql);
  733. }
  734. //GROUP USERS FUNCTIONS
  735. /**
  736. * Get all users from a given group
  737. * @param int $group_id The group
  738. * @return array list of user id
  739. */
  740. public static function get_users($group_id, $load_extra_info = false, $course_id = null)
  741. {
  742. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  743. $group_id = Database::escape_string($group_id);
  744. if (empty($course_id)) {
  745. $course_id = api_get_course_int_id();
  746. } else {
  747. $course_id = intval($course_id);
  748. }
  749. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  750. $res = Database::query($sql);
  751. $users = array();
  752. while ($obj = Database::fetch_object($res)) {
  753. if ($load_extra_info) {
  754. $users[] = api_get_user_info($obj->user_id);
  755. } else {
  756. $users[] = $obj->user_id;
  757. }
  758. }
  759. return $users;
  760. }
  761. public static function get_members_and_tutors($group_id)
  762. {
  763. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  764. $tutor_user_table = Database :: get_course_table(TABLE_GROUP_TUTOR);
  765. $course_id = api_get_course_int_id();
  766. $group_id = intval($group_id);
  767. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  768. $res = Database::query($sql);
  769. $users = array();
  770. while ($obj = Database::fetch_object($res)) {
  771. $users[] = api_get_user_info($obj->user_id);
  772. }
  773. $sql = "SELECT user_id FROM $tutor_user_table WHERE c_id = $course_id AND group_id = $group_id";
  774. $res = Database::query($sql);
  775. while ($obj = Database::fetch_object($res)) {
  776. $users[] = api_get_user_info($obj->user_id);
  777. }
  778. return $users;
  779. }
  780. /**
  781. * Returns users belonging to any of the group
  782. *
  783. * @param array $groups list of group ids
  784. * @return array list of user ids
  785. */
  786. public static function get_groups_users($groups = array())
  787. {
  788. $result = array();
  789. $tbl_group_user = Database::get_course_table(TABLE_GROUP_USER);
  790. $course_id = api_get_course_int_id();
  791. $groups = array_map('intval', $groups);
  792. $groups = implode(', ', $groups); //protect individual elements with surrounding quotes
  793. $sql = "SELECT DISTINCT user_id
  794. FROM $tbl_group_user gu
  795. WHERE c_id = $course_id AND gu.group_id IN ($groups)";
  796. $rs = Database::query($sql);
  797. while ($row = Database::fetch_array($rs)) {
  798. $result[] = $row['user_id'];
  799. }
  800. return $result;
  801. }
  802. /**
  803. * Fill the groups with students.
  804. * The algorithm takes care to first fill the groups with the least # of users.
  805. * Analysis
  806. * There was a problem with the "ALL" setting.
  807. * When max # of groups is set to all, the value is sometimes NULL and sometimes ALL
  808. * and in both cased the query does not work as expected.
  809. * Stupid solution (currently implemented: set ALL to a big number (INFINITE) and things are solved :)
  810. * Better solution: that's up to you.
  811. *
  812. * Note
  813. * Throughout Dokeos there is some confusion about "course id" and "course code"
  814. * The code is e.g. TEST101, but sometimes a variable that is called courseID also contains a course code string.
  815. * However, there is also a integer course_id that uniquely identifies the course.
  816. * ywarnier:> Now the course_id has been removed (25/1/2005)
  817. * The databases are als very inconsistent in this.
  818. *
  819. * @author Chrisptophe Gesche <christophe.geshe@claroline.net>,
  820. * Hugues Peeters <hugues.peeters@claroline.net> - original version
  821. * @author Roan Embrechts - virtual course support, code cleaning
  822. * @author Bart Mollet - code cleaning, use other GroupManager-functions
  823. * @return void
  824. */
  825. public static function fill_groups($group_ids)
  826. {
  827. $_course = api_get_course_info();
  828. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  829. $group_ids = array_map('intval', $group_ids);
  830. if (api_is_course_coach()) {
  831. for ($i = 0; $i < count($group_ids); $i++) {
  832. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  833. array_splice($group_ids, $i, 1);
  834. $i--;
  835. }
  836. }
  837. if (count($group_ids) == 0) {
  838. return false;
  839. }
  840. }
  841. $category = self::get_category_from_group($group_ids[0]);
  842. $groups_per_user = $category['groups_per_user'];
  843. $group_table = Database :: get_course_table(TABLE_GROUP);
  844. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  845. $session_id = api_get_session_id();
  846. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['code'], true, $session_id);
  847. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  848. /*
  849. * Retrieve all the groups where enrollment is still allowed
  850. * (reverse) ordered by the number of place available
  851. */
  852. $course_id = api_get_course_int_id();
  853. $sql = "SELECT g.id gid, g.max_student-count(ug.user_id) nbPlaces, g.max_student
  854. FROM ".$group_table." g
  855. LEFT JOIN ".$group_user_table." ug ON
  856. g.c_id = $course_id AND ug.c_id = $course_id AND g.id = ug.group_id
  857. WHERE
  858. g.id IN (".implode(',', $group_ids).")
  859. GROUP BY (g.id)
  860. HAVING (nbPlaces > 0 OR g.max_student = ".self::MEMBER_PER_GROUP_NO_LIMIT.")
  861. ORDER BY nbPlaces DESC";
  862. $sql_result = Database::query($sql);
  863. $group_available_place = array();
  864. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  865. $group_available_place[$group['gid']] = $group['nbPlaces'];
  866. }
  867. /*
  868. * Retrieve course users (reverse) ordered by the number
  869. * of group they are already enrolled
  870. */
  871. for ($i = 0; $i < count($complete_user_list); $i++) {
  872. //find # of groups the user is enrolled in
  873. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"], $category['id']);
  874. //add # of groups to user list
  875. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  876. }
  877. //first sort by user_id to filter out duplicates
  878. $complete_user_list = TableSort :: sort_table($complete_user_list, 'user_id');
  879. $complete_user_list = self :: filter_duplicates($complete_user_list, 'user_id');
  880. $complete_user_list = self :: filter_only_students($complete_user_list);
  881. //now sort by # of group left
  882. $complete_user_list = TableSort :: sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  883. $userToken = array();
  884. foreach ($complete_user_list as $this_user) {
  885. if ($this_user['number_groups_left'] > 0) {
  886. $userToken[$this_user['user_id']] = $this_user['number_groups_left'];
  887. }
  888. }
  889. $changed = true;
  890. while ($changed) {
  891. $changed = false;
  892. reset($group_available_place);
  893. arsort($group_available_place);
  894. reset($userToken);
  895. arsort($userToken);
  896. foreach ($group_available_place as $group_id => $place) {
  897. foreach ($userToken as $user_id => $places) {
  898. if (self :: can_user_subscribe($user_id, $group_id)) {
  899. self :: subscribe_users($user_id, $group_id);
  900. $group_available_place[$group_id]--;
  901. //$userToken[$user_id]--;
  902. unset($userToken[$user_id]);
  903. $changed = true;
  904. break;
  905. }
  906. }
  907. if ($changed) {
  908. break;
  909. }
  910. }
  911. }
  912. }
  913. /**
  914. * Get the number of students in a group.
  915. * @param int $group_id
  916. * @return int Number of students in the given group.
  917. */
  918. public static function number_of_students($group_id, $course_id = null)
  919. {
  920. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  921. $group_id = Database::escape_string($group_id);
  922. if (empty($course_id)) {
  923. $course_id = api_get_course_int_id();
  924. } else {
  925. $course_id = intval($course_id);
  926. }
  927. $sql = "SELECT COUNT(*) AS number_of_students FROM $table_group_user WHERE c_id = $course_id AND group_id = $group_id";
  928. $db_result = Database::query($sql);
  929. $db_object = Database::fetch_object($db_result);
  930. return $db_object->number_of_students;
  931. }
  932. /**
  933. * Maximum number of students in a group
  934. * @param int $group_id
  935. * @return int Maximum number of students in the given group.
  936. */
  937. public static function maximum_number_of_students($group_id)
  938. {
  939. $table_group = Database :: get_course_table(TABLE_GROUP);
  940. $group_id = Database::escape_string($group_id);
  941. $course_id = api_get_course_int_id();
  942. $db_result = Database::query("SELECT max_student FROM $table_group WHERE c_id = $course_id AND id = $group_id");
  943. $db_object = Database::fetch_object($db_result);
  944. if ($db_object->max_student == 0) {
  945. return self::INFINITE;
  946. }
  947. return $db_object->max_student;
  948. }
  949. /**
  950. * Number of groups of a user
  951. * @param int $user_id
  952. * @return int The number of groups the user is subscribed in.
  953. */
  954. public static function user_in_number_of_groups($user_id, $cat_id = null)
  955. {
  956. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  957. $table_group = Database :: get_course_table(TABLE_GROUP);
  958. $user_id = Database::escape_string($user_id);
  959. $cat_id = Database::escape_string($cat_id);
  960. $course_id = api_get_course_int_id();
  961. $cat_condition = '';
  962. if (!empty($cat_id)) {
  963. $cat_condition = " AND g.category_id = $cat_id ";
  964. }
  965. $sql = "SELECT COUNT(*) AS number_of_groups FROM $table_group_user gu, $table_group g
  966. WHERE gu.c_id = $course_id AND
  967. g.c_id = $course_id AND
  968. gu.user_id = $user_id AND
  969. g.id = gu.group_id $cat_condition";
  970. $db_result = Database::query($sql);
  971. $db_object = Database::fetch_object($db_result);
  972. return $db_object->number_of_groups;
  973. }
  974. /**
  975. * Is sef-registration allowed?
  976. * @param int $user_id
  977. * @param int $group_id
  978. * @return bool TRUE if self-registration is allowed in the given group.
  979. */
  980. public static function is_self_registration_allowed($user_id, $group_id)
  981. {
  982. $course_id = api_get_course_int_id();
  983. if (!$user_id > 0) {
  984. return false;
  985. }
  986. $table_group = Database :: get_course_table(TABLE_GROUP);
  987. $group_id = intval($group_id);
  988. if (isset($group_id)) {
  989. $sql = "SELECT self_registration_allowed FROM $table_group WHERE c_id = $course_id AND id = $group_id";
  990. $db_result = Database::query($sql);
  991. $db_object = Database::fetch_object($db_result);
  992. return $db_object->self_registration_allowed == 1 && self :: can_user_subscribe($user_id, $group_id);
  993. } else {
  994. return false;
  995. }
  996. }
  997. /**
  998. * Is sef-unregistration allowed?
  999. * @param int $user_id
  1000. * @param int $group_id
  1001. * @return bool TRUE if self-unregistration is allowed in the given group.
  1002. */
  1003. public static function is_self_unregistration_allowed($user_id, $group_id)
  1004. {
  1005. if (!$user_id > 0) {
  1006. return false;
  1007. }
  1008. $table_group = Database :: get_course_table(TABLE_GROUP);
  1009. $group_id = Database::escape_string($group_id);
  1010. $course_id = api_get_course_int_id();
  1011. $db_result = Database::query(
  1012. 'SELECT self_unregistration_allowed FROM '.$table_group.' WHERE c_id = '.$course_id.' AND id = '.$group_id
  1013. );
  1014. $db_object = Database::fetch_object($db_result);
  1015. return $db_object->self_unregistration_allowed == 1 && self :: can_user_unsubscribe($user_id, $group_id);
  1016. }
  1017. /**
  1018. * Is user subscribed in group?
  1019. * @param int $user_id
  1020. * @param int $group_id
  1021. * @return bool TRUE if given user is subscribed in given group
  1022. */
  1023. public static function is_subscribed($user_id, $group_id)
  1024. {
  1025. if (empty($user_id) or empty($group_id)) {
  1026. return false;
  1027. }
  1028. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1029. $group_id = Database::escape_string($group_id);
  1030. $user_id = Database::escape_string($user_id);
  1031. $course_id = api_get_course_int_id();
  1032. $sql = 'SELECT 1 FROM '.$table_group_user.' WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id = '.$user_id;
  1033. $db_result = Database::query($sql);
  1034. return Database::num_rows($db_result) > 0;
  1035. }
  1036. /**
  1037. * Can a user subscribe to a specified group in a course
  1038. * @param int $user_id
  1039. * @param int $group_id
  1040. * @return bool TRUE if given user can be subscribed in given group
  1041. */
  1042. public static function can_user_subscribe($user_id, $group_id)
  1043. {
  1044. $category = self :: get_category_from_group($group_id);
  1045. $result = !self :: is_subscribed($user_id, $group_id);
  1046. $result &= (self :: number_of_students($group_id) < self :: maximum_number_of_students($group_id));
  1047. if ($category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT) {
  1048. $category['groups_per_user'] = self::INFINITE;
  1049. }
  1050. $result &= (self :: user_in_number_of_groups($user_id, $category['id']) < $category['groups_per_user']);
  1051. $result &= !self :: is_tutor_of_group($user_id, $group_id);
  1052. return $result;
  1053. }
  1054. /**
  1055. * Can a user unsubscribe to a specified group in a course
  1056. * @param int $user_id
  1057. * @param int $group_id
  1058. * @return bool TRUE if given user can be unsubscribed from given group
  1059. * @internal for now, same as GroupManager::is_subscribed($user_id,$group_id)
  1060. */
  1061. public static function can_user_unsubscribe($user_id, $group_id)
  1062. {
  1063. $result = self :: is_subscribed($user_id, $group_id);
  1064. return $result;
  1065. }
  1066. /**
  1067. * Get all subscribed users from a group
  1068. * @param int $group_id
  1069. * @return array An array with information of all users from the given group.
  1070. * (user_id, firstname, lastname, email)
  1071. */
  1072. public static function get_subscribed_users($group_id)
  1073. {
  1074. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1075. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1076. $order_clause = api_sort_by_first_name(
  1077. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1078. if (empty($group_id)) {
  1079. return array();
  1080. }
  1081. $group_id = intval($group_id);
  1082. $course_id = api_get_course_int_id();
  1083. $sql = "SELECT ug.id, u.user_id, u.lastname, u.firstname, u.email, u.username
  1084. FROM $table_user u INNER JOIN $table_group_user ug ON (ug.user_id = u.user_id)
  1085. WHERE ug.c_id = $course_id AND
  1086. ug.group_id = $group_id
  1087. $order_clause";
  1088. $db_result = Database::query($sql);
  1089. $users = array();
  1090. while ($user = Database::fetch_object($db_result)) {
  1091. $member['user_id'] = $user->user_id;
  1092. $member['firstname'] = $user->firstname;
  1093. $member['lastname'] = $user->lastname;
  1094. $member['email'] = $user->email;
  1095. $member['username'] = $user->username;
  1096. $users[$member['user_id']] = $member;
  1097. }
  1098. return $users;
  1099. }
  1100. /**
  1101. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1102. * Get all subscribed tutors of a group
  1103. * @param int $group_id
  1104. * @return array An array with information of all users from the given group.
  1105. * (user_id, firstname, lastname, email)
  1106. */
  1107. public static function get_subscribed_tutors($group_id, $id_only = false)
  1108. {
  1109. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1110. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1111. $order_clause = api_sort_by_first_name(
  1112. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1113. $group_id = Database::escape_string($group_id);
  1114. $course_id = api_get_course_int_id();
  1115. $sql = "SELECT tg.id, u.user_id, u.lastname, u.firstname, u.email
  1116. FROM ".$table_user." u, ".$table_group_tutor." tg
  1117. WHERE tg.c_id = $course_id AND
  1118. tg.group_id='".$group_id."' AND
  1119. tg.user_id=u.user_id".$order_clause;
  1120. $db_result = Database::query($sql);
  1121. $users = array();
  1122. while ($user = Database::fetch_object($db_result)) {
  1123. if (!$id_only) {
  1124. $member['user_id'] = $user->user_id;
  1125. $member['firstname'] = $user->firstname;
  1126. $member['lastname'] = $user->lastname;
  1127. $member['email'] = $user->email;
  1128. $users[] = $member;
  1129. } else {
  1130. $users[] = $user->user_id;
  1131. }
  1132. }
  1133. return $users;
  1134. }
  1135. /**
  1136. * Subscribe user(s) to a specified group in current course
  1137. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1138. * @param int $group_id
  1139. * @return bool TRUE if successfull
  1140. */
  1141. public static function subscribe_users($user_ids, $group_id)
  1142. {
  1143. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1144. $result = true;
  1145. $course_id = api_get_course_int_id();
  1146. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1147. if (!empty($user_ids)) {
  1148. foreach ($user_ids as $user_id) {
  1149. $user_id = Database::escape_string($user_id);
  1150. $group_id = Database::escape_string($group_id);
  1151. $sql = "INSERT INTO ".$table_group_user." (c_id, user_id, group_id) VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1152. $result &= Database::query($sql);
  1153. }
  1154. }
  1155. return $result;
  1156. }
  1157. /**
  1158. * Subscribe tutor(s) to a specified group in current course
  1159. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1160. * @param int $group_id
  1161. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1162. * @see subscribe_users. This function is almost an exact copy of that function.
  1163. * @return bool TRUE if successfull
  1164. */
  1165. public static function subscribe_tutors($user_ids, $group_id)
  1166. {
  1167. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1168. $result = true;
  1169. $course_id = api_get_course_int_id();
  1170. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1171. foreach ($user_ids as $index => $user_id) {
  1172. $user_id = Database::escape_string($user_id);
  1173. $group_id = Database::escape_string($group_id);
  1174. $sql = "INSERT INTO ".$table_group_tutor." (c_id, user_id, group_id) VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1175. $result &= Database::query($sql);
  1176. }
  1177. return $result;
  1178. }
  1179. /**
  1180. * Unsubscribe user(s) from a specified group in current course
  1181. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1182. * @param int $group_id
  1183. * @return bool TRUE if successfull
  1184. */
  1185. public static function unsubscribe_users($user_ids, $group_id)
  1186. {
  1187. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1188. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1189. $group_id = Database::escape_string($group_id);
  1190. $course_id = api_get_course_int_id();
  1191. $result &= Database::query(
  1192. 'DELETE FROM '.$table_group_user.' WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id IN ('.implode(
  1193. ',',
  1194. $user_ids
  1195. ).')'
  1196. );
  1197. }
  1198. /**
  1199. * Unsubscribe all users from one or more groups
  1200. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1201. * @return bool TRUE if successfull
  1202. */
  1203. public static function unsubscribe_all_users($group_ids)
  1204. {
  1205. $course_id = api_get_course_int_id();
  1206. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1207. $group_ids = array_map('intval', $group_ids);
  1208. if (count($group_ids) > 0) {
  1209. if (api_is_course_coach()) {
  1210. for ($i = 0; $i < count($group_ids); $i++) {
  1211. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1212. array_splice($group_ids, $i, 1);
  1213. $i--;
  1214. }
  1215. }
  1216. if (count($group_ids) == 0) {
  1217. return false;
  1218. }
  1219. }
  1220. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1221. $sql = 'DELETE FROM '.$table_group_user.' WHERE group_id IN ('.implode(
  1222. ',',
  1223. $group_ids
  1224. ).') AND c_id = '.$course_id;
  1225. $result = Database::query($sql);
  1226. return $result;
  1227. }
  1228. return true;
  1229. }
  1230. /**
  1231. * Unsubscribe all tutors from one or more groups
  1232. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1233. * @see unsubscribe_all_users. This function is almost an exact copy of that function.
  1234. * @return bool TRUE if successfull
  1235. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1236. */
  1237. public static function unsubscribe_all_tutors($group_ids)
  1238. {
  1239. $course_id = api_get_course_int_id();
  1240. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1241. if (count($group_ids) > 0) {
  1242. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1243. $sql = 'DELETE FROM '.$table_group_tutor.' WHERE group_id IN ('.implode(
  1244. ',',
  1245. $group_ids
  1246. ).') AND c_id = '.$course_id;
  1247. $result = Database::query($sql);
  1248. return $result;
  1249. }
  1250. return true;
  1251. }
  1252. /**
  1253. * Is the user a tutor of this group?
  1254. * @param $user_id the id of the user
  1255. * @param $group_id the id of the group
  1256. * @return boolean true/false
  1257. * @todo use the function user_has_access that includes this function
  1258. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1259. */
  1260. public static function is_tutor_of_group($user_id, $group_id)
  1261. {
  1262. $_course = api_get_course_info();
  1263. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1264. $user_id = Database::escape_string($user_id);
  1265. $group_id = Database::escape_string($group_id);
  1266. $course_id = api_get_course_int_id();
  1267. $sql = "SELECT * FROM ".$table_group_tutor." WHERE c_id = $course_id AND user_id='".$user_id."' AND group_id='".$group_id."'";
  1268. $result = Database::query($sql);
  1269. if (Database::num_rows($result) > 0) {
  1270. return true;
  1271. } else {
  1272. return false;
  1273. }
  1274. }
  1275. /**
  1276. * Is the user part of this group? This can be a tutor or a normal member
  1277. * you should use this function if the access to a tool or functionality is restricted to the people who are actually in the group
  1278. * before you had to check if the user was 1. a member of the group OR 2. a tutor of the group. This function combines both
  1279. * @param $user_id the id of the user
  1280. * @param $group_id the id of the group
  1281. * @return boolean true/false
  1282. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1283. */
  1284. public static function is_user_in_group($user_id, $group_id)
  1285. {
  1286. $member = self :: is_subscribed($user_id, $group_id);
  1287. $tutor = self :: is_tutor_of_group($user_id, $group_id);
  1288. if ($member OR $tutor) {
  1289. return true;
  1290. } else {
  1291. return false;
  1292. }
  1293. }
  1294. /**
  1295. * Get all tutors for the current course.
  1296. * @return array An array with firstname, lastname and user_id for all
  1297. * tutors in the current course.
  1298. * @deprecated this function uses the old tutor implementation
  1299. */
  1300. public static function get_all_tutors()
  1301. {
  1302. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1303. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1304. $sql = "SELECT user.user_id AS user_id, user.lastname AS lastname, user.firstname AS firstname
  1305. FROM ".$user_table." user, ".$course_user_table." cu
  1306. WHERE cu.user_id=user.user_id
  1307. AND cu.tutor_id='1'
  1308. AND cu.c_id='".api_get_course_int_id()."'";
  1309. $resultTutor = Database::query($sql);
  1310. $tutors = array();
  1311. while ($tutor = Database::fetch_array($resultTutor)) {
  1312. $tutors[] = $tutor;
  1313. }
  1314. return $tutors;
  1315. }
  1316. /**
  1317. * Is user a tutor in current course
  1318. * @param int $user_id
  1319. * @return bool TRUE if given user is a tutor in the current course.
  1320. * @deprecated this function uses the old tutor implementation
  1321. */
  1322. public static function is_tutor($user_id)
  1323. {
  1324. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1325. $user_id = Database::escape_string($user_id);
  1326. $sql = "SELECT tutor_id FROM ".$course_user_table."
  1327. WHERE user_id = '".$user_id."' AND c_id ='".api_get_course_int_id()."'"."AND tutor_id=1";
  1328. $db_result = Database::query($sql);
  1329. $result = (Database::num_rows($db_result) > 0);
  1330. return $result;
  1331. }
  1332. /**
  1333. * Get all group's from a given course in which a given user is unsubscribed
  1334. * @author Patrick Cool
  1335. * @param int course id
  1336. * retrieve the groups for
  1337. * @param integer $user_id: the ID of the user you want to know all its
  1338. * group memberships
  1339. */
  1340. public static function get_group_ids($course_id, $user_id)
  1341. {
  1342. $groups = array();
  1343. $tbl_group = Database::get_course_table(TABLE_GROUP_USER);
  1344. $tbl_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1345. $user_id = intval($user_id);
  1346. $course_id = intval($course_id);
  1347. $sql = "SELECT group_id FROM $tbl_group WHERE c_id = $course_id AND user_id = '$user_id'";
  1348. $groupres = Database::query($sql);
  1349. if ($groupres) {
  1350. while ($myrow = Database::fetch_array($groupres)) {
  1351. $groups[] = $myrow['group_id'];
  1352. }
  1353. }
  1354. //Also loading if i'm the tutor
  1355. $sql = "SELECT group_id FROM $tbl_group_tutor WHERE c_id = $course_id AND user_id = '$user_id'";
  1356. $groupres = Database::query($sql);
  1357. if ($groupres) {
  1358. while ($myrow = Database::fetch_array($groupres)) {
  1359. $groups[] = $myrow['group_id'];
  1360. }
  1361. }
  1362. if (!empty($groups)) {
  1363. array_filter($groups);
  1364. }
  1365. return $groups;
  1366. }
  1367. /*
  1368. Group functions - these take virtual/linked courses into account when necessary
  1369. */
  1370. /**
  1371. * Filter out duplicates in a multidimensional array
  1372. * by comparing field $compare_field.
  1373. *
  1374. * @param $user_array_in list of users (must be sorted).
  1375. * @param string $compare_field, the field to be compared
  1376. */
  1377. public static function filter_duplicates($user_array_in, $compare_field)
  1378. {
  1379. $total_number = count($user_array_in);
  1380. $user_array_out[0] = $user_array_in[0];
  1381. $count_out = 0;
  1382. for ($count_in = 1; $count_in < $total_number; $count_in++) {
  1383. if ($user_array_in[$count_in][$compare_field] != $user_array_out[$count_out][$compare_field]) {
  1384. $count_out++;
  1385. $user_array_out[$count_out] = $user_array_in[$count_in];
  1386. }
  1387. }
  1388. return $user_array_out;
  1389. }
  1390. /**
  1391. * Filters from the array $user_array_in the users already in the group $group_id.
  1392. */
  1393. public static function filter_users_already_in_group($user_array_in, $group_id)
  1394. {
  1395. foreach ($user_array_in as $this_user) {
  1396. if (!self :: is_subscribed($this_user['user_id'], $group_id)) {
  1397. $user_array_out[] = $this_user;
  1398. }
  1399. }
  1400. return $user_array_out;
  1401. }
  1402. /**
  1403. * Remove all users that are not students and all users who have tutor status
  1404. * from the list.
  1405. */
  1406. public static function filter_only_students($user_array_in)
  1407. {
  1408. $user_array_out = array();
  1409. foreach ($user_array_in as $this_user) {
  1410. //if ($this_user['status_rel'] == STUDENT && $this_user['tutor_id'] == 0) {
  1411. if (api_get_session_id()) {
  1412. if ($this_user['status_session'] == 0) {
  1413. $user_array_out[] = $this_user;
  1414. }
  1415. } else {
  1416. if ($this_user['status_rel'] == STUDENT) {
  1417. $user_array_out[] = $this_user;
  1418. }
  1419. }
  1420. }
  1421. return $user_array_out;
  1422. }
  1423. /**
  1424. * Check if a user has access to a certain group tool
  1425. * @param int $user_id The user id
  1426. * @param int $group_id The group id
  1427. * @param constant $tool The tool to check the access rights. This should be
  1428. * one of constants: GROUP_TOOL_DOCUMENTS
  1429. * @return bool True if the given user has access to the given tool in the
  1430. * given course.
  1431. */
  1432. public static function user_has_access($user_id, $group_id, $tool)
  1433. {
  1434. //Admin have access everywhere
  1435. if (api_is_platform_admin()) {
  1436. return true;
  1437. }
  1438. //Course admin also have access to everything
  1439. if (api_is_allowed_to_edit()) {
  1440. return true;
  1441. }
  1442. switch ($tool) {
  1443. case self::GROUP_TOOL_FORUM :
  1444. $state_key = 'forum_state';
  1445. break;
  1446. case self::GROUP_TOOL_DOCUMENTS :
  1447. $state_key = 'doc_state';
  1448. break;
  1449. case self::GROUP_TOOL_CALENDAR :
  1450. $state_key = 'calendar_state';
  1451. break;
  1452. case self::GROUP_TOOL_ANNOUNCEMENT :
  1453. $state_key = 'announcements_state';
  1454. break;
  1455. case self::GROUP_TOOL_WORK :
  1456. $state_key = 'work_state';
  1457. break;
  1458. case self::GROUP_TOOL_WIKI :
  1459. $state_key = 'wiki_state';
  1460. break;
  1461. case self::GROUP_TOOL_CHAT :
  1462. $state_key = 'chat_state';
  1463. break;
  1464. default:
  1465. return false;
  1466. }
  1467. $user_is_in_group = self :: is_user_in_group($user_id, $group_id);
  1468. //Check group properties
  1469. $group_info = self :: get_group_properties($group_id);
  1470. if (api_get_setting('allow_group_categories') == 'true') {
  1471. //Check group category if exists
  1472. $category_group_info = self::get_category_from_group($group_id);
  1473. if (!empty($category_group_info)) {
  1474. //if exists check the category group status first
  1475. if ($category_group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1476. return false;
  1477. } elseif ($category_group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1478. return false;
  1479. }
  1480. }
  1481. }
  1482. //is_user_in_group() is more complete that the is_subscribed() function
  1483. if ($group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1484. return false;
  1485. } elseif ($group_info[$state_key] == self::TOOL_PUBLIC) {
  1486. return true;
  1487. } elseif (api_is_allowed_to_edit(false, true)) {
  1488. return true;
  1489. } elseif ($group_info['tutor_id'] == $user_id) { //this tutor implementation was dropped
  1490. return true;
  1491. } elseif ($group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1492. return false;
  1493. } else {
  1494. return $user_is_in_group;
  1495. }
  1496. }
  1497. /**
  1498. * Get all groups where a specific user is subscribed
  1499. */
  1500. public static function get_user_group_name($user_id)
  1501. {
  1502. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1503. $table_group = Database::get_course_table(TABLE_GROUP);
  1504. $user_id = intval($user_id);
  1505. $course_id = api_get_course_int_id();
  1506. $sql_groups = "SELECT name FROM $table_group g INNER JOIN $table_group_user gu
  1507. ON (gu.group_id=g.id)
  1508. WHERE gu.c_id= $course_id AND
  1509. g.c_id= $course_id AND
  1510. gu.user_id = $user_id";
  1511. $res = Database::query($sql_groups);
  1512. $groups = array();
  1513. while ($group = Database::fetch_array($res)) {
  1514. $groups[] .= $group['name'];
  1515. }
  1516. return $groups;
  1517. }
  1518. /**
  1519. * fill_groups_list
  1520. *
  1521. * see : fill_groups
  1522. * Fill the groups with students.
  1523. *
  1524. * note :
  1525. * optimize fill_groups_list <--> fill_groups
  1526. *
  1527. */
  1528. public static function fill_groups_list($group_ids)
  1529. {
  1530. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1531. $group_ids = array_map('intval', $group_ids);
  1532. if (api_is_course_coach()) {
  1533. for ($i = 0; $i < count($group_ids); $i++) {
  1534. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1535. array_splice($group_ids, $i, 1);
  1536. $i--;
  1537. }
  1538. }
  1539. if (count($group_ids) == 0) {
  1540. return false;
  1541. }
  1542. }
  1543. $_course = api_get_course_info();
  1544. $category = self :: get_category_from_group($group_ids[0]);
  1545. $groups_per_user = $category['groups_per_user'];
  1546. $group_table = Database :: get_course_table(TABLE_GROUP);
  1547. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  1548. $session_id = api_get_session_id();
  1549. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['sysCode'], true, $session_id);
  1550. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  1551. $course_id = api_get_course_int_id();
  1552. /*
  1553. * Retrieve all the groups where enrollment is still allowed
  1554. * (reverse) ordered by the number of place available
  1555. */
  1556. $sql = "SELECT g.id gid, g.max_student - count(ug.user_id) nbPlaces, g.max_student
  1557. FROM ".$group_table." g
  1558. LEFT JOIN ".$group_user_table." ug
  1559. ON g.id = ug.group_id
  1560. WHERE g.c_id = $course_id AND
  1561. ug.c_id = $course_id AND
  1562. g.id IN (".implode(',', $group_ids).")
  1563. GROUP BY (g.id)
  1564. HAVING (nbPlaces > 0 OR g.max_student = ".self::MEMBER_PER_GROUP_NO_LIMIT.")
  1565. ORDER BY nbPlaces DESC";
  1566. $sql_result = Database::query($sql);
  1567. $group_available_place = array();
  1568. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  1569. $group_available_place[$group['gid']] = $group['nbPlaces'];
  1570. }
  1571. /*
  1572. * Retrieve course users (reverse) ordered by the number
  1573. * of group they are already enrolled
  1574. */
  1575. for ($i = 0; $i < count($complete_user_list); $i++) {
  1576. //find # of groups the user is enrolled in
  1577. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"], $category['id']);
  1578. //add # of groups to user list
  1579. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  1580. }
  1581. //first sort by user_id to filter out duplicates
  1582. $complete_user_list = TableSort :: sort_table($complete_user_list, 'user_id');
  1583. $complete_user_list = self :: filter_duplicates($complete_user_list, 'user_id');
  1584. //now sort by # of group left
  1585. $complete_user_list = TableSort :: sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  1586. return $complete_user_list;
  1587. }
  1588. static function process_groups($group_list, $category_id = null)
  1589. {
  1590. global $origin, $charset;
  1591. $category_id = intval($category_id);
  1592. $totalRegistered = 0;
  1593. $group_data = array();
  1594. $user_info = api_get_user_info();
  1595. $session_id = api_get_session_id();
  1596. $user_id = $user_info['user_id'];
  1597. $orig = isset($origin) ? $origin : null;
  1598. foreach ($group_list as $this_group) {
  1599. // Validacion when belongs to a session
  1600. $session_img = api_get_session_image($this_group['session_id'], $user_info['status']);
  1601. // All the tutors of this group
  1602. $tutorsids_of_group = self::get_subscribed_tutors($this_group['id'], true);
  1603. // Create a new table-row
  1604. $row = array();
  1605. // Checkbox
  1606. if (api_is_allowed_to_edit(false, true) && count($group_list) > 1) {
  1607. $row[] = $this_group['id'];
  1608. }
  1609. // Group name
  1610. if ((api_is_allowed_to_edit(false, true) ||
  1611. in_array($user_id, $tutorsids_of_group) ||
  1612. $this_group['is_member'] ||
  1613. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_FORUM) ||
  1614. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_DOCUMENTS) ||
  1615. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_CALENDAR) ||
  1616. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_ANNOUNCEMENT) ||
  1617. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WORK) ||
  1618. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WIKI))
  1619. && !(api_is_course_coach() && intval($this_group['session_id']) != $session_id)
  1620. ) {
  1621. $group_name = '<a href="group_space.php?cidReq='.api_get_course_id(
  1622. ).'&amp;origin='.$orig.'&amp;gidReq='.$this_group['id'].'">'.Security::remove_XSS(
  1623. $this_group['name']
  1624. ).'</a> ';
  1625. if (!empty($user_id) && !empty($this_group['id_tutor']) && $user_id == $this_group['id_tutor']) {
  1626. $group_name .= Display::label(get_lang('OneMyGroups'), 'success');
  1627. } elseif ($this_group['is_member']) {
  1628. $group_name .= Display::label(get_lang('MyGroup'), 'success');
  1629. }
  1630. if (api_is_allowed_to_edit() && !empty($this_group['session_name'])) {
  1631. $group_name .= ' ('.$this_group['session_name'].')';
  1632. }
  1633. $group_name .= $session_img;
  1634. $row[] = $group_name.'<br />'.stripslashes(trim($this_group['description']));
  1635. } else {
  1636. $row[] = $this_group['name'].'<br />'.stripslashes(trim($this_group['description']));
  1637. }
  1638. // Tutor name
  1639. $tutor_info = null;
  1640. if (count($tutorsids_of_group) > 0) {
  1641. foreach ($tutorsids_of_group as $tutor_id) {
  1642. $tutor = api_get_user_info($tutor_id);
  1643. $username = api_htmlentities(sprintf(get_lang('LoginX'), $tutor['username']), ENT_QUOTES);
  1644. if (api_get_setting('show_email_addresses') == 'true') {
  1645. $tutor_info .= Display::tag(
  1646. 'span',
  1647. Display::encrypted_mailto_link(
  1648. $tutor['mail'],
  1649. api_get_person_name($tutor['firstName'], $tutor['lastName'])
  1650. ),
  1651. array('title' => $username)
  1652. ).', ';
  1653. } else {
  1654. if (api_is_allowed_to_edit()) {
  1655. $tutor_info .= Display::tag(
  1656. 'span',
  1657. Display::encrypted_mailto_link(
  1658. $tutor['mail'],
  1659. api_get_person_name($tutor['firstName'], $tutor['lastName'])
  1660. ),
  1661. array('title' => $username)
  1662. ).', ';
  1663. } else {
  1664. $tutor_info .= Display::tag(
  1665. 'span',
  1666. api_get_person_name($tutor['firstName'], $tutor['lastName']),
  1667. array('title' => $username)
  1668. ).', ';
  1669. }
  1670. }
  1671. }
  1672. }
  1673. $tutor_info = api_substr($tutor_info, 0, api_strlen($tutor_info) - 2);
  1674. $row[] = $tutor_info;
  1675. // Max number of members in group
  1676. $max_members = ($this_group['maximum_number_of_members'] == self::MEMBER_PER_GROUP_NO_LIMIT ? ' ' : ' / '.$this_group['maximum_number_of_members']);
  1677. // Number of members in group
  1678. $row[] = $this_group['number_of_members'].$max_members;
  1679. // Self-registration / unregistration
  1680. if (!api_is_allowed_to_edit(false, true)) {
  1681. if (self :: is_self_registration_allowed($user_id, $this_group['id'])) {
  1682. $row[] = '<a class = "btn" href="group.php?'.api_get_cidreq(
  1683. ).'&category='.$category_id.'&amp;action=self_reg&amp;group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(
  1684. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)
  1685. )."'".')) return false;">'.get_lang('GroupSelfRegInf').'</a>';
  1686. } elseif (self :: is_self_unregistration_allowed($user_id, $this_group['id'])) {
  1687. $row[] = '<a class = "btn" href="group.php?'.api_get_cidreq(
  1688. ).'&category='.$category_id.'&amp;action=self_unreg&amp;group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(
  1689. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)
  1690. )."'".')) return false;">'.get_lang('GroupSelfUnRegInf').'</a>';
  1691. } else {
  1692. $row[] = '-';
  1693. }
  1694. }
  1695. // Edit-links
  1696. if (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && intval(
  1697. $this_group['session_id']
  1698. ) != $session_id)
  1699. ) {
  1700. $edit_actions = '<a href="group_edit.php?'.api_get_cidreq(
  1701. true,
  1702. false
  1703. ).'&gidReq='.$this_group['id'].'" title="'.get_lang('Edit').'">'.Display::return_icon(
  1704. 'edit.png',
  1705. get_lang('EditGroup'),
  1706. '',
  1707. ICON_SIZE_SMALL
  1708. ).'</a>&nbsp;';
  1709. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  1710. true,
  1711. false
  1712. ).'&category='.$category_id.'&amp;action=empty_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(
  1713. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1714. )."'".')) return false;" title="'.get_lang('EmptyGroup').'">'.
  1715. Display::return_icon('clean.png', get_lang('EmptyGroup'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1716. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  1717. true,
  1718. false
  1719. ).'&category='.$category_id.'&amp;action=fill_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(
  1720. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1721. )."'".')) return false;" title="'.get_lang('FillGroup').'">'.
  1722. Display::return_icon('fill.png', get_lang('FillGroup'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1723. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  1724. true,
  1725. false
  1726. ).'&category='.$category_id.'&amp;action=delete_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(
  1727. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1728. )."'".')) return false;" title="'.get_lang('Delete').'">'.
  1729. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1730. $row[] = $edit_actions;
  1731. }
  1732. if (!empty($this_group['nbMember'])) {
  1733. $totalRegistered = $totalRegistered + $this_group['nbMember'];
  1734. }
  1735. $group_data[] = $row;
  1736. } // end loop
  1737. if (isset($_GET['show_all'])) {
  1738. $paging_options = array('per_page' => count($group_data));
  1739. } else {
  1740. $paging_options = array();
  1741. }
  1742. $table = new SortableTableFromArrayConfig($group_data, 1, 20, 'group_category_'.$category_id);
  1743. //$my_cat = isset($_GET['category']) ? Security::remove_XSS($_GET['category']) : null;
  1744. $table->set_additional_parameters(array('category' => $category_id));
  1745. $column = 0;
  1746. if (api_is_allowed_to_edit(false, true) and count($group_list) > 1) {
  1747. $table->set_header($column++, '', false);
  1748. }
  1749. $table->set_header($column++, get_lang('Groups'));
  1750. $table->set_header($column++, get_lang('GroupTutor'));
  1751. $table->set_header($column++, get_lang('Registered'), false);
  1752. if (!api_is_allowed_to_edit(false, true)) { // If self-registration allowed
  1753. $table->set_header($column++, get_lang('GroupSelfRegistration'), false);
  1754. }
  1755. if (api_is_allowed_to_edit(false, true)) { // Only for course administrator
  1756. $table->set_header($column++, get_lang('Modify'), false);
  1757. $form_actions = array();
  1758. $form_actions['fill_selected'] = get_lang('FillGroup');
  1759. $form_actions['empty_selected'] = get_lang('EmptyGroup');
  1760. $form_actions['delete_selected'] = get_lang('Delete');
  1761. if (count($group_list) > 1) {
  1762. $table->set_form_actions($form_actions, 'group');
  1763. }
  1764. }
  1765. $table->display();
  1766. }
  1767. }