groupmanager.lib.php 107 KB

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