groupmanager.lib.php 62 KB

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