groupmanager.lib.php 102 KB

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