groupmanager.lib.php 111 KB

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