groupmanager.lib.php 101 KB

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