groupmanager.lib.php 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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=='') {
  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. }
  133. } else {
  134. $session_condition = api_get_session_condition($session_id, true);
  135. }
  136. $sql .= " AND g.c_id = $course_id ";
  137. if (!empty($session_condition)) {
  138. $sql .= $session_condition;
  139. }
  140. $sql .= " GROUP BY g.id ORDER BY UPPER(g.name)";
  141. if ($can_see_groups == 1) {
  142. $groupList = Database::query($sql);
  143. } else {
  144. return array();
  145. }
  146. $groups = array();
  147. while ($thisGroup = Database::fetch_array($groupList)) {
  148. $thisGroup['number_of_members'] = count(self::get_subscribed_users($thisGroup['id']));
  149. if ($thisGroup['session_id']!=0) {
  150. $sql = 'SELECT name FROM '.Database::get_main_table(TABLE_MAIN_SESSION).'
  151. WHERE id='.$thisGroup['session_id'];
  152. $rs_session = Database::query($sql);
  153. if (Database::num_rows($rs_session)>0) {
  154. $thisGroup['session_name'] = Database::result($rs_session, 0, 0);
  155. }
  156. }
  157. $groups[] = $thisGroup;
  158. }
  159. return $groups;
  160. }
  161. /**
  162. * Create a group
  163. * @param string $name The name for this group
  164. * @param int $tutor The user-id of the group's tutor
  165. * @param int $places How many people can subscribe to the new group
  166. */
  167. public static function create_group($name, $category_id, $tutor, $places)
  168. {
  169. $_course = api_get_course_info();
  170. $table_group = Database :: get_course_table(TABLE_GROUP);
  171. $session_id = api_get_session_id();
  172. $course_id = api_get_course_int_id();
  173. $currentCourseRepository = $_course['path'];
  174. $category = self :: get_category($category_id);
  175. $places = intval($places);
  176. if ($places == 0) {
  177. //if the amount of users per group is not filled in, use the setting from the category
  178. $places = $category['max_student'];
  179. } else {
  180. if ($places > $category['max_student'] && $category['max_student'] != 0) {
  181. $places = $category['max_student'];
  182. }
  183. }
  184. $sql = "INSERT INTO ".$table_group." SET
  185. c_id = $course_id ,
  186. category_id='".Database::escape_string($category_id)."',
  187. max_student = '".$places."',
  188. doc_state = '".$category['doc_state']."',
  189. calendar_state = '".$category['calendar_state']."',
  190. work_state = '".$category['work_state']."',
  191. announcements_state = '".$category['announcements_state']."',
  192. forum_state = '".$category['forum_state']."',
  193. wiki_state = '".$category['wiki_state']."',
  194. chat_state = '".$category['chat_state']."',
  195. self_registration_allowed = '".$category['self_reg_allowed']."',
  196. self_unregistration_allowed = '".$category['self_unreg_allowed']."',
  197. session_id='".Database::escape_string($session_id)."'";
  198. Database::query($sql);
  199. $lastId = Database::insert_id();
  200. if ($lastId) {
  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, api_get_user_id(), $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."'
  206. WHERE c_id = $course_id AND id ='".$lastId."'";
  207. Database::query($sql);
  208. // create a forum if needed
  209. if ($category['forum_state'] >= 0) {
  210. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  211. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  212. $forum_categories = get_forum_categories();
  213. $values = array();
  214. $values['forum_title'] = $name;
  215. $values['group_id'] = $lastId;
  216. $counter = 0;
  217. foreach ($forum_categories as $key=>$value) {
  218. if ($counter==0) {
  219. $forum_category_id = $key;
  220. }
  221. $counter++;
  222. }
  223. // A sanity check.
  224. if (empty($forum_category_id)) {
  225. $forum_category_id = 0;
  226. }
  227. $values['forum_category'] = $forum_category_id;
  228. $values['allow_anonymous_group']['allow_anonymous'] = 0;
  229. $values['students_can_edit_group']['students_can_edit'] = 0;
  230. $values['approval_direct_group']['approval_direct'] = 0;
  231. $values['allow_attachments_group']['allow_attachments'] = 1;
  232. $values['allow_new_threads_group']['allow_new_threads'] = 1;
  233. $values['default_view_type_group']['default_view_type']=api_get_setting('default_forum_view');
  234. $values['group_forum'] = $lastId;
  235. if ($category['forum_state'] == '1') {
  236. $values['public_private_group_forum_group']['public_private_group_forum']='public';
  237. } elseif ($category['forum_state'] == '2') {
  238. $values['public_private_group_forum_group']['public_private_group_forum']='private';
  239. } elseif ($category['forum_state'] == '0') {
  240. $values['public_private_group_forum_group']['public_private_group_forum']='unavailable';
  241. }
  242. store_forum($values);
  243. }
  244. }
  245. return $lastId;
  246. }
  247. /**
  248. * Create subgroups.
  249. * This function creates new groups based on an existing group. It will
  250. * create the specified number of groups and fill those groups with users
  251. * from the base group
  252. * @param int $group_id The group from which subgroups have to be created.
  253. * @param int $number_of_groups The number of groups that have to be created
  254. */
  255. public static function create_subgroups($group_id, $number_of_groups)
  256. {
  257. $course_id = api_get_course_int_id();
  258. $table_group = Database :: get_course_table(TABLE_GROUP);
  259. $category_id = self :: create_category('Subgroups', '', self::TOOL_PRIVATE, self::TOOL_PRIVATE, 0, 0, 1, 1);
  260. $users = self :: get_users($group_id);
  261. $group_ids = array ();
  262. for ($group_nr = 1; $group_nr <= $number_of_groups; $group_nr ++) {
  263. $group_ids[] = self :: create_group('SUBGROUP '.$group_nr, $category_id, 0, 0);
  264. }
  265. $members = array ();
  266. foreach ($users as $index => $user_id) {
  267. self :: subscribe_users($user_id, $group_ids[$index % $number_of_groups]);
  268. $members[$group_ids[$index % $number_of_groups]]++;
  269. }
  270. foreach ($members as $group_id => $places) {
  271. $sql = "UPDATE $table_group SET max_student = $places WHERE c_id = $course_id AND id = $group_id";
  272. Database::query($sql);
  273. }
  274. }
  275. /**
  276. * Create groups from all virtual courses in the given course.
  277. * @deprecated
  278. */
  279. public static function create_groups_from_virtual_courses()
  280. {
  281. self :: delete_category(self::VIRTUAL_COURSE_CATEGORY);
  282. $id = self :: create_category(get_lang('GroupsFromVirtualCourses'), '', self::TOOL_NOT_AVAILABLE, self::TOOL_NOT_AVAILABLE, 0, 0, 1, 1);
  283. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  284. $course_id = api_get_course_int_id();
  285. $sql = "UPDATE ".$table_group_cat." SET id=".self::VIRTUAL_COURSE_CATEGORY." WHERE c_id = $course_id AND id=$id";
  286. Database::query($sql);
  287. $course = api_get_course_info();
  288. $course['code'] = $course['sysCode'];
  289. $course['title'] = $course['name'];
  290. $virtual_courses = CourseManager :: get_virtual_courses_linked_to_real_course($course['sysCode']);
  291. $group_courses = $virtual_courses;
  292. $group_courses[] = $course;
  293. $ids = array ();
  294. foreach ($group_courses as $index => $group_course) {
  295. $users = CourseManager :: get_user_list_from_course_code($group_course['code']);
  296. $members = array ();
  297. foreach ($users as $index => $user) {
  298. if ($user['status'] == 5 && $user['tutor_id'] == 0) {
  299. $members[] = $user['user_id'];
  300. }
  301. }
  302. $id = self :: create_group($group_course['code'], self::VIRTUAL_COURSE_CATEGORY, 0, count($members));
  303. self :: subscribe_users($members, $id);
  304. $ids[] = $id;
  305. }
  306. return $ids;
  307. }
  308. /**
  309. * Create a group for every class subscribed to the current course
  310. * @param int $category_id The category in which the groups should be created
  311. */
  312. public static function create_class_groups($category_id)
  313. {
  314. $options['where'] = array(" usergroup.course_id = ? " => api_get_real_course_id());
  315. $obj = new UserGroup();
  316. $classes = $obj->get_usergroup_in_course($options);
  317. $group_ids = array();
  318. foreach ($classes as $class) {
  319. $users_ids = $obj->get_users_by_usergroup($class['id']);
  320. $group_id = self::create_group($class['name'],$category_id,0,count($users_ids));
  321. self::subscribe_users($users_ids,$group_id);
  322. $group_ids[] = $group_id;
  323. }
  324. return $group_ids;
  325. }
  326. /**
  327. * deletes groups and their data.
  328. * @author Christophe Gesche <christophe.gesche@claroline.net>
  329. * @author Hugues Peeters <hugues.peeters@claroline.net>
  330. * @author Bart Mollet
  331. * @param mixed $groupIdList - group(s) to delete. It can be a single id
  332. * (int) or a list of id (array).
  333. * @param string $course_code Default is current course
  334. * @return integer - number of groups deleted.
  335. */
  336. public static function delete_groups($group_ids, $course_code = null)
  337. {
  338. $course_info = api_get_course_info($course_code);
  339. $course_id = $course_info['real_id'];
  340. // Database table definitions
  341. $group_table = Database :: get_course_table(TABLE_GROUP);
  342. $forum_table = Database :: get_course_table(TABLE_FORUM);
  343. $group_ids = is_array($group_ids) ? $group_ids : array ($group_ids);
  344. $group_ids = array_map('intval',$group_ids);
  345. if (api_is_course_coach()) {
  346. //a coach can only delete courses from his session
  347. for ($i=0 ; $i<count($group_ids) ; $i++) {
  348. if (!api_is_element_in_the_session(TOOL_GROUP,$group_ids[$i])) {
  349. array_splice($group_ids,$i,1);
  350. $i--;
  351. }
  352. }
  353. if (count($group_ids)==0)
  354. return 0;
  355. }
  356. // Unsubscribe all users
  357. self :: unsubscribe_all_users($group_ids);
  358. $sql = "SELECT id, secret_directory, session_id FROM $group_table
  359. WHERE c_id = $course_id AND id IN (".implode(' , ', $group_ids).")";
  360. $db_result = Database::query($sql);
  361. while ($group = Database::fetch_object($db_result)) {
  362. // move group-documents to garbage
  363. $source_directory = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$group->secret_directory;
  364. //File to renamed
  365. $destination_dir = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$group->secret_directory.'_DELETED_'.$group->id;
  366. if (!empty($group->secret_directory)) {
  367. //Deleting from document tool
  368. DocumentManager::delete_document($course_info, $group->secret_directory, $source_directory);
  369. if (file_exists($source_directory)) {
  370. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  371. // Delete
  372. my_delete($source_directory);
  373. } else {
  374. // Rename
  375. rename($source_directory, $destination_dir);
  376. }
  377. }
  378. }
  379. }
  380. // delete the groups
  381. $sql = "DELETE FROM ".$group_table." WHERE c_id = $course_id AND id IN ('".implode("' , '", $group_ids)."')";
  382. Database::query($sql);
  383. $sql = "DELETE FROM ".$forum_table." WHERE c_id = $course_id AND forum_of_group IN ('".implode("' , '", $group_ids)."')";
  384. Database::query($sql);
  385. return Database::affected_rows();
  386. }
  387. /**
  388. * Get group properties
  389. * @param int $group_id The group from which properties are requested.
  390. * @return array All properties. Array-keys are name, tutor_id, description, maximum_number_of_students, directory and visibility of tools
  391. */
  392. public static function get_group_properties($group_id)
  393. {
  394. $course_id = api_get_course_int_id();
  395. if (empty($group_id) or !is_integer(intval($group_id))) {
  396. return null;
  397. }
  398. $result = array();
  399. $table_group = Database :: get_course_table(TABLE_GROUP);
  400. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id AND id = ".intval($group_id);
  401. $db_result = Database::query($sql);
  402. $db_object = Database::fetch_object($db_result);
  403. $result['id'] = $db_object->id;
  404. $result['name'] = $db_object->name;
  405. $result['tutor_id'] = isset($db_object->tutor_id)?$db_object->tutor_id:null;
  406. $result['description'] = $db_object->description;
  407. $result['maximum_number_of_students'] = $db_object->max_student;
  408. $result['max_student'] = $db_object->max_student;
  409. $result['doc_state'] = $db_object->doc_state;
  410. $result['work_state'] = $db_object->work_state;
  411. $result['calendar_state'] = $db_object->calendar_state;
  412. $result['announcements_state'] = $db_object->announcements_state;
  413. $result['forum_state'] = $db_object->forum_state;
  414. $result['wiki_state'] = $db_object->wiki_state;
  415. $result['chat_state'] = $db_object->chat_state;
  416. $result['directory'] = $db_object->secret_directory;
  417. $result['self_registration_allowed'] = $db_object->self_registration_allowed;
  418. $result['self_unregistration_allowed'] = $db_object->self_unregistration_allowed;
  419. $result['count_users'] = count(self::get_subscribed_users($group_id));
  420. $result['count_tutor'] = count(self::get_subscribed_tutors($group_id));
  421. $result['count_all'] = $result['count_users'] + $result['count_tutor'];
  422. return $result;
  423. }
  424. /**
  425. * @param string $name
  426. * @param string $courseCode
  427. * @return array
  428. */
  429. public static function getGroupByName($name, $courseCode = null)
  430. {
  431. $name = trim($name);
  432. if (empty($name)) {
  433. return array();
  434. }
  435. $course_info = api_get_course_info($courseCode);
  436. $course_id = $course_info['real_id'];
  437. $name = Database::escape_string($name);
  438. $table_group = Database::get_course_table(TABLE_GROUP);
  439. $sql = "SELECT * FROM $table_group
  440. WHERE c_id = $course_id AND name = '$name' LIMIT 1";
  441. $res = Database::query($sql);
  442. $group = array();
  443. if (Database::num_rows($res)) {
  444. $group = Database::fetch_array($res, 'ASSOC');
  445. }
  446. return $group;
  447. }
  448. /**
  449. * @param int $courseId
  450. * @param int $categoryId
  451. * @param string $name
  452. * @return array
  453. */
  454. public static function getGroupListFilterByName($name, $categoryId, $courseId)
  455. {
  456. $name = trim($name);
  457. if (empty($name)) {
  458. return array();
  459. }
  460. $name = Database::escape_string($name);
  461. $courseId = intval($courseId);
  462. $table_group = Database::get_course_table(TABLE_GROUP);
  463. $sql = "SELECT * FROM $table_group
  464. WHERE c_id = $courseId AND name LIKE '%$name%'";
  465. if (!empty($categoryId)) {
  466. $categoryId = intval($categoryId);
  467. $sql .= " AND category_id = $categoryId";
  468. }
  469. $sql .= " ORDER BY name";
  470. $result = Database::query($sql);
  471. return Database::store_result($result, 'ASSOC');
  472. }
  473. /**
  474. * Set group properties
  475. * Changes the group's properties.
  476. * @param int Group Id
  477. * @param string Group name
  478. * @param string Group description
  479. * @param int Max number of students in group
  480. * @param int Document tool's visibility (0=none,1=private,2=public)
  481. * @param int Work tool's visibility (0=none,1=private,2=public)
  482. * @param int Calendar tool's visibility (0=none,1=private,2=public)
  483. * @param int Announcement tool's visibility (0=none,1=private,2=public)
  484. * @param int Forum tool's visibility (0=none,1=private,2=public)
  485. * @param int Wiki tool's visibility (0=none,1=private,2=public)
  486. * @param int Chat tool's visibility (0=none,1=private,2=public)
  487. * @param bool Whether self registration is allowed or not
  488. * @param bool Whether self unregistration is allowed or not
  489. * @return bool TRUE if properties are successfully changed, false otherwise
  490. */
  491. public static function set_group_properties(
  492. $group_id,
  493. $name,
  494. $description,
  495. $maximum_number_of_students,
  496. $doc_state,
  497. $work_state,
  498. $calendar_state,
  499. $announcements_state,
  500. $forum_state,
  501. $wiki_state,
  502. $chat_state,
  503. $self_registration_allowed,
  504. $self_unregistration_allowed
  505. ) {
  506. $table_group = Database :: get_course_table(TABLE_GROUP);
  507. $table_forum = Database :: get_course_table(TABLE_FORUM);
  508. //$forum_id = get_forums_of_group($group_id);
  509. $group_id = Database::escape_string($group_id);
  510. $category = self::get_category_from_group($group_id);
  511. $course_id = api_get_course_int_id();
  512. $sql = "UPDATE ".$table_group." SET
  513. name='".Database::escape_string(trim($name))."',
  514. doc_state = '".Database::escape_string($doc_state)."',
  515. work_state = '".Database::escape_string($work_state)."',
  516. calendar_state = '".Database::escape_string($calendar_state)."',
  517. announcements_state = '".Database::escape_string($announcements_state)."',
  518. forum_state = '".Database::escape_string($forum_state)."',
  519. wiki_state = '".Database::escape_string($wiki_state)."',
  520. chat_state = '".Database::escape_string($chat_state)."',
  521. description='".Database::escape_string(trim($description))."',
  522. max_student= '".Database::escape_string($maximum_number_of_students)."',
  523. self_registration_allowed='".Database::escape_string($self_registration_allowed)."',
  524. self_unregistration_allowed='".Database::escape_string($self_unregistration_allowed)."'
  525. WHERE c_id = $course_id AND id=".$group_id;
  526. $result = Database::query($sql);
  527. //Here we are updating a field in the table forum_forum that perhaps duplicates the table group_info.forum_state cvargas
  528. $forum_state = (int) $forum_state;
  529. $sql2 = "UPDATE ".$table_forum." SET ";
  530. if ($forum_state===1) {
  531. $sql2 .= " forum_group_public_private='public' ";
  532. } elseif ($forum_state===2) {
  533. $sql2 .= " forum_group_public_private='private' ";
  534. } elseif ($forum_state===0) {
  535. $sql2 .= " forum_group_public_private='unavailable' ";
  536. }
  537. $sql2 .=" WHERE c_id = $course_id AND forum_of_group=".$group_id;
  538. Database::query($sql2);
  539. return $result;
  540. }
  541. /**
  542. * Get the total number of groups for the current course.
  543. * @return int The number of groups for the current course.
  544. */
  545. public static function get_number_of_groups()
  546. {
  547. $course_id = api_get_course_int_id();
  548. $table_group = Database :: get_course_table(TABLE_GROUP);
  549. $res = Database::query("SELECT COUNT(id) AS number_of_groups FROM $table_group WHERE c_id = $course_id ");
  550. $obj = Database::fetch_object($res);
  551. return $obj->number_of_groups;
  552. }
  553. // GROUP CATEGORY FUNCTIONS
  554. /**
  555. * Get all categories
  556. * @param string $course_code The course (default = current course)
  557. * @return array
  558. */
  559. public static function get_categories($course_code = null)
  560. {
  561. $course_info = api_get_course_info($course_code);
  562. $course_id = $course_info['real_id'];
  563. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  564. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id ORDER BY display_order";
  565. $res = Database::query($sql);
  566. $cats = array ();
  567. while ($cat = Database::fetch_array($res)) {
  568. $cats[] = $cat;
  569. }
  570. return $cats;
  571. }
  572. /**
  573. * Get a group category
  574. * @param int $id The category id
  575. * @param string $course_code The course (default = current course)
  576. * @return array
  577. */
  578. public static function get_category($id, $course_code = null)
  579. {
  580. if (empty($id)) {
  581. return array();
  582. }
  583. $course_info = api_get_course_info($course_code);
  584. $course_id = $course_info['real_id'];
  585. $id = Database::escape_string($id);
  586. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  587. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id AND id = $id LIMIT 1";
  588. $res = Database::query($sql);
  589. return Database::fetch_array($res);
  590. }
  591. /**
  592. * Get a group category
  593. * @param string $title
  594. * @param string $course_code The course (default = current course)
  595. * @return array
  596. */
  597. public static function getCategoryByTitle($title, $course_code = null)
  598. {
  599. $title = trim($title);
  600. if (empty($title)) {
  601. return array();
  602. }
  603. $course_info = api_get_course_info($course_code);
  604. $course_id = $course_info['real_id'];
  605. $title = Database::escape_string($title);
  606. $table_group_cat = Database::get_course_table(TABLE_GROUP_CATEGORY);
  607. $sql = "SELECT * FROM $table_group_cat
  608. WHERE c_id = $course_id AND title = '$title' LIMIT 1";
  609. $res = Database::query($sql);
  610. $category = array();
  611. if (Database::num_rows($res)) {
  612. $category = Database::fetch_array($res, 'ASSOC');
  613. }
  614. return $category;
  615. }
  616. /**
  617. * Get the unique category of a given group
  618. * @param int $group_id The id of the group
  619. * @param string $course_code The course in which the group is (default =
  620. * current course)
  621. * @return array The category
  622. */
  623. public static function get_category_from_group($group_id, $course_code = null)
  624. {
  625. $table_group = Database :: get_course_table(TABLE_GROUP);
  626. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  627. if (empty($group_id)) {
  628. return array();
  629. }
  630. $course_info = api_get_course_info($course_code);
  631. $course_id = $course_info['real_id'];
  632. $group_id = Database::escape_string($group_id);
  633. $sql = "SELECT gc.* FROM $table_group_cat gc, $table_group g
  634. WHERE gc.c_id = $course_id AND
  635. g.c_id = $course_id AND
  636. gc.id = g.category_id AND g.id= $group_id LIMIT 1";
  637. $res = Database::query($sql);
  638. $cat = array();
  639. if (Database::num_rows($res)) {
  640. $cat = Database::fetch_array($res);
  641. }
  642. return $cat;
  643. }
  644. /**
  645. * Delete a group category
  646. * @param int $cat_id The id of the category to delete
  647. * @param string $course_code The code in which the category should be
  648. * deleted (default = current course)
  649. */
  650. public static function delete_category($cat_id, $course_code = null)
  651. {
  652. $course_info = api_get_course_info($course_code);
  653. $course_id = $course_info['real_id'];
  654. $table_group = Database :: get_course_table(TABLE_GROUP);
  655. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  656. $cat_id = Database::escape_string($cat_id);
  657. $sql = "SELECT id FROM $table_group WHERE c_id = $course_id AND category_id='".$cat_id."'";
  658. $res = Database::query($sql);
  659. if (Database::num_rows($res) > 0) {
  660. $groups_to_delete = array ();
  661. while ($group = Database::fetch_object($res)) {
  662. $groups_to_delete[] = $group->id;
  663. }
  664. self :: delete_groups($groups_to_delete);
  665. }
  666. $sql = "DELETE FROM $table_group_cat WHERE c_id = $course_id AND id='".$cat_id."'";
  667. Database::query($sql);
  668. }
  669. /**
  670. * Create group category
  671. * @param string $title The title of the new category
  672. * @param string $description The description of the new category
  673. * @param bool $self_registration_allowed
  674. * @param bool $self_unregistration_allowed
  675. * @param int $max_number_of_students
  676. * @param int $groups_per_user
  677. */
  678. public static function create_category(
  679. $title,
  680. $description,
  681. $doc_state,
  682. $work_state,
  683. $calendar_state,
  684. $announcements_state,
  685. $forum_state,
  686. $wiki_state,
  687. $chat_state = 1,
  688. $self_registration_allowed = 0,
  689. $self_unregistration_allowed = 0,
  690. $maximum_number_of_students = 8,
  691. $groups_per_user = 0
  692. ) {
  693. $table_group_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  694. $course_id = api_get_course_int_id();
  695. $sql = "SELECT MAX(display_order)+1 as new_order FROM $table_group_category WHERE c_id = $course_id ";
  696. $res = Database::query($sql);
  697. $obj = Database::fetch_object($res);
  698. if (!isset ($obj->new_order)) {
  699. $obj->new_order = 1;
  700. }
  701. $sql = "INSERT INTO ".$table_group_category." SET
  702. c_id = $course_id ,
  703. title='".Database::escape_string($title)."',
  704. display_order ='".$obj->new_order."',
  705. description='".Database::escape_string($description)."',
  706. doc_state = '".Database::escape_string($doc_state)."',
  707. work_state = '".Database::escape_string($work_state)."',
  708. calendar_state = '".Database::escape_string($calendar_state)."',
  709. announcements_state = '".Database::escape_string($announcements_state)."',
  710. forum_state = '".Database::escape_string($forum_state)."',
  711. wiki_state = '".Database::escape_string($wiki_state)."',
  712. chat_state = '".Database::escape_string($chat_state)."',
  713. groups_per_user = '".Database::escape_string($groups_per_user)."',
  714. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  715. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  716. max_student = '".Database::escape_string($maximum_number_of_students)."' ";
  717. Database::query($sql);
  718. $categoryId = Database::insert_id();
  719. if ($categoryId == self::VIRTUAL_COURSE_CATEGORY) {
  720. $sql = "UPDATE ".$table_group_category." SET id = ". ($categoryId +1)." WHERE c_id = $course_id AND id = $categoryId";
  721. Database::query($sql);
  722. return $categoryId +1;
  723. }
  724. return $categoryId;
  725. }
  726. /**
  727. * Update group category
  728. *
  729. * @param int $id
  730. * @param string $title
  731. * @param string $description
  732. * @param $doc_state
  733. * @param $work_state
  734. * @param $calendar_state
  735. * @param $announcements_state
  736. * @param $forum_state
  737. * @param $wiki_state
  738. * @param $chat_state
  739. * @param $self_registration_allowed
  740. * @param $self_unregistration_allowed
  741. * @param $maximum_number_of_students
  742. * @param $groups_per_user
  743. */
  744. public static function update_category(
  745. $id,
  746. $title,
  747. $description,
  748. $doc_state,
  749. $work_state,
  750. $calendar_state,
  751. $announcements_state,
  752. $forum_state,
  753. $wiki_state,
  754. $chat_state,
  755. $self_registration_allowed,
  756. $self_unregistration_allowed,
  757. $maximum_number_of_students,
  758. $groups_per_user
  759. ) {
  760. $table_group_category = Database::get_course_table(TABLE_GROUP_CATEGORY);
  761. $id = Database::escape_string($id);
  762. $course_id = api_get_course_int_id();
  763. $sql = "UPDATE ".$table_group_category."SET
  764. title='".Database::escape_string($title)."',
  765. description='".Database::escape_string($description)."',
  766. doc_state = '".Database::escape_string($doc_state)."',
  767. work_state = '".Database::escape_string($work_state)."',
  768. calendar_state = '".Database::escape_string($calendar_state)."',
  769. announcements_state = '".Database::escape_string($announcements_state)."',
  770. forum_state = '".Database::escape_string($forum_state)."',
  771. wiki_state = '".Database::escape_string($wiki_state)."',
  772. chat_state = '".Database::escape_string($chat_state)."',
  773. groups_per_user = '".Database::escape_string($groups_per_user)."',
  774. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  775. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  776. max_student = ".Database::escape_string($maximum_number_of_students)."
  777. WHERE c_id = $course_id AND id = $id";
  778. Database::query($sql);
  779. // Updating all groups inside this category
  780. $groups = self::get_group_list($id);
  781. if (!empty($groups)) {
  782. foreach ($groups as $group) {
  783. GroupManager::set_group_properties(
  784. $group['id'],
  785. $group['name'],
  786. $group['description'],
  787. $maximum_number_of_students,
  788. $doc_state,
  789. $work_state,
  790. $calendar_state,
  791. $announcements_state,
  792. $forum_state,
  793. $wiki_state,
  794. $chat_state,
  795. $self_registration_allowed,
  796. $self_unregistration_allowed
  797. );
  798. }
  799. }
  800. }
  801. /**
  802. * Returns the number of groups of the user with the greatest number of
  803. * subscriptions in the given category
  804. */
  805. public static function get_current_max_groups_per_user($category_id = null, $course_code = null)
  806. {
  807. $course_info = api_get_course_info ($course_code);
  808. $group_table = Database :: get_course_table(TABLE_GROUP);
  809. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  810. $sql = 'SELECT COUNT(gu.group_id) AS current_max FROM '.$group_user_table.' gu, '.$group_table.' g
  811. WHERE g.c_id = '.$course_info['real_id'].' AND gu.group_id = g.id ';
  812. if ($category_id != null) {
  813. $category_id = Database::escape_string($category_id);
  814. $sql .= ' AND g.category_id = '.$category_id;
  815. }
  816. $sql .= ' GROUP BY gu.user_id ORDER BY current_max DESC LIMIT 1';
  817. $res = Database::query($sql);
  818. $obj = Database::fetch_object($res);
  819. return $obj->current_max;
  820. }
  821. /**
  822. * Swaps the display-order of two categories
  823. * @param int $id1 The id of the first category
  824. * @param int $id2 The id of the second category
  825. */
  826. public static function swap_category_order($id1, $id2)
  827. {
  828. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  829. $id1 = Database::escape_string($id1);
  830. $id2 = Database::escape_string($id2);
  831. $course_id = api_get_course_int_id();
  832. $sql = "SELECT id,display_order FROM $table_group_cat WHERE id IN ($id1,$id2) AND c_id = $course_id ";
  833. $res = Database::query($sql);
  834. $cat1 = Database::fetch_object($res);
  835. $cat2 = Database::fetch_object($res);
  836. $sql = "UPDATE $table_group_cat SET display_order=$cat2->display_order WHERE id = $cat1->id AND c_id = $course_id ";
  837. Database::query($sql);
  838. $sql = "UPDATE $table_group_cat SET display_order=$cat1->display_order WHERE id = $cat2->id AND c_id = $course_id ";
  839. Database::query($sql);
  840. }
  841. // GROUP USERS FUNCTIONS
  842. /**
  843. * Get all users from a given group
  844. * @param int $group_id The group
  845. * @param bool $load_extra_info
  846. * @return array list of user id
  847. */
  848. public static function get_users($group_id, $load_extra_info = false)
  849. {
  850. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  851. $group_id = Database::escape_string($group_id);
  852. $course_id = api_get_course_int_id();
  853. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  854. $res = Database::query($sql);
  855. $users = array ();
  856. while ($obj = Database::fetch_object($res)) {
  857. if ($load_extra_info) {
  858. $users[] = api_get_user_info($obj->user_id);
  859. } else {
  860. $users[] = $obj->user_id;
  861. }
  862. }
  863. return $users;
  864. }
  865. /**
  866. * @param int $group_id
  867. * @return array
  868. */
  869. public static function getStudentsAndTutors($group_id)
  870. {
  871. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  872. $tutor_user_table = Database :: get_course_table(TABLE_GROUP_TUTOR);
  873. $course_id = api_get_course_int_id();
  874. $group_id = intval($group_id);
  875. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  876. $res = Database::query($sql);
  877. $users = array();
  878. while ($obj = Database::fetch_object($res)) {
  879. $users[] = api_get_user_info($obj->user_id);
  880. }
  881. $sql = "SELECT user_id FROM $tutor_user_table WHERE c_id = $course_id AND group_id = $group_id";
  882. $res = Database::query($sql);
  883. while ($obj = Database::fetch_object($res)) {
  884. $users[] = api_get_user_info($obj->user_id);
  885. }
  886. return $users;
  887. }
  888. /**
  889. * Get only tutors from a group
  890. * @param int $group_id
  891. * @return array
  892. */
  893. public static function getTutors($group_id)
  894. {
  895. $tutor_user_table = Database :: get_course_table(TABLE_GROUP_TUTOR);
  896. $course_id = api_get_course_int_id();
  897. $group_id = intval($group_id);
  898. $sql = "SELECT user_id FROM $tutor_user_table WHERE c_id = $course_id AND group_id = $group_id";
  899. $res = Database::query($sql);
  900. while ($obj = Database::fetch_object($res)) {
  901. $users[] = api_get_user_info($obj->user_id);
  902. }
  903. return $users;
  904. }
  905. /**
  906. * Get only students from a group (not tutors)
  907. * @param int $group_id
  908. * @return array
  909. */
  910. public static function getStudents($group_id)
  911. {
  912. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  913. $course_id = api_get_course_int_id();
  914. $group_id = intval($group_id);
  915. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  916. $res = Database::query($sql);
  917. $users = array();
  918. while ($obj = Database::fetch_object($res)) {
  919. $users[] = api_get_user_info($obj->user_id);
  920. }
  921. return $users;
  922. }
  923. /**
  924. * Returns users belonging to any of the group
  925. *
  926. * @param array $groups list of group ids
  927. * @return array list of user ids
  928. */
  929. public static function get_groups_users($groups = array())
  930. {
  931. $result = array();
  932. $tbl_group_user = Database::get_course_table(TABLE_GROUP_USER);
  933. $course_id = api_get_course_int_id();
  934. $groups = array_map('intval', $groups);
  935. $groups = implode(', ', $groups); //protect individual elements with surrounding quotes
  936. $sql = "SELECT DISTINCT user_id
  937. FROM $tbl_group_user gu
  938. WHERE c_id = $course_id AND gu.group_id IN ($groups)";
  939. $rs = Database::query($sql);
  940. while ($row = Database::fetch_array($rs)) {
  941. $result[] = $row['user_id'];
  942. }
  943. return $result;
  944. }
  945. /**
  946. * Fill the groups with students.
  947. * The algorithm takes care to first fill the groups with the least # of users.
  948. * Analysis
  949. * There was a problem with the "ALL" setting.
  950. * When max # of groups is set to all, the value is sometimes NULL and sometimes ALL
  951. * and in both cased the query does not work as expected.
  952. * Stupid solution (currently implemented: set ALL to a big number (INFINITE) and things are solved :)
  953. * Better solution: that's up to you.
  954. *
  955. * Note
  956. * Throughout Dokeos there is some confusion about "course id" and "course code"
  957. * The code is e.g. TEST101, but sometimes a variable that is called courseID also contains a course code string.
  958. * However, there is also a integer course_id that uniquely identifies the course.
  959. * ywarnier:> Now the course_id has been removed (25/1/2005)
  960. * The databases are als very inconsistent in this.
  961. *
  962. * @author Chrisptophe Gesche <christophe.geshe@claroline.net>,
  963. * Hugues Peeters <hugues.peeters@claroline.net> - original version
  964. * @author Roan Embrechts - virtual course support, code cleaning
  965. * @author Bart Mollet - code cleaning, use other GroupManager-functions
  966. * @return void
  967. */
  968. public static function fill_groups($group_ids)
  969. {
  970. global $_course;
  971. $group_ids = is_array($group_ids) ? $group_ids : array ($group_ids);
  972. $group_ids = array_map('intval', $group_ids);
  973. if (api_is_course_coach()) {
  974. for ($i=0 ; $i< count($group_ids) ; $i++) {
  975. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])){
  976. array_splice($group_ids,$i,1);
  977. $i--;
  978. }
  979. }
  980. if (count($group_ids)==0) {
  981. return false;
  982. }
  983. }
  984. $category = self::get_category_from_group($group_ids[0]);
  985. $groups_per_user = $category['groups_per_user'];
  986. $group_table = Database :: get_course_table(TABLE_GROUP);
  987. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  988. $session_id = api_get_session_id();
  989. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['code'], true, $session_id);
  990. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  991. /*
  992. * Retrieve all the groups where enrollment is still allowed
  993. * (reverse) ordered by the number of place available
  994. */
  995. $course_id = api_get_course_int_id();
  996. $sql = "SELECT g.id gid, g.max_student-count(ug.user_id) nbPlaces, g.max_student
  997. FROM ".$group_table." g
  998. LEFT JOIN ".$group_user_table." ug ON
  999. g.c_id = $course_id AND ug.c_id = $course_id AND g.id = ug.group_id
  1000. WHERE
  1001. g.id IN (".implode(',', $group_ids).")
  1002. GROUP BY (g.id)
  1003. HAVING (nbPlaces > 0 OR g.max_student = ".self::MEMBER_PER_GROUP_NO_LIMIT.")
  1004. ORDER BY nbPlaces DESC";
  1005. $sql_result = Database::query($sql);
  1006. $group_available_place = array();
  1007. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  1008. $group_available_place[$group['gid']] = $group['nbPlaces'];
  1009. }
  1010. /*
  1011. * Retrieve course users (reverse) ordered by the number
  1012. * of group they are already enrolled
  1013. */
  1014. for ($i = 0; $i < count($complete_user_list); $i ++) {
  1015. //find # of groups the user is enrolled in
  1016. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"], $category['id']);
  1017. //add # of groups to user list
  1018. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  1019. }
  1020. //first sort by user_id to filter out duplicates
  1021. $complete_user_list = TableSort :: sort_table($complete_user_list, 'user_id');
  1022. $complete_user_list = self :: filter_duplicates($complete_user_list, 'user_id');
  1023. $complete_user_list = self :: filter_only_students($complete_user_list);
  1024. //now sort by # of group left
  1025. $complete_user_list = TableSort :: sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  1026. $userToken = array ();
  1027. foreach ($complete_user_list as $this_user) {
  1028. if ($this_user['number_groups_left'] > 0) {
  1029. $userToken[$this_user['user_id']] = $this_user['number_groups_left'];
  1030. }
  1031. }
  1032. $changed = true;
  1033. while ($changed) {
  1034. $changed = false;
  1035. reset($group_available_place);
  1036. arsort($group_available_place);
  1037. reset($userToken);
  1038. arsort($userToken);
  1039. foreach ($group_available_place as $group_id => $place) {
  1040. foreach ($userToken as $user_id => $places) {
  1041. if (self :: can_user_subscribe($user_id, $group_id)) {
  1042. self :: subscribe_users($user_id, $group_id);
  1043. $group_available_place[$group_id]--;
  1044. unset($userToken[$user_id]);
  1045. $changed = true;
  1046. break;
  1047. }
  1048. }
  1049. if ($changed) {
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. /**
  1056. * Get the number of students in a group.
  1057. * @param int $group_id
  1058. * @return int Number of students in the given group.
  1059. */
  1060. public static function number_of_students($group_id, $course_id = null)
  1061. {
  1062. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1063. $group_id = Database::escape_string($group_id);
  1064. if (empty($course_id)) {
  1065. $course_id = api_get_course_int_id();
  1066. } else {
  1067. $course_id = intval($course_id);
  1068. }
  1069. $sql = "SELECT COUNT(*) AS number_of_students FROM $table_group_user WHERE c_id = $course_id AND group_id = $group_id";
  1070. $db_result = Database::query($sql);
  1071. $db_object = Database::fetch_object($db_result);
  1072. return $db_object->number_of_students;
  1073. }
  1074. /**
  1075. * Maximum number of students in a group
  1076. * @param int $group_id
  1077. * @return int Maximum number of students in the given group.
  1078. */
  1079. public static function maximum_number_of_students($group_id)
  1080. {
  1081. $table_group = Database :: get_course_table(TABLE_GROUP);
  1082. $group_id = Database::escape_string($group_id);
  1083. $course_id = api_get_course_int_id();
  1084. $db_result = Database::query("SELECT max_student FROM $table_group WHERE c_id = $course_id AND id = $group_id");
  1085. $db_object = Database::fetch_object($db_result);
  1086. if ($db_object->max_student == 0) {
  1087. return self::INFINITE;
  1088. }
  1089. return $db_object->max_student;
  1090. }
  1091. /**
  1092. * Number of groups of a user
  1093. * @param int $user_id
  1094. * @return int The number of groups the user is subscribed in.
  1095. */
  1096. public static function user_in_number_of_groups($user_id, $cat_id = null)
  1097. {
  1098. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1099. $table_group = Database :: get_course_table(TABLE_GROUP);
  1100. $user_id = Database::escape_string($user_id);
  1101. $cat_id = Database::escape_string($cat_id);
  1102. $course_id = api_get_course_int_id();
  1103. $cat_condition = '';
  1104. if (!empty($cat_id)) {
  1105. $cat_condition = " AND g.category_id = $cat_id ";
  1106. }
  1107. $sql = "SELECT COUNT(*) AS number_of_groups FROM $table_group_user gu, $table_group g
  1108. WHERE gu.c_id = $course_id AND
  1109. g.c_id = $course_id AND
  1110. gu.user_id = $user_id AND
  1111. g.id = gu.group_id $cat_condition";
  1112. $db_result = Database::query($sql);
  1113. $db_object = Database::fetch_object($db_result);
  1114. return $db_object->number_of_groups;
  1115. }
  1116. /**
  1117. * Is sef-registration allowed?
  1118. * @param int $user_id
  1119. * @param int $group_id
  1120. * @return bool TRUE if self-registration is allowed in the given group.
  1121. */
  1122. public static function is_self_registration_allowed($user_id, $group_id)
  1123. {
  1124. $course_id = api_get_course_int_id();
  1125. if (!$user_id > 0) {
  1126. return false;
  1127. }
  1128. $table_group = Database :: get_course_table(TABLE_GROUP);
  1129. $group_id = intval($group_id);
  1130. if (isset($group_id)) {
  1131. $sql = "SELECT self_registration_allowed FROM $table_group WHERE c_id = $course_id AND id = $group_id";
  1132. $db_result = Database::query($sql);
  1133. $db_object = Database::fetch_object($db_result);
  1134. return $db_object->self_registration_allowed == 1 && self :: can_user_subscribe($user_id, $group_id);
  1135. } else {
  1136. return false;
  1137. }
  1138. }
  1139. /**
  1140. * Is sef-unregistration allowed?
  1141. * @param int $user_id
  1142. * @param int $group_id
  1143. * @return bool TRUE if self-unregistration is allowed in the given group.
  1144. */
  1145. public static function is_self_unregistration_allowed($user_id, $group_id)
  1146. {
  1147. if (!$user_id > 0) {
  1148. return false;
  1149. }
  1150. $table_group = Database :: get_course_table(TABLE_GROUP);
  1151. $group_id = Database::escape_string($group_id);
  1152. $course_id = api_get_course_int_id();
  1153. $db_result = Database::query(
  1154. 'SELECT self_unregistration_allowed FROM '.$table_group.' WHERE c_id = '.$course_id.' AND id = '.$group_id
  1155. );
  1156. $db_object = Database::fetch_object($db_result);
  1157. return $db_object->self_unregistration_allowed == 1 && self :: can_user_unsubscribe($user_id, $group_id);
  1158. }
  1159. /**
  1160. * Is user subscribed in group?
  1161. * @param int $user_id
  1162. * @param int $group_id
  1163. * @return bool TRUE if given user is subscribed in given group
  1164. */
  1165. public static function is_subscribed($user_id, $group_id)
  1166. {
  1167. if (empty($user_id) or empty($group_id)) {
  1168. return false;
  1169. }
  1170. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1171. $group_id = Database::escape_string($group_id);
  1172. $user_id = Database::escape_string($user_id);
  1173. $course_id = api_get_course_int_id();
  1174. $sql = 'SELECT 1 FROM '.$table_group_user.' WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id = '.$user_id;
  1175. $db_result = Database::query($sql);
  1176. return Database::num_rows($db_result) > 0;
  1177. }
  1178. /**
  1179. * Can a user subscribe to a specified group in a course
  1180. * @param int $user_id
  1181. * @param int $group_id
  1182. * @return bool TRUE if given user can be subscribed in given group
  1183. */
  1184. public static function can_user_subscribe($user_id, $group_id)
  1185. {
  1186. $category = self :: get_category_from_group($group_id);
  1187. $result = !self :: is_subscribed($user_id, $group_id);
  1188. $result &= (self :: number_of_students($group_id) < self :: maximum_number_of_students($group_id));
  1189. if ($category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT) {
  1190. $category['groups_per_user'] = self::INFINITE;
  1191. }
  1192. $result &= (self :: user_in_number_of_groups($user_id, $category['id']) < $category['groups_per_user']);
  1193. $result &= !self :: is_tutor_of_group($user_id, $group_id);
  1194. return $result;
  1195. }
  1196. /**
  1197. * Can a user unsubscribe to a specified group in a course
  1198. * @param int $user_id
  1199. * @param int $group_id
  1200. * @return bool TRUE if given user can be unsubscribed from given group
  1201. * @internal for now, same as GroupManager::is_subscribed($user_id,$group_id)
  1202. */
  1203. public static function can_user_unsubscribe($user_id, $group_id)
  1204. {
  1205. $result = self :: is_subscribed($user_id, $group_id);
  1206. return $result;
  1207. }
  1208. /**
  1209. * Get all subscribed users (students and tutors) from a group
  1210. * @param int $group_id
  1211. * @return array An array with information of all users from the given group.
  1212. * (user_id, firstname, lastname, email)
  1213. */
  1214. public static function get_subscribed_users($group_id)
  1215. {
  1216. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1217. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1218. $order_clause = api_sort_by_first_name() ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1219. if (empty($group_id)) {
  1220. return array();
  1221. }
  1222. $group_id = intval($group_id);
  1223. $course_id = api_get_course_int_id();
  1224. $sql = "SELECT ug.id, u.user_id, u.lastname, u.firstname, u.email, u.username
  1225. FROM $table_user u INNER JOIN $table_group_user ug ON (ug.user_id = u.user_id)
  1226. WHERE ug.c_id = $course_id AND
  1227. ug.group_id = $group_id
  1228. $order_clause";
  1229. $db_result = Database::query($sql);
  1230. $users = array();
  1231. while ($user = Database::fetch_object($db_result)) {
  1232. $member['user_id'] = $user->user_id;
  1233. $member['firstname'] = $user->firstname;
  1234. $member['lastname'] = $user->lastname;
  1235. $member['email'] = $user->email;
  1236. $member['username'] = $user->username;
  1237. $users[$member['user_id']] = $member;
  1238. }
  1239. return $users;
  1240. }
  1241. /**
  1242. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1243. * Get all subscribed tutors of a group
  1244. * @param int $group_id
  1245. * @return array An array with information of all users from the given group.
  1246. * (user_id, firstname, lastname, email)
  1247. */
  1248. public static function get_subscribed_tutors($group_id, $id_only = false)
  1249. {
  1250. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1251. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1252. $order_clause = api_sort_by_first_name(
  1253. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1254. $group_id = Database::escape_string($group_id);
  1255. $course_id = api_get_course_int_id();
  1256. $sql = "SELECT tg.id, u.user_id, u.lastname, u.firstname, u.email
  1257. FROM ".$table_user." u, ".$table_group_tutor." tg
  1258. WHERE tg.c_id = $course_id AND
  1259. tg.group_id='".$group_id."' AND
  1260. tg.user_id=u.user_id".$order_clause;
  1261. $db_result = Database::query($sql);
  1262. $users = array ();
  1263. while ($user = Database::fetch_object($db_result)) {
  1264. if (!$id_only) {
  1265. $member['user_id'] = $user->user_id;
  1266. $member['firstname'] = $user->firstname;
  1267. $member['lastname'] = $user->lastname;
  1268. $member['email'] = $user->email;
  1269. $users[] = $member;
  1270. } else {
  1271. $users[]=$user->user_id;
  1272. }
  1273. }
  1274. return $users;
  1275. }
  1276. /**
  1277. * Subscribe user(s) to a specified group in current course
  1278. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1279. * @param int $group_id
  1280. * @return bool TRUE if successfull
  1281. */
  1282. public static function subscribe_users($user_ids, $group_id)
  1283. {
  1284. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1285. $result = true;
  1286. $course_id = api_get_course_int_id();
  1287. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1288. if (!empty($user_ids)) {
  1289. foreach ($user_ids as $user_id) {
  1290. $user_id = Database::escape_string($user_id);
  1291. $group_id = Database::escape_string($group_id);
  1292. $sql = "INSERT INTO ".$table_group_user." (c_id, user_id, group_id)
  1293. VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1294. $result &= Database::query($sql);
  1295. }
  1296. }
  1297. return $result;
  1298. }
  1299. /**
  1300. * Subscribe tutor(s) to a specified group in current course
  1301. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1302. * @param int $group_id
  1303. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1304. * @see subscribe_users. This function is almost an exact copy of that function.
  1305. * @return bool TRUE if successfull
  1306. */
  1307. public static function subscribe_tutors($user_ids, $group_id)
  1308. {
  1309. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1310. $result = true;
  1311. $course_id = api_get_course_int_id();
  1312. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1313. foreach ($user_ids as $user_id) {
  1314. $user_id = Database::escape_string($user_id);
  1315. $group_id = Database::escape_string($group_id);
  1316. $sql = "INSERT INTO ".$table_group_tutor." (c_id, user_id, group_id)
  1317. VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1318. $result &= Database::query($sql);
  1319. }
  1320. return $result;
  1321. }
  1322. /**
  1323. * Unsubscribe user(s) from a specified group in current course
  1324. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1325. * @param int $group_id
  1326. * @return bool TRUE if successfull
  1327. */
  1328. public static function unsubscribe_users($user_ids, $group_id)
  1329. {
  1330. $user_ids = is_array($user_ids) ? $user_ids : array ($user_ids);
  1331. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1332. $group_id = Database::escape_string($group_id);
  1333. $course_id = api_get_course_int_id();
  1334. $sql = 'DELETE FROM '.$table_group_user.'
  1335. WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id IN ('.implode(',', $user_ids).')';
  1336. Database::query($sql);
  1337. }
  1338. /**
  1339. * Unsubscribe all users from one or more groups
  1340. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1341. * @return bool TRUE if successfull
  1342. */
  1343. public static function unsubscribe_all_users($group_ids)
  1344. {
  1345. $course_id = api_get_course_int_id();
  1346. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1347. $group_ids = array_map('intval', $group_ids);
  1348. if (count($group_ids) > 0) {
  1349. if (api_is_course_coach()) {
  1350. for ($i = 0; $i < count($group_ids); $i++) {
  1351. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1352. array_splice($group_ids, $i, 1);
  1353. $i--;
  1354. }
  1355. }
  1356. if (count($group_ids) == 0) {
  1357. return false;
  1358. }
  1359. }
  1360. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1361. $sql = 'DELETE FROM '.$table_group_user.' WHERE group_id IN ('.implode(',', $group_ids).') AND c_id = '.$course_id;
  1362. $result = Database::query($sql);
  1363. return $result;
  1364. }
  1365. return true;
  1366. }
  1367. /**
  1368. * Unsubscribe all tutors from one or more groups
  1369. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1370. * @see unsubscribe_all_users. This function is almost an exact copy of that function.
  1371. * @return bool TRUE if successfull
  1372. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1373. */
  1374. public static function unsubscribe_all_tutors($group_ids)
  1375. {
  1376. $course_id = api_get_course_int_id();
  1377. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1378. if (count($group_ids) > 0) {
  1379. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1380. $sql = 'DELETE FROM '.$table_group_tutor.' WHERE group_id IN ('.implode(',', $group_ids).') AND c_id = '.$course_id;
  1381. $result = Database::query($sql);
  1382. return $result;
  1383. }
  1384. return true;
  1385. }
  1386. /**
  1387. * Is the user a tutor of this group?
  1388. * @param $user_id the id of the user
  1389. * @param $group_id the id of the group
  1390. * @return boolean true/false
  1391. * @todo use the function user_has_access that includes this function
  1392. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1393. */
  1394. public static function is_tutor_of_group($user_id, $group_id)
  1395. {
  1396. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1397. $user_id = Database::escape_string($user_id);
  1398. $group_id = Database::escape_string($group_id);
  1399. $course_id = api_get_course_int_id();
  1400. $sql = "SELECT * FROM ".$table_group_tutor." WHERE c_id = $course_id AND user_id='".$user_id."' AND group_id='".$group_id."'";
  1401. $result = Database::query($sql);
  1402. if (Database::num_rows($result) > 0) {
  1403. return true;
  1404. } else {
  1405. return false;
  1406. }
  1407. }
  1408. /**
  1409. * Is the user part of this group? This can be a tutor or a normal member
  1410. * you should use this function if the access to a tool or functionality is restricted to the people who are actually in the group
  1411. * 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
  1412. * @param int $user_id the id of the user
  1413. * @param int $group_id the id of the group
  1414. * @return boolean true/false
  1415. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1416. */
  1417. public static function is_user_in_group($user_id, $group_id)
  1418. {
  1419. $member = self :: is_subscribed($user_id, $group_id);
  1420. $tutor = self :: is_tutor_of_group($user_id, $group_id);
  1421. if ($member OR $tutor) {
  1422. return true;
  1423. } else {
  1424. return false;
  1425. }
  1426. }
  1427. /**
  1428. * Get all tutors for the current course.
  1429. * @return array An array with firstname, lastname and user_id for all
  1430. * tutors in the current course.
  1431. * @deprecated this function uses the old tutor implementation
  1432. */
  1433. public static function get_all_tutors()
  1434. {
  1435. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1436. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1437. $sql = "SELECT user.user_id AS user_id, user.lastname AS lastname, user.firstname AS firstname
  1438. FROM ".$user_table." user, ".$course_user_table." cu
  1439. WHERE cu.user_id=user.user_id
  1440. AND cu.tutor_id='1'
  1441. AND cu.c_id='".api_get_course_int_id()."'";
  1442. $resultTutor = Database::query($sql);
  1443. $tutors = array();
  1444. while ($tutor = Database::fetch_array($resultTutor)) {
  1445. $tutors[] = $tutor;
  1446. }
  1447. return $tutors;
  1448. }
  1449. /**
  1450. * Is user a tutor in current course
  1451. * @param int $user_id
  1452. * @return bool TRUE if given user is a tutor in the current course.
  1453. * @deprecated this function uses the old tutor implementation
  1454. */
  1455. public static function is_tutor($user_id)
  1456. {
  1457. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1458. $user_id = Database::escape_string($user_id);
  1459. $sql = "SELECT tutor_id FROM ".$course_user_table."
  1460. WHERE user_id = '".$user_id."' AND c_id ='".api_get_course_int_id()."'"."AND tutor_id=1";
  1461. $db_result = Database::query($sql);
  1462. $result = (Database::num_rows($db_result) > 0);
  1463. return $result;
  1464. }
  1465. /**
  1466. * Get all group's from a given course in which a given user is unsubscribed
  1467. * @author Patrick Cool
  1468. * @param int course id
  1469. * retrieve the groups for
  1470. * @param integer $user_id: the ID of the user you want to know all its
  1471. * group memberships
  1472. */
  1473. public static function get_group_ids($course_id, $user_id)
  1474. {
  1475. $groups = array();
  1476. $tbl_group = Database::get_course_table(TABLE_GROUP_USER);
  1477. $tbl_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1478. $user_id = intval($user_id);
  1479. $course_id = intval($course_id);
  1480. $sql = "SELECT group_id FROM $tbl_group WHERE c_id = $course_id AND user_id = '$user_id'";
  1481. $groupres = Database::query($sql);
  1482. if ($groupres) {
  1483. while ($myrow = Database::fetch_array($groupres)) {
  1484. $groups[] = $myrow['group_id'];
  1485. }
  1486. }
  1487. //Also loading if i'm the tutor
  1488. $sql = "SELECT group_id FROM $tbl_group_tutor WHERE c_id = $course_id AND user_id = '$user_id'";
  1489. $groupres = Database::query($sql);
  1490. if ($groupres) {
  1491. while ($myrow = Database::fetch_array($groupres)) {
  1492. $groups[] = $myrow['group_id'];
  1493. }
  1494. }
  1495. if (!empty($groups)) {
  1496. array_filter($groups);
  1497. }
  1498. return $groups;
  1499. }
  1500. /*
  1501. Group functions - these take virtual/linked courses into account when necessary
  1502. */
  1503. /**
  1504. * Get a combined list of all users of the real course $course_code
  1505. * and all users in virtual courses linked to this course $course_code
  1506. * Filter user list: remove duplicate users; plus
  1507. * remove users that
  1508. * - are already in the current group $group_id;
  1509. * - do not have student status in these courses;
  1510. * - are not appointed as tutor (group assistent) for this group;
  1511. * - have already reached their maximum # of groups in this course.
  1512. *
  1513. * Originally to get the correct list of users a big SQL statement was used,
  1514. * but this has become more complicated now there is not just one real course but many virtual courses.
  1515. * Still, that could have worked as well.
  1516. *
  1517. * @version 1.1.3
  1518. * @author Roan Embrechts
  1519. */
  1520. public static function get_complete_list_of_users_that_can_be_added_to_group ($course_code, $group_id) {
  1521. global $_course, $_user;
  1522. $category = self :: get_category_from_group($group_id, $course_code);
  1523. $number_of_groups_limit = $category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $category['groups_per_user'];
  1524. $real_course_code = $_course['sysCode'];
  1525. $real_course_info = Database :: get_course_info($real_course_code);
  1526. $real_course_user_list = CourseManager :: get_user_list_from_course_code($real_course_code);
  1527. //get list of all virtual courses
  1528. $user_subscribed_course_list = CourseManager :: get_list_of_virtual_courses_for_specific_user_and_real_course($_user['user_id'], $real_course_code);
  1529. //add real course to the list
  1530. $user_subscribed_course_list[] = $real_course_info;
  1531. if (!is_array($user_subscribed_course_list)) {
  1532. return;
  1533. }
  1534. //for all courses...
  1535. foreach ($user_subscribed_course_list as $this_course) {
  1536. $this_course_code = $this_course['code'];
  1537. $course_user_list = CourseManager :: get_user_list_from_course_code($this_course_code);
  1538. //for all users in the course
  1539. foreach ($course_user_list as $this_user) {
  1540. $user_id = $this_user['user_id'];
  1541. $loginname = $this_user['username'];
  1542. $lastname = $this_user['lastname'];
  1543. $firstname = $this_user['firstname'];
  1544. $status = $this_user['status'];
  1545. //$role = $this_user['role'];
  1546. $tutor_id = $this_user['tutor_id'];
  1547. $full_name = api_get_person_name($firstname, $lastname);
  1548. if ($lastname == "" || $firstname == '') {
  1549. $full_name = $loginname;
  1550. }
  1551. $complete_user['user_id'] = $user_id;
  1552. $complete_user['full_name'] = $full_name;
  1553. $complete_user['firstname'] = $firstname;
  1554. $complete_user['lastname'] = $lastname;
  1555. $complete_user['status'] = $status;
  1556. $complete_user['tutor_id'] = $tutor_id;
  1557. $student_number_of_groups = self :: user_in_number_of_groups($user_id, $category['id']);
  1558. //filter: only add users that have not exceeded their maximum amount of groups
  1559. if ($student_number_of_groups < $number_of_groups_limit) {
  1560. $complete_user_list[] = $complete_user;
  1561. }
  1562. }
  1563. }
  1564. if (is_array($complete_user_list)) {
  1565. //sort once, on array field "full_name"
  1566. $complete_user_list = TableSort :: sort_table($complete_user_list, "full_name");
  1567. //filter out duplicates, based on field "user_id"
  1568. $complete_user_list = self :: filter_duplicates($complete_user_list, "user_id");
  1569. $complete_user_list = self :: filter_users_already_in_group($complete_user_list, $group_id);
  1570. }
  1571. return $complete_user_list;
  1572. }
  1573. /**
  1574. * Filter out duplicates in a multidimensional array
  1575. * by comparing field $compare_field.
  1576. *
  1577. * @param $user_array_in list of users (must be sorted).
  1578. * @param string $compare_field, the field to be compared
  1579. */
  1580. public static function filter_duplicates($user_array_in, $compare_field)
  1581. {
  1582. $total_number = count($user_array_in);
  1583. $user_array_out[0] = $user_array_in[0];
  1584. $count_out = 0;
  1585. for ($count_in = 1; $count_in < $total_number; $count_in++) {
  1586. if ($user_array_in[$count_in][$compare_field] != $user_array_out[$count_out][$compare_field]) {
  1587. $count_out++;
  1588. $user_array_out[$count_out] = $user_array_in[$count_in];
  1589. }
  1590. }
  1591. return $user_array_out;
  1592. }
  1593. /**
  1594. * Filters from the array $user_array_in the users already in the group $group_id.
  1595. */
  1596. public static function filter_users_already_in_group($user_array_in, $group_id)
  1597. {
  1598. foreach ($user_array_in as $this_user) {
  1599. if (!self :: is_subscribed($this_user['user_id'], $group_id)) {
  1600. $user_array_out[] = $this_user;
  1601. }
  1602. }
  1603. return $user_array_out;
  1604. }
  1605. /**
  1606. * Remove all users that are not students and all users who have tutor status
  1607. * from the list.
  1608. */
  1609. public static function filter_only_students($user_array_in)
  1610. {
  1611. $user_array_out = array();
  1612. foreach ($user_array_in as $this_user) {
  1613. //if ($this_user['status_rel'] == STUDENT && $this_user['tutor_id'] == 0) {
  1614. if (api_get_session_id()) {
  1615. if ($this_user['status_session'] == 0) {
  1616. $user_array_out[] = $this_user;
  1617. }
  1618. } else {
  1619. if ($this_user['status_rel'] == STUDENT) {
  1620. $user_array_out[] = $this_user;
  1621. }
  1622. }
  1623. }
  1624. return $user_array_out;
  1625. }
  1626. /**
  1627. * Check if a user has access to a certain group tool
  1628. * @param int $user_id The user id
  1629. * @param int $group_id The group id
  1630. * @param constant $tool The tool to check the access rights. This should be
  1631. * one of constants: GROUP_TOOL_DOCUMENTS
  1632. * @return bool True if the given user has access to the given tool in the
  1633. * given course.
  1634. */
  1635. public static function user_has_access($user_id, $group_id, $tool)
  1636. {
  1637. // Admin have access everywhere
  1638. if (api_is_platform_admin()) {
  1639. return true;
  1640. }
  1641. // Course admin also have access to everything
  1642. if (api_is_allowed_to_edit()) {
  1643. return true;
  1644. }
  1645. switch ($tool) {
  1646. case self::GROUP_TOOL_FORUM :
  1647. $state_key = 'forum_state';
  1648. break;
  1649. case self::GROUP_TOOL_DOCUMENTS :
  1650. $state_key = 'doc_state';
  1651. break;
  1652. case self::GROUP_TOOL_CALENDAR :
  1653. $state_key = 'calendar_state';
  1654. break;
  1655. case self::GROUP_TOOL_ANNOUNCEMENT :
  1656. $state_key = 'announcements_state';
  1657. break;
  1658. case self::GROUP_TOOL_WORK :
  1659. $state_key = 'work_state';
  1660. break;
  1661. case self::GROUP_TOOL_WIKI :
  1662. $state_key = 'wiki_state';
  1663. break;
  1664. case self::GROUP_TOOL_CHAT :
  1665. $state_key = 'chat_state';
  1666. break;
  1667. default:
  1668. return false;
  1669. }
  1670. $user_is_in_group = self :: is_user_in_group($user_id, $group_id);
  1671. // Check group properties
  1672. $group_info = self :: get_group_properties($group_id);
  1673. /*if (api_get_setting('allow_group_categories') == 'true') {
  1674. // Check if group category exists
  1675. $category_group_info = self::get_category_from_group($group_id);
  1676. if (!empty($category_group_info)) {
  1677. // If exists check the category group status first
  1678. if ($category_group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1679. return false;
  1680. } elseif($category_group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1681. return false;
  1682. }
  1683. }
  1684. }*/
  1685. // is_user_in_group() is more complete that the is_subscribed() function
  1686. if ($group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1687. return false;
  1688. } elseif ($group_info[$state_key] == self::TOOL_PUBLIC) {
  1689. return true;
  1690. } elseif (api_is_allowed_to_edit(false, true)) {
  1691. return true;
  1692. } elseif ($group_info['tutor_id'] == $user_id) { //this tutor implementation was dropped
  1693. return true;
  1694. } elseif ($group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1695. return false;
  1696. } else {
  1697. return $user_is_in_group;
  1698. }
  1699. }
  1700. /**
  1701. * Get all groups where a specific user is subscribed
  1702. */
  1703. public static function get_user_group_name($user_id)
  1704. {
  1705. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1706. $table_group = Database::get_course_table(TABLE_GROUP);
  1707. $user_id = intval($user_id);
  1708. $course_id = api_get_course_int_id();
  1709. $sql_groups = "SELECT name FROM $table_group g INNER JOIN $table_group_user gu
  1710. ON (gu.group_id=g.id)
  1711. WHERE gu.c_id= $course_id AND
  1712. g.c_id= $course_id AND
  1713. gu.user_id = $user_id";
  1714. $res = Database::query($sql_groups);
  1715. $groups = array();
  1716. while ($group = Database::fetch_array($res)) {
  1717. $groups[] .= $group['name'];
  1718. }
  1719. return $groups;
  1720. }
  1721. /**
  1722. *
  1723. * See : fill_groups
  1724. * Fill the groups with students.
  1725. *
  1726. * note : optimize fill_groups_list <--> fill_groups
  1727. * @param array $group_ids
  1728. * @return array|bool
  1729. */
  1730. public static function fill_groups_list($group_ids)
  1731. {
  1732. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1733. $group_ids = array_map('intval', $group_ids);
  1734. if (api_is_course_coach()) {
  1735. for ($i = 0; $i < count($group_ids); $i++) {
  1736. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1737. array_splice($group_ids, $i, 1);
  1738. $i--;
  1739. }
  1740. }
  1741. if (count($group_ids) == 0) {
  1742. return false;
  1743. }
  1744. }
  1745. $_course = api_get_course_info();
  1746. $category = self :: get_category_from_group($group_ids[0]);
  1747. $groups_per_user = $category['groups_per_user'];
  1748. $group_table = Database :: get_course_table(TABLE_GROUP);
  1749. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  1750. $session_id = api_get_session_id();
  1751. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['sysCode'], true, $session_id);
  1752. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  1753. $course_id = api_get_course_int_id();
  1754. /*
  1755. * Retrieve all the groups where enrollment is still allowed
  1756. * (reverse) ordered by the number of place available
  1757. */
  1758. $sql = "SELECT g.id gid, count(ug.user_id) count_users, g.max_student
  1759. FROM ".$group_table." g
  1760. LEFT JOIN ".$group_user_table." ug
  1761. ON g.id = ug.group_id
  1762. WHERE g.c_id = $course_id AND
  1763. ug.c_id = $course_id AND
  1764. g.id IN (".implode(',', $group_ids).")
  1765. GROUP BY (g.id)";
  1766. $sql_result = Database::query($sql);
  1767. $group_available_place = array();
  1768. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  1769. if (!empty($group['max_student'])) {
  1770. $places = intval($group['max_student'] - $group['count_users']);
  1771. } else {
  1772. $places = self::MEMBER_PER_GROUP_NO_LIMIT;
  1773. }
  1774. $group_available_place[$group['gid']] = $places;
  1775. }
  1776. /*
  1777. * Retrieve course users (reverse) ordered by the number
  1778. * of group they are already enrolled
  1779. */
  1780. for ($i = 0; $i < count($complete_user_list); $i ++) {
  1781. //find # of groups the user is enrolled in
  1782. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"],$category['id']);
  1783. //add # of groups to user list
  1784. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  1785. }
  1786. //first sort by user_id to filter out duplicates
  1787. $complete_user_list = TableSort::sort_table($complete_user_list, 'user_id');
  1788. $complete_user_list = self::filter_duplicates($complete_user_list, 'user_id');
  1789. //$complete_user_list = self :: filter_only_students($complete_user_list);
  1790. //now sort by # of group left
  1791. $complete_user_list = TableSort::sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  1792. return $complete_user_list;
  1793. }
  1794. /**
  1795. * @param array $group_list
  1796. * @param int $category_id
  1797. */
  1798. static function process_groups($group_list, $category_id = null)
  1799. {
  1800. global $origin, $charset;
  1801. $category_id = intval($category_id);
  1802. $totalRegistered = 0;
  1803. $group_data = array();
  1804. $user_info = api_get_user_info();
  1805. $session_id = api_get_session_id();
  1806. $user_id = $user_info['user_id'];
  1807. $orig = isset($origin) ? $origin : null;
  1808. foreach ($group_list as $this_group) {
  1809. // Validation when belongs to a session
  1810. $session_img = api_get_session_image($this_group['session_id'], $user_info['status']);
  1811. // All the tutors of this group
  1812. $tutorsids_of_group = self::get_subscribed_tutors($this_group['id'], true);
  1813. // Create a new table-row
  1814. $row = array();
  1815. // Checkbox
  1816. if (api_is_allowed_to_edit(false, true) && count($group_list) > 1) {
  1817. $row[] = $this_group['id'];
  1818. }
  1819. // Group name
  1820. if ((api_is_allowed_to_edit(false, true) ||
  1821. in_array($user_id, $tutorsids_of_group) ||
  1822. $this_group['is_member'] ||
  1823. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_FORUM) ||
  1824. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_DOCUMENTS) ||
  1825. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_CALENDAR) ||
  1826. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_ANNOUNCEMENT) ||
  1827. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WORK) ||
  1828. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WIKI))
  1829. && !(api_is_course_coach() && intval($this_group['session_id']) != $session_id)
  1830. ) {
  1831. $group_name = '<a href="group_space.php?cidReq='.api_get_course_id().'&amp;origin='.$orig.'&amp;gidReq='.$this_group['id'].'">'.
  1832. Security::remove_XSS($this_group['name']).'</a> ';
  1833. if (!empty($user_id) && !empty($this_group['id_tutor']) && $user_id == $this_group['id_tutor']) {
  1834. $group_name .= Display::label(get_lang('OneMyGroups'), 'success');
  1835. } elseif ($this_group['is_member']) {
  1836. $group_name .= Display::label(get_lang('MyGroup'), 'success');
  1837. }
  1838. if (api_is_allowed_to_edit() && !empty($this_group['session_name'])) {
  1839. $group_name .= ' ('.$this_group['session_name'].')';
  1840. }
  1841. $group_name .= $session_img;
  1842. $row[] = $group_name.'<br />'.stripslashes(trim($this_group['description']));
  1843. } else {
  1844. $row[] = $this_group['name'].'<br />'.stripslashes(trim($this_group['description']));
  1845. }
  1846. // Tutor name
  1847. $tutor_info = null;
  1848. if (count($tutorsids_of_group) > 0) {
  1849. foreach ($tutorsids_of_group as $tutor_id) {
  1850. $tutor = api_get_user_info($tutor_id);
  1851. $username = api_htmlentities(sprintf(get_lang('LoginX'), $tutor['username']), ENT_QUOTES);
  1852. if (api_get_setting('show_email_addresses') == 'true') {
  1853. $tutor_info .= Display::tag('span', Display::encrypted_mailto_link($tutor['mail'], api_get_person_name($tutor['firstName'], $tutor['lastName'])), array('title'=>$username)).', ';
  1854. } else {
  1855. if (api_is_allowed_to_edit()) {
  1856. $tutor_info .= Display::tag('span', Display::encrypted_mailto_link($tutor['mail'], api_get_person_name($tutor['firstName'], $tutor['lastName'])), array('title'=>$username)).', ';
  1857. } else {
  1858. $tutor_info .= Display::tag('span', api_get_person_name($tutor['firstName'], $tutor['lastName']), array('title'=>$username)).', ';
  1859. }
  1860. }
  1861. }
  1862. }
  1863. $tutor_info = api_substr($tutor_info, 0, api_strlen($tutor_info) - 2);
  1864. $row[] = $tutor_info;
  1865. // Max number of members in group
  1866. $max_members = ($this_group['maximum_number_of_members'] == self::MEMBER_PER_GROUP_NO_LIMIT ? ' ' : ' / '.$this_group['maximum_number_of_members']);
  1867. // Number of members in group
  1868. $row[] = $this_group['number_of_members'].$max_members;
  1869. // Self-registration / unregistration
  1870. if (!api_is_allowed_to_edit(false, true)) {
  1871. if (self :: is_self_registration_allowed($user_id, $this_group['id'])) {
  1872. $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>';
  1873. } elseif (self :: is_self_unregistration_allowed($user_id, $this_group['id'])) {
  1874. $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>';
  1875. } else {
  1876. $row[] = '-';
  1877. }
  1878. }
  1879. $url = api_get_path(WEB_CODE_PATH).'group/';
  1880. // Edit-links
  1881. if (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && intval($this_group['session_id']) != $session_id)) {
  1882. $edit_actions = '<a href="'.$url.'settings.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'" title="'.get_lang('Edit').'">'.
  1883. Display::return_icon('edit.png', get_lang('EditGroup'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  1884. $edit_actions .= '<a href="'.$url.'member_settings.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'" title="'.get_lang('GroupMembers').'">'.
  1885. Display::return_icon('user.png', get_lang('GroupMembers'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1886. $edit_actions .= '<a href="'.$url.'group_overview.php?action=export&type=xls&'.api_get_cidreq(true, false).'&id='.$this_group['id'].'" title="'.get_lang('ExportUsers').'">'.
  1887. Display::return_icon('export_excel.png', get_lang('Export'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1888. /*$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').'">'.
  1889. Display::return_icon('clean.png',get_lang('EmptyGroup'),'',ICON_SIZE_SMALL).'</a>&nbsp;';*/
  1890. $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').'">'.
  1891. Display::return_icon('fill.png',get_lang('FillGroup'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  1892. $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').'">'.
  1893. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  1894. $row[] = $edit_actions;
  1895. }
  1896. if (!empty($this_group['nbMember'])) {
  1897. $totalRegistered = $totalRegistered + $this_group['nbMember'];
  1898. }
  1899. $group_data[] = $row;
  1900. } // end loop
  1901. $table = new SortableTableFromArrayConfig($group_data, 1, 20, 'group_category_'.$category_id);
  1902. $table->set_additional_parameters(array('category' => $category_id));
  1903. $column = 0;
  1904. if (api_is_allowed_to_edit(false, true) and count($group_list) > 1) {
  1905. $table->set_header($column++, '', false);
  1906. }
  1907. $table->set_header($column++, get_lang('Groups'));
  1908. $table->set_header($column++, get_lang('GroupTutor'));
  1909. $table->set_header($column++, get_lang('Registered'), false);
  1910. if (!api_is_allowed_to_edit(false, true)) { // If self-registration allowed
  1911. $table->set_header($column++, get_lang('GroupSelfRegistration'), false);
  1912. }
  1913. if (api_is_allowed_to_edit(false, true)) { // Only for course administrator
  1914. $table->set_header($column++, get_lang('Modify'), false);
  1915. $form_actions = array();
  1916. $form_actions['fill_selected'] = get_lang('FillGroup');
  1917. $form_actions['empty_selected'] = get_lang('EmptyGroup');
  1918. $form_actions['delete_selected'] = get_lang('Delete');
  1919. if (count($group_list) > 1) {
  1920. $table->set_form_actions($form_actions, 'group');
  1921. }
  1922. }
  1923. $table->display();
  1924. }
  1925. /**
  1926. *
  1927. * @param array $groupData
  1928. * @param bool $deleteNotInArray
  1929. * @return array
  1930. */
  1931. public static function importCategoriesAndGroupsFromArray($groupData, $deleteNotInArray = false)
  1932. {
  1933. $result = array();
  1934. $elementsFound = array(
  1935. 'categories' => array(),
  1936. 'groups' => array()
  1937. );
  1938. foreach ($groupData as $data) {
  1939. $isCategory = empty($data['group']) ? true : false;
  1940. if ($isCategory) {
  1941. $categoryInfo = self::getCategoryByTitle($data['category']);
  1942. $categoryId = $categoryInfo['id'];
  1943. if (!empty($categoryInfo)) {
  1944. // Update
  1945. self::update_category(
  1946. $categoryId,
  1947. $data['category'],
  1948. $data['description'],
  1949. $data['doc_state'],
  1950. $data['work_state'],
  1951. $data['calendar_state'],
  1952. $data['announcements_state'],
  1953. $data['forum_state'],
  1954. $data['wiki_state'],
  1955. $data['chat_state'],
  1956. $data['self_reg_allowed'],
  1957. $data['self_unreg_allowed'],
  1958. $data['max_student'],
  1959. $data['groups_per_user']
  1960. );
  1961. $data['category_id'] = $categoryId;
  1962. $result['updated']['category'][] = $data;
  1963. } else {
  1964. // Add
  1965. $categoryId = self::create_category(
  1966. $data['category'],
  1967. $data['description'],
  1968. $data['doc_state'],
  1969. $data['work_state'],
  1970. $data['calendar_state'],
  1971. $data['announcements_state'],
  1972. $data['forum_state'],
  1973. $data['wiki_state'],
  1974. $data['chat_state'],
  1975. $data['self_reg_allowed'],
  1976. $data['self_unreg_allowed'],
  1977. $data['max_student'],
  1978. $data['groups_per_user']
  1979. );
  1980. if ($categoryId) {
  1981. $data['category_id'] = $categoryId;
  1982. $result['added']['category'][] = $data;
  1983. }
  1984. }
  1985. $elementsFound['categories'][] = $categoryId;
  1986. } else {
  1987. $groupInfo = self::getGroupByName($data['group']);
  1988. if (empty($groupInfo)) {
  1989. $categoryInfo = self::getCategoryByTitle($data['category']);
  1990. $categoryId = null;
  1991. if (!empty($categoryInfo)) {
  1992. $categoryId = $categoryInfo['id'];
  1993. }
  1994. // Add
  1995. $groupId = self::create_group(
  1996. $data['group'],
  1997. $categoryId,
  1998. null,
  1999. $data['max_students']
  2000. );
  2001. if ($groupId) {
  2002. self::set_group_properties(
  2003. $groupId,
  2004. $data['group'],
  2005. $data['description'],
  2006. $data['max_students'],
  2007. $data['doc_state'],
  2008. $data['work_state'],
  2009. $data['calendar_state'],
  2010. $data['announcements_state'],
  2011. $data['forum_state'],
  2012. $data['wiki_state'],
  2013. $data['chat_state'],
  2014. $data['self_reg_allowed'],
  2015. $data['self_unreg_allowed']
  2016. );
  2017. $data['group_id'] = $groupId;
  2018. $result['added']['group'][] = $data;
  2019. }
  2020. } else {
  2021. // Update
  2022. $groupId = $groupInfo['id'];
  2023. self::set_group_properties(
  2024. $groupId,
  2025. $data['group'],
  2026. $data['description'],
  2027. $data['max_students'],
  2028. $data['doc_state'],
  2029. $data['work_state'],
  2030. $data['calendar_state'],
  2031. $data['announcements_state'],
  2032. $data['forum_state'],
  2033. $data['wiki_state'],
  2034. $data['chat_state'],
  2035. $data['self_reg_allowed'],
  2036. $data['self_unreg_allowed']
  2037. );
  2038. $data['group_id'] = $groupId;
  2039. $result['updated']['group'][] = $data;
  2040. }
  2041. $elementsFound['groups'][] = $groupId;
  2042. }
  2043. }
  2044. if ($deleteNotInArray) {
  2045. // Check categories
  2046. $categories = GroupManager::get_categories();
  2047. foreach ($categories as $category) {
  2048. if (!in_array($category['id'], $elementsFound['categories'])) {
  2049. GroupManager::delete_category($category['id']);
  2050. $category['category'] = $category['title'];
  2051. $result['deleted']['category'][] = $category;
  2052. }
  2053. }
  2054. $groups = GroupManager::get_groups();
  2055. foreach ($groups as $group) {
  2056. if (!in_array($group['id'], $elementsFound['groups'])) {
  2057. GroupManager::delete_groups(array($group['id']));
  2058. $group['group'] = $group['name'];
  2059. $result['deleted']['group'][] = $group;
  2060. }
  2061. }
  2062. }
  2063. return $result;
  2064. }
  2065. /**
  2066. * Export all categories/group from a course to an array.
  2067. * This function works only in a context of a course.
  2068. * @return array
  2069. */
  2070. public static function exportCategoriesAndGroupsToArray()
  2071. {
  2072. $data = array();
  2073. $data[] = array(
  2074. 'category',
  2075. 'group',
  2076. 'description',
  2077. 'announcements_state',
  2078. 'calendar_state',
  2079. 'chat_state',
  2080. 'doc_state',
  2081. 'forum_state',
  2082. 'work_state',
  2083. 'wiki_state',
  2084. 'max_student',
  2085. 'self_reg_allowed',
  2086. 'self_unreg_allowed',
  2087. 'groups_per_user'
  2088. );
  2089. $categories = GroupManager::get_categories();
  2090. foreach ($categories as $categoryInfo) {
  2091. $data[] = array(
  2092. $categoryInfo['title'],
  2093. null,
  2094. $categoryInfo['description'],
  2095. $categoryInfo['announcements_state'],
  2096. $categoryInfo['calendar_state'],
  2097. $categoryInfo['chat_state'],
  2098. $categoryInfo['doc_state'],
  2099. $categoryInfo['forum_state'],
  2100. $categoryInfo['work_state'],
  2101. $categoryInfo['wiki_state'],
  2102. $categoryInfo['max_student'],
  2103. $categoryInfo['self_reg_allowed'],
  2104. $categoryInfo['self_unreg_allowed'],
  2105. $categoryInfo['groups_per_user']
  2106. );
  2107. }
  2108. $groups = GroupManager::get_group_list();
  2109. foreach ($groups as $groupInfo) {
  2110. $categoryTitle = null;
  2111. $categoryInfo = GroupManager::get_category($groupInfo['category_id']);
  2112. $groupSettings = GroupManager::get_group_properties($groupInfo['id']);
  2113. if (!empty($categoryInfo)) {
  2114. $categoryTitle = $categoryInfo['title'];
  2115. }
  2116. $data[] = array(
  2117. $categoryTitle,
  2118. $groupSettings['name'],
  2119. $groupSettings['description'],
  2120. $groupSettings['announcements_state'],
  2121. $groupSettings['calendar_state'],
  2122. $groupSettings['chat_state'],
  2123. $groupSettings['doc_state'],
  2124. $groupSettings['forum_state'],
  2125. $groupSettings['work_state'],
  2126. $groupSettings['wiki_state'],
  2127. $groupSettings['maximum_number_of_students'],
  2128. $groupSettings['self_registration_allowed'],
  2129. $groupSettings['self_unregistration_allowed'],
  2130. );
  2131. }
  2132. return $data;
  2133. }
  2134. static function getSettingBar($default)
  2135. {
  2136. $activeSettings = null;
  2137. $activeTutor = null;
  2138. $activeMember = null;
  2139. switch($default) {
  2140. case 'settings':
  2141. $activeSettings = 'active';
  2142. break;
  2143. case'tutor':
  2144. $activeTutor = 'active';
  2145. break;
  2146. case 'member':
  2147. $activeMember = 'active';
  2148. break;
  2149. }
  2150. $url = api_get_path(WEB_CODE_PATH).'group/%s?'.api_get_cidreq();
  2151. echo '
  2152. <ul class="nav nav-tabs">
  2153. <li class="'.$activeSettings.'">
  2154. <a href="'.sprintf($url, 'settings.php').'">
  2155. '.Display::return_icon('settings.png').' '.get_lang('Settings').'
  2156. </a>
  2157. </li>
  2158. <li class="'.$activeMember.'">
  2159. <a href="'.sprintf($url, 'member_settings.php').'">
  2160. '.Display::return_icon('user.png').' '.get_lang('GroupMembers').'
  2161. </a>
  2162. </li>
  2163. <li class="'.$activeTutor.'">
  2164. <a href="'.sprintf($url, 'tutor_settings.php').'">
  2165. '.Display::return_icon('teacher.png').' '.get_lang('GroupTutors').'
  2166. </a>
  2167. </li>
  2168. </ul>';
  2169. }
  2170. /**
  2171. * @param int $courseId
  2172. * @param string $keyword
  2173. * @return string
  2174. */
  2175. public static function getOverview($courseId, $keyword = null)
  2176. {
  2177. $content = null;
  2178. $categories = GroupManager::get_categories();
  2179. if (!empty($categories)) {
  2180. foreach ($categories as $category) {
  2181. if (api_get_setting('allow_group_categories') == 'true') {
  2182. $content .= '<h2>'.$category['title'].'</h2>';
  2183. }
  2184. if (!empty($keyword)) {
  2185. $groups = GroupManager::getGroupListFilterByName($keyword, $category['id'], $courseId);
  2186. } else {
  2187. $groups = GroupManager::get_group_list($category['id']);
  2188. }
  2189. $content .= '<ul>';
  2190. if (!empty($groups)) {
  2191. foreach ($groups as $group) {
  2192. $content .= '<li>';
  2193. $content .= Display::tag('h3', Security::remove_XSS($group['name']));
  2194. $users = GroupManager::getTutors($group['id']);
  2195. if (!empty($users)) {
  2196. $content .= '<ul>';
  2197. $content .= "<li>".Display::tag('h4', get_lang('Tutors'))."</li><ul>";
  2198. foreach ($users as $user) {
  2199. $user_info = api_get_user_info($user['user_id']);
  2200. $content .= '<li title="'.$user_info['username'].'">'.$user_info['complete_name_with_username'].'</li>';
  2201. }
  2202. $content .= '</ul>';
  2203. $content .= '</ul>';
  2204. }
  2205. $users = GroupManager::getStudents($group['id']);
  2206. if (!empty($users)) {
  2207. $content .= '<ul>';
  2208. $content .= "<li>".Display::tag('h4', get_lang('Students'))."</li><ul>";
  2209. foreach ($users as $user) {
  2210. $user_info = api_get_user_info($user['user_id']);
  2211. $content .= '<li title="'.$user_info['username'].'">'.$user_info['complete_name_with_username'].'</li>';
  2212. }
  2213. $content .= '</ul>';
  2214. $content .= '</ul>';
  2215. }
  2216. $content .= '</li>';
  2217. }
  2218. }
  2219. $content .= '</ul>';
  2220. }
  2221. }
  2222. return $content;
  2223. }
  2224. /**
  2225. * Returns the search form
  2226. * @return string
  2227. */
  2228. public static function getSearchForm()
  2229. {
  2230. $url = api_get_path(WEB_CODE_PATH).'group/group_overview.php?'.api_get_cidreq();
  2231. $form = new FormValidator('search_groups', 'get', $url, null, array('class' => 'form-search'));
  2232. $form->addElement('text', 'keyword');
  2233. $form->addElement('button', 'submit', get_lang('Search'));
  2234. return $form->toHtml();
  2235. }
  2236. }