groupmanager.lib.php 61 KB

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