groupmanager.lib.php 111 KB

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