groupmanager.lib.php 105 KB

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