groupmanager.lib.php 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CGroupRelUser;
  4. /**
  5. * This library contains some functions for group-management.
  6. *
  7. * @author Bart Mollet
  8. *
  9. * @package chamilo.library
  10. *
  11. * @todo Add $course_code parameter to all functions. So this GroupManager can
  12. * be used outside a session.
  13. */
  14. class GroupManager
  15. {
  16. /* VIRTUAL_COURSE_CATEGORY:
  17. in this category groups are created based on the virtual course of a course*/
  18. const VIRTUAL_COURSE_CATEGORY = 1;
  19. /* DEFAULT_GROUP_CATEGORY:
  20. When group categories aren't available (platform-setting),
  21. all groups are created in this 'dummy'-category*/
  22. const DEFAULT_GROUP_CATEGORY = 2;
  23. /**
  24. * infinite.
  25. */
  26. const INFINITE = 99999;
  27. /**
  28. * No limit on the number of users in a group.
  29. */
  30. const MEMBER_PER_GROUP_NO_LIMIT = 0;
  31. /**
  32. * No limit on the number of groups per user.
  33. */
  34. const GROUP_PER_MEMBER_NO_LIMIT = 0;
  35. /**
  36. * The tools of a group can have 3 states
  37. * - not available
  38. * - public
  39. * - private.
  40. */
  41. const TOOL_NOT_AVAILABLE = 0;
  42. const TOOL_PUBLIC = 1;
  43. const TOOL_PRIVATE = 2;
  44. /**
  45. * Constants for the available group tools.
  46. */
  47. const GROUP_TOOL_FORUM = 0;
  48. const GROUP_TOOL_DOCUMENTS = 1;
  49. const GROUP_TOOL_CALENDAR = 2;
  50. const GROUP_TOOL_ANNOUNCEMENT = 3;
  51. const GROUP_TOOL_WORK = 4;
  52. const GROUP_TOOL_WIKI = 5;
  53. const GROUP_TOOL_CHAT = 6;
  54. /**
  55. * GroupManager constructor.
  56. */
  57. public function __construct()
  58. {
  59. }
  60. /**
  61. * @return array
  62. */
  63. public static function get_groups($courseId = null)
  64. {
  65. $table_group = Database::get_course_table(TABLE_GROUP);
  66. $courseId = !empty($courseId) ? (int) $courseId : api_get_course_int_id();
  67. $sql = "SELECT * FROM $table_group WHERE c_id = $courseId ";
  68. $result = Database::query($sql);
  69. return Database::store_result($result, 'ASSOC');
  70. }
  71. /**
  72. * Get list of groups for current course.
  73. *
  74. * @param int $categoryId The id of the category from which the groups are
  75. * requested
  76. * @param array $course_info Default is current course
  77. * @param int $status group status
  78. * @param int $sessionId
  79. * @param bool $getCount
  80. *
  81. * @return array an array with all information about the groups
  82. */
  83. public static function get_group_list(
  84. $categoryId = null,
  85. $course_info = [],
  86. $status = null,
  87. $sessionId = 0,
  88. $getCount = false
  89. ) {
  90. $course_info = empty($course_info) ? api_get_course_info() : $course_info;
  91. if (empty($course_info)) {
  92. return [];
  93. }
  94. $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
  95. $course_id = $course_info['real_id'];
  96. $table_group = Database::get_course_table(TABLE_GROUP);
  97. $select = " g.id,
  98. g.iid,
  99. g.name,
  100. g.description,
  101. g.category_id,
  102. g.max_student maximum_number_of_members,
  103. g.secret_directory,
  104. g.self_registration_allowed,
  105. g.self_unregistration_allowed,
  106. g.session_id,
  107. g.status";
  108. if ($getCount) {
  109. $select = " DISTINCT count(g.iid) as count ";
  110. }
  111. $sql = "SELECT
  112. $select
  113. FROM $table_group g
  114. WHERE 1 = 1 ";
  115. if (!is_null($categoryId)) {
  116. $sql .= " AND g.category_id = '".intval($categoryId)."' ";
  117. $session_condition = api_get_session_condition($sessionId);
  118. if (!empty($session_condition)) {
  119. $sql .= $session_condition;
  120. }
  121. } else {
  122. $session_condition = api_get_session_condition($sessionId, true);
  123. }
  124. if (!is_null($status)) {
  125. $sql .= " AND g.status = '".intval($status)."' ";
  126. }
  127. $sql .= " AND g.c_id = $course_id ";
  128. if (!empty($session_condition)) {
  129. $sql .= $session_condition;
  130. }
  131. $sql .= "ORDER BY UPPER(g.name)";
  132. $result = Database::query($sql);
  133. if ($getCount) {
  134. $row = Database::fetch_array($result);
  135. return $row['count'];
  136. }
  137. $groups = [];
  138. while ($thisGroup = Database::fetch_array($result)) {
  139. $thisGroup['number_of_members'] = count(self::get_subscribed_users($thisGroup));
  140. if ($thisGroup['session_id'] != 0) {
  141. $sql = 'SELECT name FROM '.Database::get_main_table(TABLE_MAIN_SESSION).'
  142. WHERE id='.$thisGroup['session_id'];
  143. $rs_session = Database::query($sql);
  144. if (Database::num_rows($rs_session) > 0) {
  145. $thisGroup['session_name'] = Database::result($rs_session, 0, 0);
  146. }
  147. }
  148. $groups[] = $thisGroup;
  149. }
  150. return $groups;
  151. }
  152. /**
  153. * Create a group.
  154. *
  155. * @param string $name The name for this group
  156. * @param int $category_id
  157. * @param int $tutor The user-id of the group's tutor
  158. * @param int $places How many people can subscribe to the new group
  159. *
  160. * @return int
  161. */
  162. public static function create_group($name, $category_id, $tutor, $places)
  163. {
  164. $_course = api_get_course_info();
  165. $session_id = api_get_session_id();
  166. $course_id = $_course['real_id'];
  167. $currentCourseRepository = $_course['path'];
  168. $category = self::get_category($category_id);
  169. $places = intval($places);
  170. if ($category) {
  171. if ($places == 0) {
  172. //if the amount of users per group is not filled in, use the setting from the category
  173. $places = $category['max_student'];
  174. } else {
  175. if ($places > $category['max_student'] && $category['max_student'] != 0) {
  176. $places = $category['max_student'];
  177. }
  178. }
  179. $docState = $category['doc_state'];
  180. $calendarState = $category['calendar_state'];
  181. $workState = $category['work_state'];
  182. $anonuncementState = $category['announcements_state'];
  183. $forumState = $category['forum_state'];
  184. $wikiState = $category['wiki_state'];
  185. $chatState = $category['chat_state'];
  186. $selfRegAllowed = $category['self_reg_allowed'];
  187. $selfUnregAllwoed = $category['self_unreg_allowed'];
  188. } else {
  189. $docState = self::TOOL_PRIVATE;
  190. $calendarState = self::TOOL_PRIVATE;
  191. $workState = self::TOOL_PRIVATE;
  192. $anonuncementState = self::TOOL_PRIVATE;
  193. $forumState = self::TOOL_PRIVATE;
  194. $wikiState = self::TOOL_PRIVATE;
  195. $chatState = self::TOOL_PRIVATE;
  196. $selfRegAllowed = 0;
  197. $selfUnregAllwoed = 0;
  198. }
  199. $table_group = Database::get_course_table(TABLE_GROUP);
  200. $sql = "INSERT INTO $table_group SET
  201. c_id = $course_id,
  202. status = 1,
  203. category_id='".Database::escape_string($category_id)."',
  204. max_student = '".$places."',
  205. doc_state = '".$docState."',
  206. calendar_state = '".$calendarState."',
  207. work_state = '".$workState."',
  208. announcements_state = '".$anonuncementState."',
  209. forum_state = '".$forumState."',
  210. wiki_state = '".$wikiState."',
  211. chat_state = '".$chatState."',
  212. self_registration_allowed = '".$selfRegAllowed."',
  213. self_unregistration_allowed = '".$selfUnregAllwoed."',
  214. session_id='".intval($session_id)."'";
  215. Database::query($sql);
  216. $lastId = Database::insert_id();
  217. if ($lastId) {
  218. $sql = "UPDATE $table_group SET id = iid WHERE iid = $lastId";
  219. Database::query($sql);
  220. $desired_dir_name = '/'.api_replace_dangerous_char($name).'_groupdocs';
  221. $my_path = api_get_path(SYS_COURSE_PATH).$currentCourseRepository.'/document';
  222. $newFolderData = create_unexisting_directory(
  223. $_course,
  224. api_get_user_id(),
  225. $session_id,
  226. $lastId,
  227. null,
  228. $my_path,
  229. $desired_dir_name,
  230. null,
  231. 1
  232. );
  233. $unique_name = $newFolderData['path'];
  234. /* Stores the directory path into the group table */
  235. $sql = "UPDATE $table_group SET
  236. name = '".Database::escape_string($name)."',
  237. secret_directory = '".$unique_name."'
  238. WHERE c_id = $course_id AND id ='".$lastId."'";
  239. Database::query($sql);
  240. // create a forum if needed
  241. if ($forumState >= 0) {
  242. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  243. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  244. $forum_categories = get_forum_categories();
  245. if (empty($forum_categories)) {
  246. $categoryParam = [
  247. 'forum_category_title' => get_lang('GroupForums'),
  248. ];
  249. store_forumcategory($categoryParam);
  250. $forum_categories = get_forum_categories();
  251. }
  252. $counter = 0;
  253. foreach ($forum_categories as $key => $value) {
  254. if ($counter == 0) {
  255. $forum_category_id = $key;
  256. }
  257. $counter++;
  258. }
  259. // A sanity check.
  260. if (empty($forum_category_id)) {
  261. $forum_category_id = 0;
  262. }
  263. $values = [];
  264. $values['forum_title'] = $name;
  265. $values['group_id'] = $lastId;
  266. $values['forum_category'] = $forum_category_id;
  267. $values['allow_anonymous_group']['allow_anonymous'] = 0;
  268. $values['students_can_edit_group']['students_can_edit'] = 0;
  269. $values['approval_direct_group']['approval_direct'] = 0;
  270. $values['allow_attachments_group']['allow_attachments'] = 1;
  271. $values['allow_new_threads_group']['allow_new_threads'] = 1;
  272. $values['default_view_type_group']['default_view_type'] = api_get_setting('default_forum_view');
  273. $values['group_forum'] = $lastId;
  274. if ($forumState == '1') {
  275. $values['public_private_group_forum_group']['public_private_group_forum'] = 'public';
  276. } elseif ($forumState == '2') {
  277. $values['public_private_group_forum_group']['public_private_group_forum'] = 'private';
  278. } elseif ($forumState == '0') {
  279. $values['public_private_group_forum_group']['public_private_group_forum'] = 'unavailable';
  280. }
  281. store_forum($values);
  282. }
  283. }
  284. return $lastId;
  285. }
  286. /**
  287. * Create subgroups.
  288. * This function creates new groups based on an existing group. It will
  289. * create the specified number of groups and fill those groups with users
  290. * from the base group.
  291. *
  292. * @param int $group_id the group from which subgroups have to be created
  293. * @param int $number_of_groups The number of groups that have to be created
  294. */
  295. public static function create_subgroups($group_id, $number_of_groups)
  296. {
  297. $course_id = api_get_course_int_id();
  298. $table_group = Database::get_course_table(TABLE_GROUP);
  299. $category_id = self::create_category(
  300. get_lang('Subgroups'),
  301. '',
  302. self::TOOL_PRIVATE,
  303. self::TOOL_PRIVATE,
  304. 0,
  305. 0,
  306. 1,
  307. 1
  308. );
  309. $users = self::get_users($group_id);
  310. $group_ids = [];
  311. for ($group_nr = 1; $group_nr <= $number_of_groups; $group_nr++) {
  312. $group_ids[] = self::create_group(
  313. get_lang('Subgroup').' '.$group_nr,
  314. $category_id,
  315. 0,
  316. 0
  317. );
  318. }
  319. $members = [];
  320. foreach ($users as $index => $user_id) {
  321. $groupId = $group_ids[$index % $number_of_groups];
  322. $groupInfo = self::get_group_properties($groupId);
  323. self::subscribe_users(
  324. $user_id,
  325. $groupInfo
  326. );
  327. $members[$group_ids[$groupId]]++;
  328. }
  329. foreach ($members as $group_id => $places) {
  330. $sql = "UPDATE $table_group SET max_student = $places
  331. WHERE c_id = $course_id AND id = $group_id";
  332. Database::query($sql);
  333. }
  334. }
  335. /**
  336. * Create a group for every class subscribed to the current course.
  337. *
  338. * @param int $category_id The category in which the groups should be created
  339. *
  340. * @return array
  341. */
  342. public static function create_class_groups($category_id)
  343. {
  344. $options['where'] = [" usergroup.course_id = ? " => api_get_course_int_id()];
  345. $obj = new UserGroup();
  346. $classes = $obj->getUserGroupInCourse($options);
  347. $group_ids = [];
  348. foreach ($classes as $class) {
  349. $users_ids = $obj->get_users_by_usergroup($class['id']);
  350. $group_id = self::create_group(
  351. $class['name'],
  352. $category_id,
  353. 0,
  354. count($users_ids)
  355. );
  356. $groupInfo = self::get_group_properties($group_id);
  357. self::subscribe_users($users_ids, $groupInfo);
  358. $group_ids[] = $group_id;
  359. }
  360. return $group_ids;
  361. }
  362. /**
  363. * Deletes groups and their data.
  364. *
  365. * @author Christophe Gesche <christophe.gesche@claroline.net>
  366. * @author Hugues Peeters <hugues.peeters@claroline.net>
  367. * @author Bart Mollet
  368. *
  369. * @param array $groupInfo iid
  370. * @param string $course_code Default is current course
  371. *
  372. * @return int - number of groups deleted
  373. */
  374. public static function delete_groups($groupInfo, $course_code = null)
  375. {
  376. if (empty($groupInfo['iid'])) {
  377. return false;
  378. }
  379. $course_info = api_get_course_info($course_code);
  380. $course_id = $course_info['real_id'];
  381. // Database table definitions
  382. $group_table = Database::get_course_table(TABLE_GROUP);
  383. $forum_table = Database::get_course_table(TABLE_FORUM);
  384. $groupInfo = self::get_group_properties($groupInfo['iid'], true);
  385. if ($groupInfo) {
  386. $groupIid = $groupInfo['iid'];
  387. $groupId = $groupInfo['id'];
  388. // Unsubscribe all users
  389. self::unsubscribe_all_users($groupInfo);
  390. self::unsubscribe_all_tutors($groupInfo);
  391. if (!empty($groupInfo['secret_directory'])) {
  392. $directory = $groupInfo['secret_directory'];
  393. // move group-documents to garbage
  394. $source_directory = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$directory;
  395. // File to renamed
  396. $destination_dir = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$directory.'_DELETED_'.$groupInfo['id'];
  397. //Deleting from document tool
  398. DocumentManager::delete_document(
  399. $course_info,
  400. $directory,
  401. $source_directory
  402. );
  403. if (file_exists($source_directory)) {
  404. if (api_get_setting('permanently_remove_deleted_files') === 'true') {
  405. // Delete
  406. my_delete($source_directory);
  407. } else {
  408. // Rename
  409. rename($source_directory, $destination_dir);
  410. }
  411. }
  412. }
  413. $sql = "DELETE FROM $forum_table
  414. WHERE c_id = $course_id AND forum_of_group = $groupId ";
  415. Database::query($sql);
  416. // Delete item properties of this group.
  417. // to_group_id is related to c_group_info.iid
  418. $itemPropertyTable = Database::get_course_table(TABLE_ITEM_PROPERTY);
  419. $sql = "DELETE FROM $itemPropertyTable
  420. WHERE c_id = $course_id AND to_group_id = $groupIid ";
  421. Database::query($sql);
  422. // delete the groups
  423. $sql = "DELETE FROM $group_table
  424. WHERE c_id = $course_id AND iid = $groupIid ";
  425. Database::query($sql);
  426. }
  427. return true;
  428. }
  429. /**
  430. * Get group properties.
  431. *
  432. * @param int $group_id the group from which properties are requested
  433. * @param bool $useIid
  434. *
  435. * @return array All properties. Array-keys are:
  436. * name, tutor_id, description, maximum_number_of_students,
  437. * directory and visibility of tools
  438. */
  439. public static function get_group_properties($group_id, $useIid = false)
  440. {
  441. $course_id = api_get_course_int_id();
  442. if (empty($group_id) || !is_integer(intval($group_id))) {
  443. return null;
  444. }
  445. $table_group = Database::get_course_table(TABLE_GROUP);
  446. $sql = "SELECT * FROM $table_group
  447. WHERE c_id = $course_id AND id = ".intval($group_id);
  448. if ($useIid) {
  449. $sql = "SELECT * FROM $table_group
  450. WHERE c_id = $course_id AND iid = ".intval($group_id);
  451. }
  452. $db_result = Database::query($sql);
  453. $db_object = Database::fetch_object($db_result);
  454. $result = [];
  455. if ($db_object) {
  456. $result['id'] = $db_object->id;
  457. $result['iid'] = $db_object->iid;
  458. $result['name'] = $db_object->name;
  459. $result['status'] = $db_object->status;
  460. $result['description'] = $db_object->description;
  461. $result['maximum_number_of_students'] = $db_object->max_student;
  462. $result['max_student'] = $db_object->max_student;
  463. $result['doc_state'] = $db_object->doc_state;
  464. $result['work_state'] = $db_object->work_state;
  465. $result['calendar_state'] = $db_object->calendar_state;
  466. $result['announcements_state'] = $db_object->announcements_state;
  467. $result['forum_state'] = $db_object->forum_state;
  468. $result['wiki_state'] = $db_object->wiki_state;
  469. $result['chat_state'] = $db_object->chat_state;
  470. $result['directory'] = $db_object->secret_directory;
  471. $result['self_registration_allowed'] = $db_object->self_registration_allowed;
  472. $result['self_unregistration_allowed'] = $db_object->self_unregistration_allowed;
  473. $result['count_users'] = count(
  474. self::get_subscribed_users($result)
  475. );
  476. $result['count_tutor'] = count(
  477. self::get_subscribed_tutors($result)
  478. );
  479. $result['count_all'] = $result['count_users'] + $result['count_tutor'];
  480. $result['document_access'] = isset($db_object->document_access) ? $db_object->document_access : 0;
  481. }
  482. return $result;
  483. }
  484. /**
  485. * @param string $name
  486. * @param string $courseCode
  487. * @param int $sessionId
  488. *
  489. * @return array
  490. */
  491. public static function getGroupByName($name, $courseCode = null, $sessionId = 0)
  492. {
  493. $name = trim($name);
  494. if (empty($name)) {
  495. return [];
  496. }
  497. $course_info = api_get_course_info($courseCode);
  498. $course_id = $course_info['real_id'];
  499. $name = Database::escape_string($name);
  500. $sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
  501. $sessionCondition = api_get_session_condition($sessionId);
  502. $table = Database::get_course_table(TABLE_GROUP);
  503. $sql = "SELECT * FROM $table
  504. WHERE
  505. c_id = $course_id AND
  506. name = '$name'
  507. $sessionCondition
  508. LIMIT 1";
  509. $res = Database::query($sql);
  510. $group = [];
  511. if (Database::num_rows($res)) {
  512. $group = Database::fetch_array($res, 'ASSOC');
  513. }
  514. return $group;
  515. }
  516. /**
  517. * @param int $courseId
  518. * @param int $categoryId
  519. * @param string $name
  520. *
  521. * @return array
  522. */
  523. public static function getGroupListFilterByName($name, $categoryId, $courseId)
  524. {
  525. $name = trim($name);
  526. if (empty($name)) {
  527. return [];
  528. }
  529. $name = Database::escape_string($name);
  530. $courseId = intval($courseId);
  531. $table_group = Database::get_course_table(TABLE_GROUP);
  532. $sql = "SELECT * FROM $table_group
  533. WHERE c_id = $courseId AND name LIKE '%$name%'";
  534. if (!empty($categoryId)) {
  535. $categoryId = intval($categoryId);
  536. $sql .= " AND category_id = $categoryId";
  537. }
  538. $sql .= " ORDER BY name";
  539. $result = Database::query($sql);
  540. return Database::store_result($result, 'ASSOC');
  541. }
  542. /**
  543. * Set group properties
  544. * Changes the group's properties.
  545. *
  546. * @param int Group Id
  547. * @param string Group name
  548. * @param string Group description
  549. * @param int Max number of students in group
  550. * @param int Document tool's visibility (0=none,1=private,2=public)
  551. * @param int Work tool's visibility (0=none,1=private,2=public)
  552. * @param int Calendar tool's visibility (0=none,1=private,2=public)
  553. * @param int Announcement tool's visibility (0=none,1=private,2=public)
  554. * @param int Forum tool's visibility (0=none,1=private,2=public)
  555. * @param int Wiki tool's visibility (0=none,1=private,2=public)
  556. * @param int Chat tool's visibility (0=none,1=private,2=public)
  557. * @param bool Whether self registration is allowed or not
  558. * @param bool Whether self unregistration is allowed or not
  559. * @param int $categoryId
  560. * @param int $documentAccess
  561. *
  562. * @return bool TRUE if properties are successfully changed, false otherwise
  563. */
  564. public static function set_group_properties(
  565. $group_id,
  566. $name,
  567. $description,
  568. $maximum_number_of_students,
  569. $doc_state,
  570. $work_state,
  571. $calendar_state,
  572. $announcements_state,
  573. $forum_state,
  574. $wiki_state,
  575. $chat_state,
  576. $self_registration_allowed,
  577. $self_unregistration_allowed,
  578. $categoryId = null,
  579. $documentAccess = 0
  580. ) {
  581. $table_group = Database::get_course_table(TABLE_GROUP);
  582. $table_forum = Database::get_course_table(TABLE_FORUM);
  583. $categoryId = intval($categoryId);
  584. $group_id = intval($group_id);
  585. $course_id = api_get_course_int_id();
  586. $allowDocumentAccess = api_get_configuration_value('group_document_access');
  587. $documentCondition = '';
  588. if ($allowDocumentAccess) {
  589. $documentAccess = (int) $documentAccess;
  590. $documentCondition = " document_access = $documentAccess, ";
  591. }
  592. $sql = "UPDATE ".$table_group." SET
  593. name='".Database::escape_string(trim($name))."',
  594. doc_state = '".Database::escape_string($doc_state)."',
  595. work_state = '".Database::escape_string($work_state)."',
  596. calendar_state = '".Database::escape_string($calendar_state)."',
  597. announcements_state = '".Database::escape_string($announcements_state)."',
  598. forum_state = '".Database::escape_string($forum_state)."',
  599. wiki_state = '".Database::escape_string($wiki_state)."',
  600. chat_state = '".Database::escape_string($chat_state)."',
  601. description ='".Database::escape_string(trim($description))."',
  602. max_student = '".Database::escape_string($maximum_number_of_students)."',
  603. self_registration_allowed = '".Database::escape_string($self_registration_allowed)."',
  604. self_unregistration_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  605. $documentCondition
  606. category_id = ".intval($categoryId)."
  607. WHERE c_id = $course_id AND id=".$group_id;
  608. $result = Database::query($sql);
  609. /* Here we are updating a field in the table forum_forum that perhaps
  610. duplicates the table group_info.forum_state cvargas*/
  611. $forum_state = (int) $forum_state;
  612. $sql2 = "UPDATE ".$table_forum." SET ";
  613. if ($forum_state === 1) {
  614. $sql2 .= " forum_group_public_private='public' ";
  615. } elseif ($forum_state === 2) {
  616. $sql2 .= " forum_group_public_private='private' ";
  617. } elseif ($forum_state === 0) {
  618. $sql2 .= " forum_group_public_private='unavailable' ";
  619. }
  620. $sql2 .= " WHERE c_id = $course_id AND forum_of_group=".$group_id;
  621. Database::query($sql2);
  622. return $result;
  623. }
  624. /**
  625. * Get the total number of groups for the current course.
  626. *
  627. * @return int the number of groups for the current course
  628. */
  629. public static function get_number_of_groups()
  630. {
  631. $course_id = api_get_course_int_id();
  632. $table = Database::get_course_table(TABLE_GROUP);
  633. $sql = "SELECT COUNT(id) AS number_of_groups
  634. FROM $table
  635. WHERE c_id = $course_id ";
  636. $res = Database::query($sql);
  637. $obj = Database::fetch_object($res);
  638. return $obj->number_of_groups;
  639. }
  640. /**
  641. * Get all categories.
  642. *
  643. * @param string $course_code The course (default = current course)
  644. *
  645. * @return array
  646. */
  647. public static function get_categories($course_code = null)
  648. {
  649. $course_info = api_get_course_info($course_code);
  650. $course_id = $course_info['real_id'];
  651. $table = Database::get_course_table(TABLE_GROUP_CATEGORY);
  652. $sql = "SELECT * FROM $table
  653. WHERE c_id = $course_id
  654. ORDER BY display_order";
  655. $res = Database::query($sql);
  656. $cats = [];
  657. while ($cat = Database::fetch_array($res)) {
  658. $cats[] = $cat;
  659. }
  660. return $cats;
  661. }
  662. /**
  663. * Get a group category.
  664. *
  665. * @param int $id The category id
  666. * @param string $course_code The course (default = current course)
  667. *
  668. * @return array
  669. */
  670. public static function get_category($id, $course_code = null)
  671. {
  672. if (empty($id)) {
  673. return [];
  674. }
  675. $course_info = api_get_course_info($course_code);
  676. $course_id = $course_info['real_id'];
  677. $id = intval($id);
  678. $table = Database::get_course_table(TABLE_GROUP_CATEGORY);
  679. $sql = "SELECT * FROM $table
  680. WHERE c_id = $course_id AND id = $id
  681. LIMIT 1";
  682. $res = Database::query($sql);
  683. return Database::fetch_array($res);
  684. }
  685. /**
  686. * Get a group category.
  687. *
  688. * @param string $title
  689. * @param string $course_code The course (default = current course)
  690. *
  691. * @return array
  692. */
  693. public static function getCategoryByTitle($title, $course_code = null)
  694. {
  695. $title = trim($title);
  696. if (empty($title)) {
  697. return [];
  698. }
  699. $course_info = api_get_course_info($course_code);
  700. $course_id = $course_info['real_id'];
  701. $title = Database::escape_string($title);
  702. $table = Database::get_course_table(TABLE_GROUP_CATEGORY);
  703. $sql = "SELECT * FROM $table
  704. WHERE c_id = $course_id AND title = '$title'
  705. LIMIT 1";
  706. $res = Database::query($sql);
  707. $category = [];
  708. if (Database::num_rows($res)) {
  709. $category = Database::fetch_array($res, 'ASSOC');
  710. }
  711. return $category;
  712. }
  713. /**
  714. * Get the unique category of a given group.
  715. *
  716. * @param int $group_id The iid of the group
  717. * @param string $course_code The course in which the group is (default =
  718. * current course)
  719. *
  720. * @return array The category
  721. */
  722. public static function get_category_from_group($group_id, $course_code = '')
  723. {
  724. $table_group = Database::get_course_table(TABLE_GROUP);
  725. $table_group_cat = Database::get_course_table(TABLE_GROUP_CATEGORY);
  726. $group_id = intval($group_id);
  727. if (empty($group_id)) {
  728. return [];
  729. }
  730. $course_info = api_get_course_info($course_code);
  731. if (empty($course_info)) {
  732. return false;
  733. }
  734. $course_id = $course_info['real_id'];
  735. $sql = "SELECT gc.* FROM $table_group_cat gc, $table_group g
  736. WHERE
  737. gc.c_id = $course_id AND
  738. g.c_id = $course_id AND
  739. gc.id = g.category_id AND
  740. g.iid = $group_id
  741. LIMIT 1";
  742. $res = Database::query($sql);
  743. $cat = [];
  744. if (Database::num_rows($res)) {
  745. $cat = Database::fetch_array($res);
  746. }
  747. return $cat;
  748. }
  749. /**
  750. * Delete a group category.
  751. *
  752. * @param int $cat_id The id of the category to delete
  753. * @param string $course_code The code in which the category should be
  754. * deleted (default = current course)
  755. *
  756. * @return bool
  757. */
  758. public static function delete_category($cat_id, $course_code = '')
  759. {
  760. $course_info = api_get_course_info($course_code);
  761. if (empty($course_info)) {
  762. return false;
  763. }
  764. $course_id = $course_info['real_id'];
  765. $table_group = Database::get_course_table(TABLE_GROUP);
  766. $table_group_cat = Database::get_course_table(TABLE_GROUP_CATEGORY);
  767. $cat_id = intval($cat_id);
  768. $sql = "SELECT iid FROM $table_group
  769. WHERE c_id = $course_id AND category_id='".$cat_id."'";
  770. $res = Database::query($sql);
  771. if (Database::num_rows($res) > 0) {
  772. while ($group = Database::fetch_object($res)) {
  773. // Delete all groups in category
  774. /*$groupInfo = self::get_group_properties($group->iid, true);
  775. self::delete_groups($groupInfo, $course_code);
  776. */
  777. // Set the category to NULL to avoid losing groups in sessions.
  778. $sql = "UPDATE $table_group SET category_id = NULL WHERE iid = ".$group->iid;
  779. Database::query($sql);
  780. }
  781. }
  782. $sql = "DELETE FROM $table_group_cat
  783. WHERE c_id = $course_id AND id='".$cat_id."'";
  784. Database::query($sql);
  785. return true;
  786. }
  787. /**
  788. * Create group category.
  789. *
  790. * @param string $title The title of the new category
  791. * @param string $description The description of the new category
  792. * @param int $doc_state
  793. * @param int $work_state
  794. * @param int $calendar_state
  795. * @param int $announcements_state
  796. * @param int $forum_state
  797. * @param int $wiki_state
  798. * @param int $chat_state
  799. * @param int $self_registration_allowed
  800. * @param int $self_unregistration_allowed
  801. * @param int $maximum_number_of_students
  802. * @param int $groups_per_user
  803. *
  804. * @return mixed
  805. */
  806. public static function create_category(
  807. $title,
  808. $description,
  809. $doc_state,
  810. $work_state,
  811. $calendar_state,
  812. $announcements_state,
  813. $forum_state,
  814. $wiki_state,
  815. $chat_state = 1,
  816. $self_registration_allowed = 0,
  817. $self_unregistration_allowed = 0,
  818. $maximum_number_of_students = 8,
  819. $groups_per_user = 0
  820. ) {
  821. if (empty($title)) {
  822. return false;
  823. }
  824. $table = Database::get_course_table(TABLE_GROUP_CATEGORY);
  825. $course_id = api_get_course_int_id();
  826. $sql = "SELECT MAX(display_order)+1 as new_order
  827. FROM $table
  828. WHERE c_id = $course_id ";
  829. $res = Database::query($sql);
  830. $obj = Database::fetch_object($res);
  831. if (!isset($obj->new_order)) {
  832. $obj->new_order = 1;
  833. }
  834. $params = [
  835. 'c_id' => $course_id,
  836. 'title' => $title,
  837. 'display_order' => $obj->new_order,
  838. 'description' => $description,
  839. 'doc_state' => $doc_state,
  840. 'work_state' => $work_state,
  841. 'calendar_state' => $calendar_state,
  842. 'announcements_state' => $announcements_state,
  843. 'forum_state' => $forum_state,
  844. 'wiki_state' => $wiki_state,
  845. 'chat_state' => $chat_state,
  846. 'groups_per_user' => $groups_per_user,
  847. 'self_reg_allowed' => $self_registration_allowed,
  848. 'self_unreg_allowed' => $self_unregistration_allowed,
  849. 'max_student' => $maximum_number_of_students,
  850. ];
  851. $categoryId = Database::insert($table, $params);
  852. if ($categoryId) {
  853. $sql = "UPDATE $table SET id = iid
  854. WHERE iid = $categoryId";
  855. Database::query($sql);
  856. return $categoryId;
  857. }
  858. return false;
  859. }
  860. /**
  861. * Update group category.
  862. *
  863. * @param int $id
  864. * @param string $title
  865. * @param string $description
  866. * @param $doc_state
  867. * @param $work_state
  868. * @param $calendar_state
  869. * @param $announcements_state
  870. * @param $forum_state
  871. * @param $wiki_state
  872. * @param $chat_state
  873. * @param $self_registration_allowed
  874. * @param $self_unregistration_allowed
  875. * @param $maximum_number_of_students
  876. * @param $groups_per_user
  877. */
  878. public static function update_category(
  879. $id,
  880. $title,
  881. $description,
  882. $doc_state,
  883. $work_state,
  884. $calendar_state,
  885. $announcements_state,
  886. $forum_state,
  887. $wiki_state,
  888. $chat_state,
  889. $self_registration_allowed,
  890. $self_unregistration_allowed,
  891. $maximum_number_of_students,
  892. $groups_per_user
  893. ) {
  894. $table = Database::get_course_table(TABLE_GROUP_CATEGORY);
  895. $id = intval($id);
  896. $course_id = api_get_course_int_id();
  897. $sql = "UPDATE ".$table." SET
  898. title='".Database::escape_string($title)."',
  899. description='".Database::escape_string($description)."',
  900. doc_state = '".Database::escape_string($doc_state)."',
  901. work_state = '".Database::escape_string($work_state)."',
  902. calendar_state = '".Database::escape_string($calendar_state)."',
  903. announcements_state = '".Database::escape_string($announcements_state)."',
  904. forum_state = '".Database::escape_string($forum_state)."',
  905. wiki_state = '".Database::escape_string($wiki_state)."',
  906. chat_state = '".Database::escape_string($chat_state)."',
  907. groups_per_user = '".Database::escape_string($groups_per_user)."',
  908. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  909. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  910. max_student = ".intval($maximum_number_of_students)."
  911. WHERE c_id = $course_id AND id = $id";
  912. Database::query($sql);
  913. // Updating all groups inside this category
  914. $groups = self::get_group_list($id);
  915. if (!empty($groups)) {
  916. foreach ($groups as $group) {
  917. self::set_group_properties(
  918. $group['id'],
  919. $group['name'],
  920. $group['description'],
  921. $maximum_number_of_students,
  922. $doc_state,
  923. $work_state,
  924. $calendar_state,
  925. $announcements_state,
  926. $forum_state,
  927. $wiki_state,
  928. $chat_state,
  929. $self_registration_allowed,
  930. $self_unregistration_allowed,
  931. $id
  932. );
  933. }
  934. }
  935. }
  936. /**
  937. * Returns the number of groups of the user with the greatest number of
  938. * subscriptions in the given category.
  939. */
  940. public static function get_current_max_groups_per_user(
  941. $category_id = null,
  942. $course_code = null
  943. ) {
  944. $course_info = api_get_course_info($course_code);
  945. $group_table = Database::get_course_table(TABLE_GROUP);
  946. $group_user_table = Database::get_course_table(TABLE_GROUP_USER);
  947. $sql = 'SELECT COUNT(gu.group_id) AS current_max
  948. FROM '.$group_user_table.' gu, '.$group_table.' g
  949. WHERE g.c_id = '.$course_info['real_id'].'
  950. AND gu.c_id = g.c_id
  951. AND gu.group_id = g.iid ';
  952. if ($category_id != null) {
  953. $category_id = intval($category_id);
  954. $sql .= ' AND g.category_id = '.$category_id;
  955. }
  956. $sql .= ' GROUP BY gu.user_id ORDER BY current_max DESC LIMIT 1';
  957. $res = Database::query($sql);
  958. $obj = Database::fetch_object($res);
  959. return $obj->current_max;
  960. }
  961. /**
  962. * Swaps the display-order of two categories.
  963. *
  964. * @param int $id1 The id of the first category
  965. * @param int $id2 The id of the second category
  966. */
  967. public static function swap_category_order($id1, $id2)
  968. {
  969. $table = Database::get_course_table(TABLE_GROUP_CATEGORY);
  970. $id1 = intval($id1);
  971. $id2 = intval($id2);
  972. $course_id = api_get_course_int_id();
  973. $sql = "SELECT id, display_order FROM $table
  974. WHERE id IN ($id1,$id2) AND c_id = $course_id ";
  975. $res = Database::query($sql);
  976. $cat1 = Database::fetch_object($res);
  977. $cat2 = Database::fetch_object($res);
  978. if ($cat1 && $cat2) {
  979. $sql = "UPDATE $table SET display_order=$cat2->display_order
  980. WHERE id = $cat1->id AND c_id = $course_id ";
  981. Database::query($sql);
  982. $sql = "UPDATE $table SET display_order=$cat1->display_order
  983. WHERE id = $cat2->id AND c_id = $course_id ";
  984. Database::query($sql);
  985. }
  986. }
  987. /**
  988. * Get all users from a given group.
  989. *
  990. * @param int $group_id The group
  991. * @param bool $load_extra_info
  992. * @param int $start
  993. * @param int $limit
  994. * @param bool $getCount
  995. * @param int $courseId
  996. * @param $column
  997. * @param $direction
  998. *
  999. * @return array list of user id
  1000. */
  1001. public static function get_users(
  1002. $group_id,
  1003. $load_extra_info = false,
  1004. $start = null,
  1005. $limit = null,
  1006. $getCount = false,
  1007. $courseId = null,
  1008. $column = null,
  1009. $direction = null
  1010. ) {
  1011. $group_user_table = Database::get_course_table(TABLE_GROUP_USER);
  1012. $groupTable = Database::get_course_table(TABLE_GROUP);
  1013. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  1014. $group_id = intval($group_id);
  1015. if (empty($courseId)) {
  1016. $courseId = api_get_course_int_id();
  1017. } else {
  1018. $courseId = intval($courseId);
  1019. }
  1020. $select = " SELECT u.id, firstname, lastname ";
  1021. if ($getCount) {
  1022. $select = " SELECT count(u.id) count";
  1023. }
  1024. $sql = "$select
  1025. FROM $group_user_table gu
  1026. INNER JOIN $groupTable g
  1027. ON (gu.group_id = g.id and g.c_id = gu.c_id)
  1028. INNER JOIN $user_table u
  1029. ON (u.id = gu.user_id)
  1030. WHERE
  1031. gu.c_id = $courseId AND
  1032. g.id = $group_id";
  1033. if (!empty($column) && !empty($direction)) {
  1034. $column = Database::escape_string($column, null, false);
  1035. $direction = ($direction == 'ASC' ? 'ASC' : 'DESC');
  1036. $sql .= " ORDER BY $column $direction";
  1037. }
  1038. if (!empty($start) && !empty($limit)) {
  1039. $start = intval($start);
  1040. $limit = intval($limit);
  1041. $sql .= " LIMIT $start, $limit";
  1042. }
  1043. $res = Database::query($sql);
  1044. $users = [];
  1045. while ($obj = Database::fetch_object($res)) {
  1046. if ($getCount) {
  1047. return $obj->count;
  1048. break;
  1049. }
  1050. if ($load_extra_info) {
  1051. $users[] = api_get_user_info($obj->id);
  1052. } else {
  1053. $users[] = $obj->id;
  1054. }
  1055. }
  1056. return $users;
  1057. }
  1058. /**
  1059. * @param int $group_id id
  1060. *
  1061. * @return array
  1062. */
  1063. public static function getStudentsAndTutors($group_id)
  1064. {
  1065. $group_user_table = Database::get_course_table(TABLE_GROUP_USER);
  1066. $tutor_user_table = Database::get_course_table(TABLE_GROUP_TUTOR);
  1067. $groupTable = Database::get_course_table(TABLE_GROUP);
  1068. $course_id = api_get_course_int_id();
  1069. $group_id = intval($group_id);
  1070. $sql = "SELECT user_id
  1071. FROM $group_user_table gu
  1072. INNER JOIN $groupTable g
  1073. ON (gu.group_id = g.iid and g.c_id = gu.c_id)
  1074. WHERE gu.c_id = $course_id AND g.id = $group_id";
  1075. $res = Database::query($sql);
  1076. $users = [];
  1077. while ($obj = Database::fetch_object($res)) {
  1078. $users[] = api_get_user_info($obj->user_id);
  1079. }
  1080. $sql = "SELECT user_id
  1081. FROM $tutor_user_table gu
  1082. INNER JOIN $groupTable g
  1083. ON (gu.group_id = g.id and g.c_id = gu.c_id)
  1084. WHERE gu.c_id = $course_id AND g.id = $group_id";
  1085. $res = Database::query($sql);
  1086. while ($obj = Database::fetch_object($res)) {
  1087. $users[] = api_get_user_info($obj->user_id);
  1088. }
  1089. return $users;
  1090. }
  1091. /**
  1092. * Get only tutors from a group.
  1093. *
  1094. * @param array $groupInfo
  1095. *
  1096. * @return array
  1097. */
  1098. public static function getTutors($groupInfo)
  1099. {
  1100. $groupTable = Database::get_course_table(TABLE_GROUP);
  1101. $tutor_user_table = Database::get_course_table(TABLE_GROUP_TUTOR);
  1102. $course_id = api_get_course_int_id();
  1103. $group_id = intval($groupInfo['iid']);
  1104. $sql = "SELECT user_id
  1105. FROM $tutor_user_table gu
  1106. INNER JOIN $groupTable g
  1107. ON (gu.group_id = g.id and g.c_id = gu.c_id)
  1108. WHERE gu.c_id = $course_id AND g.iid = $group_id";
  1109. $res = Database::query($sql);
  1110. $users = [];
  1111. while ($obj = Database::fetch_object($res)) {
  1112. $users[] = api_get_user_info($obj->user_id);
  1113. }
  1114. return $users;
  1115. }
  1116. /**
  1117. * Get only students from a group (not tutors).
  1118. *
  1119. * @param int $group_id iid
  1120. *
  1121. * @return array
  1122. */
  1123. public static function getStudents($group_id)
  1124. {
  1125. $em = Database::getManager();
  1126. $subscriptions = $em
  1127. ->createQuery('
  1128. SELECT gu
  1129. FROM ChamiloCourseBundle:CGroupRelUser gu
  1130. INNER JOIN ChamiloCourseBundle:CGroupInfo g
  1131. WITH gu.groupId = g.id AND g.cId = gu.cId
  1132. WHERE gu.cId = :course AND g.id = :group
  1133. ')
  1134. ->setParameters([
  1135. 'course' => api_get_course_int_id(),
  1136. 'group' => intval($group_id),
  1137. ])
  1138. ->getResult();
  1139. $users = [];
  1140. /** @var CGroupRelUser $subscription */
  1141. foreach ($subscriptions as $subscription) {
  1142. $users[] = api_get_user_info($subscription->getUserId());
  1143. }
  1144. return $users;
  1145. }
  1146. /**
  1147. * Returns users belonging to any of the group.
  1148. *
  1149. * @param array $groups list of group ids
  1150. *
  1151. * @return array list of user ids
  1152. */
  1153. public static function get_groups_users($groups = [])
  1154. {
  1155. $result = [];
  1156. $table = Database::get_course_table(TABLE_GROUP_USER);
  1157. $course_id = api_get_course_int_id();
  1158. $groups = array_map('intval', $groups);
  1159. // protect individual elements with surrounding quotes
  1160. $groups = implode(', ', $groups);
  1161. $sql = "SELECT DISTINCT user_id
  1162. FROM $table gu
  1163. WHERE c_id = $course_id AND gu.group_id IN ($groups)";
  1164. $rs = Database::query($sql);
  1165. while ($row = Database::fetch_array($rs)) {
  1166. $result[] = $row['user_id'];
  1167. }
  1168. return $result;
  1169. }
  1170. /**
  1171. * Fill the groups with students.
  1172. * The algorithm takes care to first fill the groups with the least # of users.
  1173. * Analysis
  1174. * There was a problem with the "ALL" setting.
  1175. * When max # of groups is set to all, the value is sometimes NULL and sometimes ALL
  1176. * and in both cased the query does not work as expected.
  1177. * Stupid solution (currently implemented: set ALL to a big number (INFINITE) and things are solved :)
  1178. * Better solution: that's up to you.
  1179. *
  1180. * Note
  1181. * Throughout Dokeos there is some confusion about "course id" and "course code"
  1182. * The code is e.g. TEST101, but sometimes a variable that is called courseID also contains a course code string.
  1183. * However, there is also a integer course_id that uniquely identifies the course.
  1184. * ywarnier:> Now the course_id has been removed (25/1/2005)
  1185. * The databases are als very inconsistent in this.
  1186. *
  1187. * @param array $groupInfo
  1188. *
  1189. * @author Chrisptophe Gesche <christophe.geshe@claroline.net>,
  1190. * Hugues Peeters <hugues.peeters@claroline.net> - original version
  1191. * @author Roan Embrechts - virtual course support, code cleaning
  1192. * @author Bart Mollet - code cleaning, use other GroupManager-functions
  1193. *
  1194. * @return bool
  1195. */
  1196. public static function fillGroupWithUsers($groupInfo)
  1197. {
  1198. $_course = api_get_course_info();
  1199. if (empty($_course) || empty($groupInfo)) {
  1200. return false;
  1201. }
  1202. $session_id = api_get_session_id();
  1203. $complete_user_list = CourseManager::get_user_list_from_course_code(
  1204. $_course['code'],
  1205. $session_id
  1206. );
  1207. $groupIid = $groupInfo['iid'];
  1208. $category = self::get_category_from_group($groupIid);
  1209. // Getting max numbers of user from group
  1210. $maxNumberStudents = empty($groupInfo['maximum_number_of_students']) ? self::INFINITE : $groupInfo['maximum_number_of_students'];
  1211. $groupsPerUser = self::INFINITE;
  1212. $categoryId = 0;
  1213. if ($category) {
  1214. $groupsPerUser = empty($category['groups_per_user']) ? self::INFINITE : $category['groups_per_user'];
  1215. $maxNumberStudentsCategory = empty($category['max_student']) ? self::INFINITE : $category['max_student'];
  1216. $categoryId = $category['id'];
  1217. if ($maxNumberStudentsCategory < $maxNumberStudents) {
  1218. $maxNumberStudents = $maxNumberStudentsCategory;
  1219. }
  1220. }
  1221. $usersToAdd = [];
  1222. foreach ($complete_user_list as $userInfo) {
  1223. $isSubscribed = self::is_subscribed($userInfo['user_id'], $groupInfo);
  1224. if ($isSubscribed) {
  1225. continue;
  1226. }
  1227. $numberOfGroups = self::user_in_number_of_groups(
  1228. $userInfo['user_id'],
  1229. $categoryId
  1230. );
  1231. if ($groupsPerUser > $numberOfGroups) {
  1232. $usersToAdd[] = $userInfo['user_id'];
  1233. }
  1234. if (count($usersToAdd) == $maxNumberStudents) {
  1235. break;
  1236. }
  1237. }
  1238. foreach ($usersToAdd as $userId) {
  1239. self::subscribe_users($userId, $groupInfo);
  1240. }
  1241. }
  1242. /**
  1243. * Get the number of students in a group.
  1244. *
  1245. * @param int $group_id id
  1246. *
  1247. * @return int number of students in the given group
  1248. */
  1249. public static function number_of_students($group_id, $course_id = null)
  1250. {
  1251. $table = Database::get_course_table(TABLE_GROUP_USER);
  1252. $group_id = intval($group_id);
  1253. if (empty($course_id)) {
  1254. $course_id = api_get_course_int_id();
  1255. } else {
  1256. $course_id = intval($course_id);
  1257. }
  1258. $sql = "SELECT COUNT(*) AS number_of_students
  1259. FROM $table
  1260. WHERE c_id = $course_id AND group_id = $group_id";
  1261. $result = Database::query($sql);
  1262. $db_object = Database::fetch_object($result);
  1263. return $db_object->number_of_students;
  1264. }
  1265. /**
  1266. * Maximum number of students in a group.
  1267. *
  1268. * @param int $group_id iid
  1269. *
  1270. * @return int maximum number of students in the given group
  1271. */
  1272. public static function maximum_number_of_students($group_id)
  1273. {
  1274. $table = Database::get_course_table(TABLE_GROUP);
  1275. $group_id = intval($group_id);
  1276. $course_id = api_get_course_int_id();
  1277. $sql = "SELECT max_student FROM $table
  1278. WHERE c_id = $course_id AND iid = $group_id";
  1279. $db_result = Database::query($sql);
  1280. $db_object = Database::fetch_object($db_result);
  1281. if ($db_object->max_student == 0) {
  1282. return self::INFINITE;
  1283. }
  1284. return $db_object->max_student;
  1285. }
  1286. /**
  1287. * Number of groups of a user.
  1288. *
  1289. * @param int $user_id
  1290. * @param int $cat_id
  1291. *
  1292. * @return int the number of groups the user is subscribed in
  1293. */
  1294. public static function user_in_number_of_groups($user_id, $cat_id = 0)
  1295. {
  1296. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1297. $table_group = Database::get_course_table(TABLE_GROUP);
  1298. $user_id = (int) $user_id;
  1299. $cat_id = (int) $cat_id;
  1300. $course_id = api_get_course_int_id();
  1301. $cat_condition = '';
  1302. if (!empty($cat_id)) {
  1303. $cat_condition = " AND g.category_id = $cat_id ";
  1304. }
  1305. $sql = "SELECT COUNT(*) AS number_of_groups
  1306. FROM $table_group_user gu
  1307. INNER JOIN $table_group g
  1308. ON (g.iid = gu.group_id AND gu.c_id = g.c_id)
  1309. WHERE
  1310. gu.c_id = $course_id AND
  1311. g.c_id = $course_id AND
  1312. gu.user_id = $user_id
  1313. $cat_condition";
  1314. $result = Database::query($sql);
  1315. $db_object = Database::fetch_object($result);
  1316. return $db_object->number_of_groups;
  1317. }
  1318. /**
  1319. * Is sef-registration allowed?
  1320. *
  1321. * @param int $user_id
  1322. * @param array $groupInfo
  1323. *
  1324. * @return bool TRUE if self-registration is allowed in the given group
  1325. */
  1326. public static function is_self_registration_allowed($user_id, $groupInfo)
  1327. {
  1328. $course_id = api_get_course_int_id();
  1329. if (!$user_id > 0) {
  1330. return false;
  1331. }
  1332. $groupIid = $groupInfo['iid'];
  1333. $table = Database::get_course_table(TABLE_GROUP);
  1334. if (isset($groupIid)) {
  1335. $sql = "SELECT status, self_registration_allowed
  1336. FROM $table
  1337. WHERE c_id = $course_id AND iid = $groupIid";
  1338. $result = Database::query($sql);
  1339. $group = Database::fetch_object($result);
  1340. if ($group->status == 0 || $group->self_registration_allowed != 1) {
  1341. return false;
  1342. }
  1343. return self::canUserSubscribe($user_id, $groupInfo);
  1344. } else {
  1345. return false;
  1346. }
  1347. }
  1348. /**
  1349. * Is sef-unregistration allowed?
  1350. *
  1351. * @param int $user_id
  1352. * @param array $groupInfo
  1353. *
  1354. * @return bool TRUE if self-unregistration is allowed in the given group
  1355. */
  1356. public static function is_self_unregistration_allowed($user_id, $groupInfo)
  1357. {
  1358. if (!$user_id > 0 || empty($groupInfo)) {
  1359. return false;
  1360. }
  1361. $groupIid = $groupInfo['iid'];
  1362. $table = Database::get_course_table(TABLE_GROUP);
  1363. $course_id = api_get_course_int_id();
  1364. $sql = "SELECT status, self_unregistration_allowed
  1365. FROM $table
  1366. WHERE c_id = $course_id AND iid = $groupIid";
  1367. $result = Database::query($sql);
  1368. $group = Database::fetch_object($result);
  1369. if ($group->status == 0 || $group->self_unregistration_allowed != 1) {
  1370. return false;
  1371. }
  1372. return self::is_subscribed($user_id, $groupInfo);
  1373. }
  1374. /**
  1375. * Is user subscribed in group?
  1376. *
  1377. * @param int $user_id
  1378. * @param array $groupInfo
  1379. *
  1380. * @return bool TRUE if given user is subscribed in given group
  1381. */
  1382. public static function is_subscribed($user_id, $groupInfo)
  1383. {
  1384. $course_id = api_get_course_int_id();
  1385. if (empty($user_id) || empty($groupInfo) || empty($course_id)) {
  1386. return false;
  1387. }
  1388. $table = Database::get_course_table(TABLE_GROUP_USER);
  1389. $group_id = intval($groupInfo['id']);
  1390. $user_id = intval($user_id);
  1391. $sql = "SELECT 1 FROM $table
  1392. WHERE
  1393. c_id = $course_id AND
  1394. group_id = $group_id AND
  1395. user_id = $user_id
  1396. ";
  1397. $result = Database::query($sql);
  1398. return Database::num_rows($result) > 0;
  1399. }
  1400. /**
  1401. * Can a user subscribe to a specified group in a course.
  1402. *
  1403. * @param int $user_id
  1404. * @param array $groupInfo
  1405. * @param bool $checkMaxNumberStudents
  1406. *
  1407. * @return bool TRUE if given user can be subscribed in given group
  1408. */
  1409. public static function canUserSubscribe(
  1410. $user_id,
  1411. $groupInfo,
  1412. $checkMaxNumberStudents = true
  1413. ) {
  1414. $group_id = $groupInfo['id'];
  1415. $groupIid = $groupInfo['iid'];
  1416. if ($checkMaxNumberStudents) {
  1417. $category = self::get_category_from_group($groupIid);
  1418. if ($category) {
  1419. if ($category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT) {
  1420. $category['groups_per_user'] = self::INFINITE;
  1421. }
  1422. $result = self::user_in_number_of_groups($user_id, $category['id']) < $category['groups_per_user'];
  1423. if ($result == false) {
  1424. return false;
  1425. }
  1426. }
  1427. $result = self::number_of_students($group_id) < self::maximum_number_of_students($groupIid);
  1428. if ($result == false) {
  1429. return false;
  1430. }
  1431. }
  1432. $result = self::is_tutor_of_group($user_id, $groupInfo);
  1433. if ($result) {
  1434. return false;
  1435. }
  1436. $result = self::is_subscribed($user_id, $groupInfo);
  1437. if ($result) {
  1438. return false;
  1439. }
  1440. return true;
  1441. }
  1442. /**
  1443. * Get all subscribed users (members) from a group.
  1444. *
  1445. * @param array $groupInfo
  1446. *
  1447. * @return array An array with information of all users from the given group.
  1448. * (user_id, firstname, lastname, email)
  1449. */
  1450. public static function get_subscribed_users($groupInfo)
  1451. {
  1452. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1453. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1454. $order_clause = api_sort_by_first_name() ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1455. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  1456. if ($orderListByOfficialCode === 'true') {
  1457. $order_clause = " ORDER BY u.official_code, u.firstname, u.lastname";
  1458. }
  1459. $group_id = intval($groupInfo['id']);
  1460. if (empty($group_id)) {
  1461. return [];
  1462. }
  1463. $course_id = api_get_course_int_id();
  1464. $sql = "SELECT
  1465. ug.id,
  1466. u.user_id,
  1467. u.lastname,
  1468. u.firstname,
  1469. u.email,
  1470. u.username
  1471. FROM $table_user u
  1472. INNER JOIN $table_group_user ug
  1473. ON (ug.user_id = u.user_id)
  1474. WHERE ug.c_id = $course_id AND
  1475. ug.group_id = $group_id
  1476. $order_clause";
  1477. $db_result = Database::query($sql);
  1478. $users = [];
  1479. while ($user = Database::fetch_object($db_result)) {
  1480. $users[$user->user_id] = [
  1481. 'user_id' => $user->user_id,
  1482. 'firstname' => $user->firstname,
  1483. 'lastname' => $user->lastname,
  1484. 'email' => $user->email,
  1485. 'username' => $user->username,
  1486. ];
  1487. }
  1488. return $users;
  1489. }
  1490. /**
  1491. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1492. * Get all subscribed tutors of a group
  1493. *
  1494. * @param array $groupInfo
  1495. *
  1496. * @return array An array with information of all users from the given group.
  1497. * (user_id, firstname, lastname, email)
  1498. */
  1499. public static function get_subscribed_tutors($groupInfo, $id_only = false)
  1500. {
  1501. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  1502. $table_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1503. $order_clause = api_sort_by_first_name() ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1504. $orderListByOfficialCode = api_get_setting('order_user_list_by_official_code');
  1505. if ($orderListByOfficialCode === 'true') {
  1506. $order_clause = " ORDER BY u.official_code, u.firstname, u.lastname";
  1507. }
  1508. $group_id = intval($groupInfo['id']);
  1509. $course_id = api_get_course_int_id();
  1510. $sql = "SELECT tg.id, u.user_id, u.lastname, u.firstname, u.email
  1511. FROM $table_user u, $table_group_tutor tg
  1512. WHERE
  1513. tg.c_id = $course_id AND
  1514. tg.group_id = $group_id AND
  1515. tg.user_id = u.user_id
  1516. $order_clause
  1517. ";
  1518. $db_result = Database::query($sql);
  1519. $users = [];
  1520. while ($user = Database::fetch_object($db_result)) {
  1521. if (!$id_only) {
  1522. $member['user_id'] = $user->user_id;
  1523. $member['firstname'] = $user->firstname;
  1524. $member['lastname'] = $user->lastname;
  1525. $member['email'] = $user->email;
  1526. $users[] = $member;
  1527. } else {
  1528. $users[] = $user->user_id;
  1529. }
  1530. }
  1531. return $users;
  1532. }
  1533. /**
  1534. * Subscribe user(s) to a specified group in current course (as a student).
  1535. *
  1536. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1537. * @param array $groupInfo
  1538. * @param int $course_id
  1539. *
  1540. * @return bool TRUE if successful
  1541. */
  1542. public static function subscribe_users($user_ids, $groupInfo, $course_id = null)
  1543. {
  1544. $user_ids = is_array($user_ids) ? $user_ids : [$user_ids];
  1545. $course_id = empty($course_id) ? api_get_course_int_id() : (int) $course_id;
  1546. $group_id = $groupInfo['id'];
  1547. $table = Database::get_course_table(TABLE_GROUP_USER);
  1548. if (!empty($user_ids)) {
  1549. foreach ($user_ids as $user_id) {
  1550. if (self::canUserSubscribe($user_id, $groupInfo)) {
  1551. $user_id = intval($user_id);
  1552. $sql = "INSERT INTO ".$table." (c_id, user_id, group_id)
  1553. VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1554. Database::query($sql);
  1555. }
  1556. }
  1557. }
  1558. return true;
  1559. }
  1560. /**
  1561. * Subscribe tutor(s) to a specified group in current course.
  1562. *
  1563. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1564. * @param array $groupInfo
  1565. * @param int $course_id
  1566. *
  1567. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1568. *
  1569. * @see subscribe_users. This function is almost an exact copy of that function.
  1570. *
  1571. * @return bool TRUE if successful
  1572. */
  1573. public static function subscribe_tutors($user_ids, $groupInfo, $course_id = 0)
  1574. {
  1575. $user_ids = is_array($user_ids) ? $user_ids : [$user_ids];
  1576. $result = true;
  1577. $course_id = isset($course_id) && !empty($course_id) ? intval($course_id) : api_get_course_int_id();
  1578. $table_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1579. $groupId = (int) $groupInfo['id'];
  1580. foreach ($user_ids as $user_id) {
  1581. $user_id = intval($user_id);
  1582. if (self::canUserSubscribe($user_id, $groupInfo, false)) {
  1583. $sql = "INSERT INTO ".$table_group_tutor." (c_id, user_id, group_id)
  1584. VALUES ('$course_id', '".$user_id."', '".$groupId."')";
  1585. $result = Database::query($sql);
  1586. }
  1587. }
  1588. return $result;
  1589. }
  1590. /**
  1591. * Unsubscribe user(s) from a specified group in current course.
  1592. *
  1593. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1594. * @param array $groupInfo
  1595. *
  1596. * @return bool TRUE if successful
  1597. */
  1598. public static function unsubscribe_users($user_ids, $groupInfo)
  1599. {
  1600. $user_ids = is_array($user_ids) ? $user_ids : [$user_ids];
  1601. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1602. $group_id = intval($groupInfo['id']);
  1603. $course_id = api_get_course_int_id();
  1604. $sql = 'DELETE FROM '.$table_group_user.'
  1605. WHERE
  1606. c_id = '.$course_id.' AND
  1607. group_id = '.$group_id.' AND
  1608. user_id IN ('.implode(',', $user_ids).')
  1609. ';
  1610. Database::query($sql);
  1611. }
  1612. /**
  1613. * Unsubscribe all users from one or more groups.
  1614. *
  1615. * @param array $groupInfo
  1616. *
  1617. * @return bool TRUE if successful
  1618. */
  1619. public static function unsubscribe_all_users($groupInfo)
  1620. {
  1621. $course_id = api_get_course_int_id();
  1622. $groupId = (int) $groupInfo['id'];
  1623. if (empty($course_id) || empty($groupId)) {
  1624. return false;
  1625. }
  1626. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1627. $sql = "DELETE FROM $table_group_user
  1628. WHERE
  1629. group_id = $groupId AND
  1630. c_id = $course_id";
  1631. $result = Database::query($sql);
  1632. return $result;
  1633. }
  1634. /**
  1635. * Unsubscribe all tutors from one or more groups.
  1636. *
  1637. * @param int $groupId iid
  1638. *
  1639. * @see unsubscribe_all_users. This function is almost an exact copy of that function.
  1640. *
  1641. * @return bool TRUE if successful
  1642. *
  1643. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1644. */
  1645. public static function unsubscribe_all_tutors($groupId)
  1646. {
  1647. $courseId = api_get_course_int_id();
  1648. $groupId = (int) $groupId;
  1649. if (empty($courseId) || empty($groupId)) {
  1650. return false;
  1651. }
  1652. if (!empty($groupId) > 0) {
  1653. $table_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1654. $sql = "DELETE FROM $table_group_tutor
  1655. WHERE group_id = $groupId AND c_id = $courseId";
  1656. $result = Database::query($sql);
  1657. return $result;
  1658. }
  1659. return true;
  1660. }
  1661. /**
  1662. * Is the user a tutor of this group?
  1663. *
  1664. * @param int $user_id the id of the user
  1665. * @param array $groupInfo
  1666. * @param int $courseId
  1667. *
  1668. * @return bool true/false
  1669. *
  1670. * @todo use the function user_has_access that includes this function
  1671. *
  1672. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1673. */
  1674. public static function is_tutor_of_group($user_id, $groupInfo, $courseId = 0)
  1675. {
  1676. if (empty($groupInfo)) {
  1677. return false;
  1678. }
  1679. $courseId = empty($courseId) ? api_get_course_int_id() : (int) $courseId;
  1680. if (empty($courseId)) {
  1681. return false;
  1682. }
  1683. $user_id = intval($user_id);
  1684. $group_id = intval($groupInfo['id']);
  1685. $table = Database::get_course_table(TABLE_GROUP_TUTOR);
  1686. $sql = "SELECT * FROM $table
  1687. WHERE
  1688. c_id = $courseId AND
  1689. user_id = $user_id AND
  1690. group_id = $group_id";
  1691. $result = Database::query($sql);
  1692. if (Database::num_rows($result) > 0) {
  1693. return true;
  1694. } else {
  1695. return false;
  1696. }
  1697. }
  1698. /**
  1699. * Is the user part of this group? This can be a tutor or a normal member
  1700. * you should use this function if the access to a tool or functionality is
  1701. * restricted to the people who are actually in the group
  1702. * before you had to check if the user was
  1703. * 1. a member of the group OR
  1704. * 2. a tutor of the group. This function combines both.
  1705. *
  1706. * @param int $user_id the id of the user
  1707. * @param array $groupInfo
  1708. *
  1709. * @return bool true/false
  1710. *
  1711. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1712. */
  1713. public static function is_user_in_group($user_id, $groupInfo)
  1714. {
  1715. $member = self::is_subscribed($user_id, $groupInfo);
  1716. $tutor = self::is_tutor_of_group($user_id, $groupInfo);
  1717. if ($member || $tutor) {
  1718. return true;
  1719. } else {
  1720. return false;
  1721. }
  1722. }
  1723. /**
  1724. * Get all group's from a given course in which a given user is unsubscribed.
  1725. *
  1726. * @author Patrick Cool
  1727. *
  1728. * @param int $course_id retrieve the groups for
  1729. * @param int $user_id the ID of the user you want to know all its group memberships
  1730. *
  1731. * @return array
  1732. */
  1733. public static function get_group_ids($course_id, $user_id)
  1734. {
  1735. $groups = [];
  1736. $tbl_group = Database::get_course_table(TABLE_GROUP_USER);
  1737. $tbl_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1738. $user_id = intval($user_id);
  1739. $course_id = intval($course_id);
  1740. $sql = "SELECT group_id FROM $tbl_group
  1741. WHERE c_id = $course_id AND user_id = '$user_id'";
  1742. $result = Database::query($sql);
  1743. if ($result) {
  1744. while ($row = Database::fetch_array($result)) {
  1745. $groups[] = $row['group_id'];
  1746. }
  1747. }
  1748. //Also loading if i'm the tutor
  1749. $sql = "SELECT group_id FROM $tbl_group_tutor
  1750. WHERE c_id = $course_id AND user_id = '$user_id'";
  1751. $result = Database::query($sql);
  1752. if ($result) {
  1753. while ($row = Database::fetch_array($result)) {
  1754. $groups[] = $row['group_id'];
  1755. }
  1756. }
  1757. if (!empty($groups)) {
  1758. array_filter($groups);
  1759. }
  1760. return $groups;
  1761. }
  1762. /**
  1763. * Remove all users that are not students and all users who have tutor status
  1764. * from the list.
  1765. *
  1766. * @param array $user_array_in
  1767. *
  1768. * @return array
  1769. */
  1770. public static function filter_only_students($user_array_in)
  1771. {
  1772. $user_array_out = [];
  1773. foreach ($user_array_in as $this_user) {
  1774. if (api_get_session_id()) {
  1775. if ($this_user['status_session'] == 0) {
  1776. $user_array_out[] = $this_user;
  1777. }
  1778. } else {
  1779. if ($this_user['status_rel'] == STUDENT) {
  1780. $user_array_out[] = $this_user;
  1781. }
  1782. }
  1783. }
  1784. return $user_array_out;
  1785. }
  1786. /**
  1787. * Check if a user has access to a certain group tool.
  1788. *
  1789. * @param int $user_id The user id
  1790. * @param int $group_id The group iid
  1791. * @param string $tool The tool to check the access rights. This should be
  1792. * one of constants: GROUP_TOOL_DOCUMENTS
  1793. *
  1794. * @return bool true if the given user has access to the given tool in the
  1795. * given course
  1796. */
  1797. public static function user_has_access($user_id, $group_id, $tool)
  1798. {
  1799. // Admin have access everywhere
  1800. if (api_is_platform_admin()) {
  1801. return true;
  1802. }
  1803. // Course admin also have access to everything
  1804. if (api_is_allowed_to_edit()) {
  1805. return true;
  1806. }
  1807. switch ($tool) {
  1808. case self::GROUP_TOOL_FORUM:
  1809. $key = 'forum_state';
  1810. break;
  1811. case self::GROUP_TOOL_DOCUMENTS:
  1812. $key = 'doc_state';
  1813. break;
  1814. case self::GROUP_TOOL_CALENDAR:
  1815. $key = 'calendar_state';
  1816. break;
  1817. case self::GROUP_TOOL_ANNOUNCEMENT:
  1818. $key = 'announcements_state';
  1819. break;
  1820. case self::GROUP_TOOL_WORK:
  1821. $key = 'work_state';
  1822. break;
  1823. case self::GROUP_TOOL_WIKI:
  1824. $key = 'wiki_state';
  1825. break;
  1826. case self::GROUP_TOOL_CHAT:
  1827. $key = 'chat_state';
  1828. break;
  1829. default:
  1830. return false;
  1831. }
  1832. // Check group properties
  1833. $groupInfo = self::get_group_properties($group_id, true);
  1834. if (empty($groupInfo)) {
  1835. return false;
  1836. }
  1837. if ($groupInfo['status'] == 0) {
  1838. return false;
  1839. }
  1840. if (!isset($groupInfo[$key])) {
  1841. return false;
  1842. }
  1843. if (api_is_allowed_to_edit(false, true)) {
  1844. return true;
  1845. }
  1846. $status = $groupInfo[$key];
  1847. switch ($status) {
  1848. case self::TOOL_NOT_AVAILABLE:
  1849. return false;
  1850. break;
  1851. case self::TOOL_PUBLIC:
  1852. return true;
  1853. break;
  1854. case self::TOOL_PRIVATE:
  1855. $userIsInGroup = self::is_user_in_group($user_id, $groupInfo);
  1856. if ($userIsInGroup) {
  1857. return true;
  1858. }
  1859. break;
  1860. }
  1861. return false;
  1862. }
  1863. /**
  1864. * @param int $userId
  1865. * @param array $groupInfo
  1866. * @param int $sessionId
  1867. *
  1868. * @return bool
  1869. */
  1870. public static function userHasAccessToBrowse($userId, $groupInfo, $sessionId = 0)
  1871. {
  1872. if (empty($groupInfo)) {
  1873. return false;
  1874. }
  1875. if (api_is_platform_admin()) {
  1876. return true;
  1877. }
  1878. if (api_is_allowed_to_edit(false, true)) {
  1879. return true;
  1880. }
  1881. $groupId = $groupInfo['iid'];
  1882. $tutors = self::get_subscribed_tutors($groupInfo, true);
  1883. if (in_array($userId, $tutors)) {
  1884. return true;
  1885. }
  1886. if ($groupInfo['status'] == 0) {
  1887. return false;
  1888. }
  1889. if (self::user_has_access($userId, $groupId, self::GROUP_TOOL_FORUM) ||
  1890. self::user_has_access($userId, $groupId, self::GROUP_TOOL_DOCUMENTS) ||
  1891. self::user_has_access($userId, $groupId, self::GROUP_TOOL_CALENDAR) ||
  1892. self::user_has_access($userId, $groupId, self::GROUP_TOOL_ANNOUNCEMENT) ||
  1893. self::user_has_access($userId, $groupId, self::GROUP_TOOL_WORK) ||
  1894. self::user_has_access($userId, $groupId, self::GROUP_TOOL_WIKI) ||
  1895. self::user_has_access($userId, $groupId, self::GROUP_TOOL_CHAT)
  1896. ) {
  1897. return true;
  1898. }
  1899. if (api_is_session_general_coach() && $groupInfo['session_id'] == $sessionId) {
  1900. return true;
  1901. }
  1902. return false;
  1903. }
  1904. /**
  1905. * Get all groups where a specific user is subscribed.
  1906. *
  1907. * @param int $user_id
  1908. *
  1909. * @return array
  1910. */
  1911. public static function get_user_group_name($user_id)
  1912. {
  1913. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1914. $table_group = Database::get_course_table(TABLE_GROUP);
  1915. $user_id = intval($user_id);
  1916. $course_id = api_get_course_int_id();
  1917. $sql = "SELECT name
  1918. FROM $table_group g
  1919. INNER JOIN $table_group_user gu
  1920. ON (gu.group_id = g.iid)
  1921. WHERE
  1922. gu.c_id= $course_id AND
  1923. g.c_id= $course_id AND
  1924. gu.user_id = $user_id";
  1925. $res = Database::query($sql);
  1926. $groups = [];
  1927. while ($group = Database::fetch_array($res)) {
  1928. $groups[] .= $group['name'];
  1929. }
  1930. return $groups;
  1931. }
  1932. /**
  1933. * Get all groups where a specific user is subscribed.
  1934. *
  1935. * @param int $user_id
  1936. *
  1937. * @return array
  1938. */
  1939. public static function getAllGroupPerUserSubscription($user_id)
  1940. {
  1941. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1942. $table_tutor_user = Database::get_course_table(TABLE_GROUP_TUTOR);
  1943. $table_group = Database::get_course_table(TABLE_GROUP);
  1944. $user_id = intval($user_id);
  1945. $course_id = api_get_course_int_id();
  1946. $sql = "SELECT DISTINCT g.*
  1947. FROM $table_group g
  1948. LEFT JOIN $table_group_user gu
  1949. ON (gu.group_id = g.iid AND g.c_id = gu.c_id)
  1950. LEFT JOIN $table_tutor_user tu
  1951. ON (tu.group_id = g.iid AND g.c_id = tu.c_id)
  1952. WHERE
  1953. g.c_id = $course_id AND
  1954. (gu.user_id = $user_id OR tu.user_id = $user_id) ";
  1955. $res = Database::query($sql);
  1956. $groups = [];
  1957. while ($group = Database::fetch_array($res, 'ASSOC')) {
  1958. $groups[] = $group;
  1959. }
  1960. return $groups;
  1961. }
  1962. /**
  1963. * @param array $userList
  1964. * @param array $groupInfo
  1965. *
  1966. * @return mixed
  1967. */
  1968. public static function getNumberLeftFromGroupFromUserList($userList, $groupInfo)
  1969. {
  1970. $groupIid = (int) $groupInfo['iid'];
  1971. $category = self::get_category_from_group($groupIid);
  1972. $number_groups_per_user = $groupInfo['maximum_number_of_students'];
  1973. $categoryId = 0;
  1974. if ($category) {
  1975. $groups_per_user = $category['groups_per_user'];
  1976. $number_groups_per_user = $groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user;
  1977. $categoryId = $category['id'];
  1978. }
  1979. $usersAdded = [];
  1980. foreach ($userList as &$userInfo) {
  1981. // find # of groups the user is enrolled in
  1982. $numberOfGroups = self::user_in_number_of_groups(
  1983. $userInfo['user_id'],
  1984. $categoryId
  1985. );
  1986. if (in_array($userInfo['user_id'], $usersAdded)) {
  1987. continue;
  1988. }
  1989. $usersAdded[] = $userInfo['user_id'];
  1990. // add # of groups to user list
  1991. $userInfo['number_groups_left'] = $number_groups_per_user - $numberOfGroups;
  1992. }
  1993. return $userList;
  1994. }
  1995. /**
  1996. * @param array $group_list
  1997. * @param int $category_id
  1998. *
  1999. * @return string
  2000. */
  2001. public static function process_groups($group_list, $category_id = 0)
  2002. {
  2003. global $charset;
  2004. $category_id = (int) $category_id;
  2005. $totalRegistered = 0;
  2006. $group_data = [];
  2007. $user_info = api_get_user_info();
  2008. $session_id = api_get_session_id();
  2009. $user_id = $user_info['user_id'];
  2010. $hideGroup = api_get_setting('hide_course_group_if_no_tools_available');
  2011. foreach ($group_list as $this_group) {
  2012. // Validation when belongs to a session
  2013. $session_img = api_get_session_image($this_group['session_id'], $user_info['status']);
  2014. // All the tutors of this group
  2015. $tutorsids_of_group = self::get_subscribed_tutors($this_group, true);
  2016. $isMember = self::is_subscribed($user_id, $this_group);
  2017. // Create a new table-row
  2018. $row = [];
  2019. // Checkbox
  2020. if (api_is_allowed_to_edit(false, true) && count($group_list) > 1) {
  2021. $row[] = $this_group['id'];
  2022. }
  2023. if (self::userHasAccessToBrowse($user_id, $this_group, $session_id)) {
  2024. // Group name
  2025. $groupNameClass = null;
  2026. if ($this_group['status'] == 0) {
  2027. $groupNameClass = 'muted';
  2028. }
  2029. $group_name = '<a class="'.$groupNameClass.'" href="group_space.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'">'.
  2030. Security::remove_XSS($this_group['name']).'</a> ';
  2031. if (!empty($user_id) && !empty($this_group['id_tutor']) && $user_id == $this_group['id_tutor']) {
  2032. $group_name .= Display::label(get_lang('OneMyGroups'), 'success');
  2033. } elseif ($isMember) {
  2034. $group_name .= Display::label(get_lang('MyGroup'), 'success');
  2035. }
  2036. if (api_is_allowed_to_edit() && !empty($this_group['session_name'])) {
  2037. $group_name .= ' ('.$this_group['session_name'].')';
  2038. }
  2039. $group_name .= $session_img;
  2040. $row[] = $group_name.'<br />'.stripslashes(trim($this_group['description']));
  2041. } else {
  2042. if ($hideGroup === 'true') {
  2043. continue;
  2044. }
  2045. $row[] = $this_group['name'].'<br />'.stripslashes(trim($this_group['description']));
  2046. }
  2047. // Tutor name
  2048. $tutor_info = '';
  2049. if (count($tutorsids_of_group) > 0) {
  2050. foreach ($tutorsids_of_group as $tutor_id) {
  2051. $tutor = api_get_user_info($tutor_id);
  2052. $username = api_htmlentities(
  2053. sprintf(get_lang('LoginX'), $tutor['username']),
  2054. ENT_QUOTES
  2055. );
  2056. if (api_get_setting('show_email_addresses') === 'true') {
  2057. $tutor_info .= Display::tag(
  2058. 'span',
  2059. Display::encrypted_mailto_link(
  2060. $tutor['mail'],
  2061. $tutor['complete_name']
  2062. ),
  2063. ['title' => $username]
  2064. ).', ';
  2065. } else {
  2066. if (api_is_allowed_to_edit()) {
  2067. $tutor_info .= Display::tag(
  2068. 'span',
  2069. Display::encrypted_mailto_link(
  2070. $tutor['mail'],
  2071. $tutor['complete_name_with_username']
  2072. ),
  2073. ['title' => $username]
  2074. ).', ';
  2075. } else {
  2076. $tutor_info .= Display::tag(
  2077. 'span',
  2078. $tutor['complete_name'],
  2079. ['title' => $username]
  2080. ).', ';
  2081. }
  2082. }
  2083. }
  2084. }
  2085. $tutor_info = api_substr(
  2086. $tutor_info,
  2087. 0,
  2088. api_strlen($tutor_info) - 2
  2089. );
  2090. $row[] = $tutor_info;
  2091. // Max number of members in group
  2092. $max_members = $this_group['maximum_number_of_members'] == self::MEMBER_PER_GROUP_NO_LIMIT ? ' ' : ' / '.$this_group['maximum_number_of_members'];
  2093. // Number of members in group
  2094. $row[] = $this_group['number_of_members'].$max_members;
  2095. // Self-registration / unregistration
  2096. if (!api_is_allowed_to_edit(false, true)) {
  2097. if (self::is_self_registration_allowed($user_id, $this_group)) {
  2098. $row[] = '<a class = "btn btn-default" href="group.php?'.api_get_cidreq().'&category='.$category_id.'&action=self_reg&group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."'".')) return false;">'.get_lang('GroupSelfRegInf').'</a>';
  2099. } elseif (self::is_self_unregistration_allowed($user_id, $this_group)) {
  2100. $row[] = '<a class = "btn btn-default" href="group.php?'.api_get_cidreq().'&category='.$category_id.'&action=self_unreg&group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."'".')) return false;">'.get_lang('GroupSelfUnRegInf').'</a>';
  2101. } else {
  2102. $row[] = '-';
  2103. }
  2104. }
  2105. $url = api_get_path(WEB_CODE_PATH).'group/';
  2106. // Edit-links
  2107. if (api_is_allowed_to_edit(false, true) &&
  2108. !(api_is_session_general_coach() && intval($this_group['session_id']) != $session_id)
  2109. ) {
  2110. $edit_actions = '<a href="'.$url.'settings.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'" title="'.get_lang('Edit').'">'.
  2111. Display::return_icon('edit.png', get_lang('EditGroup'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2112. if ($this_group['status'] == 1) {
  2113. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&action=set_invisible&id='.$this_group['id'].'" title="'.get_lang('Hide').'">'.
  2114. Display::return_icon('visible.png', get_lang('Hide'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2115. } else {
  2116. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&action=set_visible&id='.$this_group['id'].'" title="'.get_lang('Show').'">'.
  2117. Display::return_icon('invisible.png', get_lang('Show'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2118. }
  2119. $edit_actions .= '<a href="'.$url.'member_settings.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'" title="'.get_lang('GroupMembers').'">'.
  2120. Display::return_icon('user.png', get_lang('GroupMembers'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2121. $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').'">'.
  2122. Display::return_icon('export_excel.png', get_lang('Export'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2123. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&action=fill_one&id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('FillGroup').'">'.
  2124. Display::return_icon('fill.png', get_lang('FillGroup'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2125. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&action=delete_one&id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'">'.
  2126. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  2127. $row[] = $edit_actions;
  2128. }
  2129. if (!empty($this_group['nbMember'])) {
  2130. $totalRegistered = $totalRegistered + $this_group['nbMember'];
  2131. }
  2132. $group_data[] = $row;
  2133. } // end loop
  2134. // If no groups then don't show the table (only for students)
  2135. if (!api_is_allowed_to_edit(true, false)) {
  2136. if (empty($group_data)) {
  2137. return '';
  2138. }
  2139. }
  2140. $table = new SortableTableFromArrayConfig(
  2141. $group_data,
  2142. 1,
  2143. 20,
  2144. 'group_category_'.$category_id
  2145. );
  2146. $table->set_additional_parameters(['category' => $category_id]);
  2147. $column = 0;
  2148. if (api_is_allowed_to_edit(false, true) and count($group_list) > 1) {
  2149. $table->set_header($column++, '', false);
  2150. }
  2151. $table->set_header($column++, get_lang('Groups'));
  2152. $table->set_header($column++, get_lang('GroupTutor'));
  2153. $table->set_header($column++, get_lang('Registered'), false);
  2154. if (!api_is_allowed_to_edit(false, true)) {
  2155. // If self-registration allowed
  2156. $table->set_header($column++, get_lang('GroupSelfRegistration'), false);
  2157. }
  2158. if (api_is_allowed_to_edit(false, true)) {
  2159. // Only for course administrator
  2160. $table->set_header($column++, get_lang('Modify'), false);
  2161. $form_actions = [];
  2162. $form_actions['fill_selected'] = get_lang('FillGroup');
  2163. $form_actions['empty_selected'] = get_lang('EmptyGroup');
  2164. $form_actions['delete_selected'] = get_lang('Delete');
  2165. if (count($group_list) > 1) {
  2166. $table->set_form_actions($form_actions, 'group');
  2167. }
  2168. }
  2169. return $table->return_table();
  2170. }
  2171. /**
  2172. * @param array $groupData
  2173. * @param bool $deleteNotInArray
  2174. *
  2175. * @return array
  2176. */
  2177. public static function importCategoriesAndGroupsFromArray(
  2178. $groupData,
  2179. $deleteNotInArray = false
  2180. ) {
  2181. $result = [];
  2182. $elementsFound = [
  2183. 'categories' => [],
  2184. 'groups' => [],
  2185. ];
  2186. $courseCode = api_get_course_id();
  2187. $sessionId = api_get_session_id();
  2188. $groupCategories = self::get_categories();
  2189. if (empty($groupCategories)) {
  2190. $result['error'][] = get_lang('CreateACategory');
  2191. return $result;
  2192. }
  2193. foreach ($groupData as $data) {
  2194. $isCategory = empty($data['group']) ? true : false;
  2195. if ($isCategory) {
  2196. $categoryInfo = self::getCategoryByTitle($data['category']);
  2197. $categoryId = $categoryInfo['id'];
  2198. if (!empty($categoryInfo)) {
  2199. // Update
  2200. self::update_category(
  2201. $categoryId,
  2202. $data['category'],
  2203. $data['description'],
  2204. $data['doc_state'],
  2205. $data['work_state'],
  2206. $data['calendar_state'],
  2207. $data['announcements_state'],
  2208. $data['forum_state'],
  2209. $data['wiki_state'],
  2210. $data['chat_state'],
  2211. $data['self_reg_allowed'],
  2212. $data['self_unreg_allowed'],
  2213. $data['max_student'],
  2214. $data['groups_per_user']
  2215. );
  2216. $data['category_id'] = $categoryId;
  2217. $result['updated']['category'][] = $data;
  2218. } else {
  2219. // Add
  2220. $categoryId = self::create_category(
  2221. $data['category'],
  2222. $data['description'],
  2223. $data['doc_state'],
  2224. $data['work_state'],
  2225. $data['calendar_state'],
  2226. $data['announcements_state'],
  2227. $data['forum_state'],
  2228. $data['wiki_state'],
  2229. $data['chat_state'],
  2230. $data['self_reg_allowed'],
  2231. $data['self_unreg_allowed'],
  2232. $data['max_student'],
  2233. $data['groups_per_user']
  2234. );
  2235. if ($categoryId) {
  2236. $data['category_id'] = $categoryId;
  2237. $result['added']['category'][] = $data;
  2238. }
  2239. }
  2240. $elementsFound['categories'][] = $categoryId;
  2241. } else {
  2242. $groupInfo = self::getGroupByName($data['group']);
  2243. $categoryInfo = [];
  2244. if (isset($data['category'])) {
  2245. $categoryInfo = self::getCategoryByTitle($data['category']);
  2246. }
  2247. $categoryId = null;
  2248. if (!empty($categoryInfo)) {
  2249. $categoryId = $categoryInfo['id'];
  2250. } else {
  2251. if (!empty($groupCategories) && isset($groupCategories[0])) {
  2252. $defaultGroupCategory = $groupCategories[0];
  2253. $categoryId = $defaultGroupCategory['id'];
  2254. }
  2255. }
  2256. if (empty($groupInfo)) {
  2257. // Add
  2258. $groupId = self::create_group(
  2259. $data['group'],
  2260. $categoryId,
  2261. null,
  2262. $data['max_student']
  2263. );
  2264. if ($groupId) {
  2265. self::set_group_properties(
  2266. $groupId,
  2267. $data['group'],
  2268. $data['description'],
  2269. $data['max_student'],
  2270. $data['doc_state'],
  2271. $data['work_state'],
  2272. $data['calendar_state'],
  2273. $data['announcements_state'],
  2274. $data['forum_state'],
  2275. $data['wiki_state'],
  2276. $data['chat_state'],
  2277. $data['self_reg_allowed'],
  2278. $data['self_unreg_allowed'],
  2279. $categoryId
  2280. );
  2281. $data['group_id'] = $groupId;
  2282. $result['added']['group'][] = $data;
  2283. }
  2284. $groupInfo = self::get_group_properties($groupId, true);
  2285. } else {
  2286. // Update
  2287. $groupId = $groupInfo['id'];
  2288. self::set_group_properties(
  2289. $groupId,
  2290. $data['group'],
  2291. $data['description'],
  2292. $data['max_student'],
  2293. $data['doc_state'],
  2294. $data['work_state'],
  2295. $data['calendar_state'],
  2296. $data['announcements_state'],
  2297. $data['forum_state'],
  2298. $data['wiki_state'],
  2299. $data['chat_state'],
  2300. $data['self_reg_allowed'],
  2301. $data['self_unreg_allowed'],
  2302. $categoryId
  2303. );
  2304. $data['group_id'] = $groupId;
  2305. $result['updated']['group'][] = $data;
  2306. $groupInfo = self::get_group_properties($groupId);
  2307. }
  2308. $students = isset($data['students']) ? explode(',', $data['students']) : [];
  2309. if (!empty($students)) {
  2310. $studentUserIdList = [];
  2311. foreach ($students as $student) {
  2312. $userInfo = api_get_user_info_from_username($student);
  2313. if (!$userInfo) {
  2314. continue;
  2315. }
  2316. if (!CourseManager::is_user_subscribed_in_course(
  2317. $userInfo['user_id'],
  2318. $courseCode,
  2319. !empty($sessionId),
  2320. $sessionId
  2321. )
  2322. ) {
  2323. Display::addFlash(
  2324. Display::return_message(
  2325. sprintf(
  2326. get_lang('StudentXIsNotSubscribedToCourse'),
  2327. $userInfo['complete_name']
  2328. ),
  2329. 'warning'
  2330. )
  2331. );
  2332. continue;
  2333. }
  2334. $studentUserIdList[] = $userInfo['user_id'];
  2335. }
  2336. self::subscribe_users($studentUserIdList, $groupInfo);
  2337. }
  2338. $tutors = isset($data['tutors']) ? explode(',', $data['tutors']) : [];
  2339. if (!empty($tutors)) {
  2340. $tutorIdList = [];
  2341. foreach ($tutors as $tutor) {
  2342. $userInfo = api_get_user_info_from_username($tutor);
  2343. if (!$userInfo) {
  2344. continue;
  2345. }
  2346. if (!CourseManager::is_user_subscribed_in_course(
  2347. $userInfo['user_id'],
  2348. $courseCode,
  2349. !empty($sessionId),
  2350. $sessionId
  2351. )
  2352. ) {
  2353. Display::addFlash(
  2354. Display::return_message(
  2355. sprintf(get_lang('TutorXIsNotSubscribedToCourse'), $userInfo['complete_name']),
  2356. 'warning'
  2357. )
  2358. );
  2359. continue;
  2360. }
  2361. $tutorIdList[] = $userInfo['user_id'];
  2362. }
  2363. self::subscribe_tutors($tutorIdList, $groupInfo);
  2364. }
  2365. $elementsFound['groups'][] = $groupId;
  2366. }
  2367. }
  2368. if ($deleteNotInArray) {
  2369. // Check categories
  2370. $categories = self::get_categories();
  2371. foreach ($categories as $category) {
  2372. if (!in_array($category['id'], $elementsFound['categories'])) {
  2373. self::delete_category($category['id']);
  2374. $category['category'] = $category['title'];
  2375. $result['deleted']['category'][] = $category;
  2376. }
  2377. }
  2378. $groups = self::get_groups();
  2379. foreach ($groups as $group) {
  2380. if (!in_array($group['iid'], $elementsFound['groups'])) {
  2381. self::delete_groups($group);
  2382. $group['group'] = $group['name'];
  2383. $result['deleted']['group'][] = $group;
  2384. }
  2385. }
  2386. }
  2387. return $result;
  2388. }
  2389. /**
  2390. * Export all categories/group from a course to an array.
  2391. * This function works only in a context of a course.
  2392. *
  2393. * @param int $groupId
  2394. * @param bool $loadUsers
  2395. *
  2396. * @return array
  2397. */
  2398. public static function exportCategoriesAndGroupsToArray($groupId = null, $loadUsers = false)
  2399. {
  2400. $data = [];
  2401. $data[] = [
  2402. 'category',
  2403. 'group',
  2404. 'description',
  2405. 'announcements_state',
  2406. 'calendar_state',
  2407. 'chat_state',
  2408. 'doc_state',
  2409. 'forum_state',
  2410. 'work_state',
  2411. 'wiki_state',
  2412. 'max_student',
  2413. 'self_reg_allowed',
  2414. 'self_unreg_allowed',
  2415. 'groups_per_user',
  2416. ];
  2417. $count = 1;
  2418. if ($loadUsers) {
  2419. $data[0][] = 'students';
  2420. $data[0][] = 'tutors';
  2421. }
  2422. if ($loadUsers == false) {
  2423. $categories = self::get_categories();
  2424. foreach ($categories as $categoryInfo) {
  2425. $data[$count] = [
  2426. $categoryInfo['title'],
  2427. null,
  2428. $categoryInfo['description'],
  2429. $categoryInfo['announcements_state'],
  2430. $categoryInfo['calendar_state'],
  2431. $categoryInfo['chat_state'],
  2432. $categoryInfo['doc_state'],
  2433. $categoryInfo['forum_state'],
  2434. $categoryInfo['work_state'],
  2435. $categoryInfo['wiki_state'],
  2436. $categoryInfo['max_student'],
  2437. $categoryInfo['self_reg_allowed'],
  2438. $categoryInfo['self_unreg_allowed'],
  2439. $categoryInfo['groups_per_user'],
  2440. ];
  2441. $count++;
  2442. }
  2443. }
  2444. $groups = self::get_group_list();
  2445. foreach ($groups as $groupInfo) {
  2446. $categoryTitle = null;
  2447. $categoryInfo = self::get_category($groupInfo['category_id']);
  2448. $groupSettings = self::get_group_properties($groupInfo['id']);
  2449. if (!empty($categoryInfo)) {
  2450. $categoryTitle = $categoryInfo['title'];
  2451. }
  2452. $users = self::getStudents($groupInfo['iid']);
  2453. $userList = [];
  2454. foreach ($users as $user) {
  2455. $user = api_get_user_info($user['user_id']);
  2456. $userList[] = $user['username'];
  2457. }
  2458. $tutors = self::getTutors($groupInfo);
  2459. $tutorList = [];
  2460. foreach ($tutors as $user) {
  2461. $user = api_get_user_info($user['user_id']);
  2462. $tutorList[] = $user['username'];
  2463. }
  2464. $userListToString = null;
  2465. if (!empty($userList)) {
  2466. $userListToString = implode(',', $userList);
  2467. }
  2468. $tutorListToString = null;
  2469. if (!empty($tutorList)) {
  2470. $tutorListToString = implode(',', $tutorList);
  2471. }
  2472. $data[$count] = [
  2473. $categoryTitle,
  2474. $groupSettings['name'],
  2475. $groupSettings['description'],
  2476. $groupSettings['announcements_state'],
  2477. $groupSettings['calendar_state'],
  2478. $groupSettings['chat_state'],
  2479. $groupSettings['doc_state'],
  2480. $groupSettings['forum_state'],
  2481. $groupSettings['work_state'],
  2482. $groupSettings['wiki_state'],
  2483. $groupSettings['maximum_number_of_students'],
  2484. $groupSettings['self_registration_allowed'],
  2485. $groupSettings['self_unregistration_allowed'],
  2486. null,
  2487. ];
  2488. if ($loadUsers) {
  2489. $data[$count][] = $userListToString;
  2490. $data[$count][] = $tutorListToString;
  2491. }
  2492. if (!empty($groupId)) {
  2493. if ($groupId == $groupInfo['id']) {
  2494. break;
  2495. }
  2496. }
  2497. $count++;
  2498. }
  2499. return $data;
  2500. }
  2501. /**
  2502. * @param string $default
  2503. */
  2504. public static function getSettingBar($default)
  2505. {
  2506. $activeSettings = null;
  2507. $activeTutor = null;
  2508. $activeMember = null;
  2509. switch ($default) {
  2510. case 'settings':
  2511. $activeSettings = 'active';
  2512. break;
  2513. case'tutor':
  2514. $activeTutor = 'active';
  2515. break;
  2516. case 'member':
  2517. $activeMember = 'active';
  2518. break;
  2519. }
  2520. $url = api_get_path(WEB_CODE_PATH).'group/%s?'.api_get_cidreq();
  2521. echo '
  2522. <ul class="toolbar-groups nav nav-tabs">
  2523. <li class="'.$activeSettings.'">
  2524. <a href="'.sprintf($url, 'settings.php').'">
  2525. '.Display::return_icon('settings.png').' '.get_lang('Settings').'
  2526. </a>
  2527. </li>
  2528. <li class="'.$activeMember.'">
  2529. <a href="'.sprintf($url, 'member_settings.php').'">
  2530. '.Display::return_icon('user.png').' '.get_lang('GroupMembers').'
  2531. </a>
  2532. </li>
  2533. <li class="'.$activeTutor.'">
  2534. <a href="'.sprintf($url, 'tutor_settings.php').'">
  2535. '.Display::return_icon('teacher.png').' '.get_lang('GroupTutors').'
  2536. </a>
  2537. </li>
  2538. </ul>';
  2539. }
  2540. /**
  2541. * @param int $courseId
  2542. * @param string $keyword
  2543. *
  2544. * @return string
  2545. */
  2546. public static function getOverview($courseId, $keyword = null)
  2547. {
  2548. $content = null;
  2549. $categories = self::get_categories();
  2550. if (!empty($categories)) {
  2551. foreach ($categories as $category) {
  2552. if (api_get_setting('allow_group_categories') == 'true') {
  2553. $content .= '<h2>'.$category['title'].'</h2>';
  2554. }
  2555. if (!empty($keyword)) {
  2556. $groups = self::getGroupListFilterByName(
  2557. $keyword,
  2558. $category['id'],
  2559. $courseId
  2560. );
  2561. } else {
  2562. $groups = self::get_group_list($category['id']);
  2563. }
  2564. if (empty($groups)) {
  2565. $groups = self::get_group_list();
  2566. }
  2567. $content .= '<ul>';
  2568. if (!empty($groups)) {
  2569. foreach ($groups as $group) {
  2570. $content .= '<li>';
  2571. $content .= Display::tag(
  2572. 'h3',
  2573. Security::remove_XSS($group['name'])
  2574. );
  2575. $users = self::getTutors($group);
  2576. if (!empty($users)) {
  2577. $content .= '<ul>';
  2578. $content .= "<li>".Display::tag('h4', get_lang('Tutors'))."</li><ul>";
  2579. foreach ($users as $user) {
  2580. $user_info = api_get_user_info($user['user_id']);
  2581. $content .= '<li title="'.$user_info['username'].'">'.
  2582. $user_info['complete_name_with_username'].
  2583. '</li>';
  2584. }
  2585. $content .= '</ul>';
  2586. $content .= '</ul>';
  2587. }
  2588. $users = self::getStudents($group['id']);
  2589. if (!empty($users)) {
  2590. $content .= '<ul>';
  2591. $content .= "<li>".Display::tag('h4', get_lang('Students'))."</li><ul>";
  2592. foreach ($users as $user) {
  2593. $user_info = api_get_user_info($user['user_id']);
  2594. $content .= '<li title="'.$user_info['username'].'">'.
  2595. $user_info['complete_name_with_username'].
  2596. '</li>';
  2597. }
  2598. $content .= '</ul>';
  2599. $content .= '</ul>';
  2600. }
  2601. $content .= '</li>';
  2602. }
  2603. }
  2604. $content .= '</ul>';
  2605. }
  2606. }
  2607. return $content;
  2608. }
  2609. /**
  2610. * Returns the search form.
  2611. *
  2612. * @return string
  2613. */
  2614. public static function getSearchForm()
  2615. {
  2616. $url = api_get_path(WEB_CODE_PATH).'group/group_overview.php?'.api_get_cidreq();
  2617. $form = new FormValidator(
  2618. 'search_groups',
  2619. 'get',
  2620. $url,
  2621. null,
  2622. ['class' => 'form-search'],
  2623. FormValidator::LAYOUT_INLINE
  2624. );
  2625. $form->addElement('text', 'keyword');
  2626. $form->addButtonSearch();
  2627. return $form->toHtml();
  2628. }
  2629. /**
  2630. * @param int $groupId
  2631. * @param int $status
  2632. */
  2633. public static function setStatus($groupId, $status)
  2634. {
  2635. $groupInfo = self::get_group_properties($groupId);
  2636. $courseId = api_get_course_int_id();
  2637. if (!empty($groupInfo)) {
  2638. $table = Database::get_course_table(TABLE_GROUP);
  2639. $params = [
  2640. 'status' => intval($status),
  2641. ];
  2642. Database::update(
  2643. $table,
  2644. $params,
  2645. ['c_id = ? AND id = ?' => [$courseId, $groupId]]
  2646. );
  2647. }
  2648. }
  2649. /**
  2650. * @param int $groupId
  2651. */
  2652. public static function setVisible($groupId)
  2653. {
  2654. self::setStatus($groupId, 1);
  2655. }
  2656. /**
  2657. * @param int $groupId
  2658. */
  2659. public static function setInvisible($groupId)
  2660. {
  2661. self::setStatus($groupId, 0);
  2662. }
  2663. /**
  2664. * @param int $userId
  2665. * @param int $courseId
  2666. * @param array $groupInfo
  2667. * @param bool $blockPage
  2668. */
  2669. public static function allowUploadEditDocument($userId, $courseId, $groupInfo, $blockPage = false)
  2670. {
  2671. $allow = api_get_configuration_value('group_document_access');
  2672. if (!$allow) {
  2673. return true;
  2674. }
  2675. if (isset($groupInfo['document_access']) &&
  2676. $groupInfo['document_access'] == 1 &&
  2677. (!api_is_allowed_to_edit() ||
  2678. (!api_is_allowed_to_edit() && !GroupManager::is_tutor_of_group($userId, $groupInfo, $courseId))
  2679. )
  2680. ) {
  2681. if ($blockPage) {
  2682. api_not_allowed(true);
  2683. }
  2684. return false;
  2685. }
  2686. return true;
  2687. }
  2688. }