groupmanager.lib.php 84 KB

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