groupmanager.lib.php 60 KB

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