groupmanager.lib.php 101 KB

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