groupmanager.lib.php 61 KB

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