groupmanager.lib.php 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * @package chamilo.library
  6. */
  7. /**
  8. * Code
  9. */
  10. /**
  11. * This library contains some functions for group-management.
  12. * @author Bart Mollet
  13. * @package chamilo.library
  14. * @todo Add $course_code parameter to all functions. So this GroupManager can
  15. * be used outside a session.
  16. */
  17. class GroupManager
  18. {
  19. //- VIRTUAL_COURSE_CATEGORY: in this category groups are created based on the virtual course of a course
  20. CONST VIRTUAL_COURSE_CATEGORY = 1;
  21. //DEFAULT_GROUP_CATEGORY: When group categories aren't available (platform-setting), all groups are created in this 'dummy'-category
  22. CONST DEFAULT_GROUP_CATEGORY = 2;
  23. /**
  24. * infinite
  25. */
  26. CONST INFINITE = 99999;
  27. /**
  28. * No limit on the number of users in a group
  29. */
  30. CONST MEMBER_PER_GROUP_NO_LIMIT = 0;
  31. /**
  32. * No limit on the number of groups per user
  33. */
  34. CONST GROUP_PER_MEMBER_NO_LIMIT = 0;
  35. /**
  36. * The tools of a group can have 3 states
  37. * - not available
  38. * - public
  39. * - private
  40. */
  41. CONST TOOL_NOT_AVAILABLE = 0;
  42. CONST TOOL_PUBLIC = 1;
  43. CONST TOOL_PRIVATE = 2;
  44. /**
  45. * Constants for the available group tools
  46. */
  47. CONST GROUP_TOOL_FORUM = 0;
  48. CONST GROUP_TOOL_DOCUMENTS = 1;
  49. CONST GROUP_TOOL_CALENDAR = 2;
  50. CONST GROUP_TOOL_ANNOUNCEMENT = 3;
  51. CONST GROUP_TOOL_WORK = 4;
  52. CONST GROUP_TOOL_WIKI = 5;
  53. CONST GROUP_TOOL_CHAT = 6;
  54. //GROUP FUNCTIONS
  55. private function __construct()
  56. {
  57. }
  58. public static function get_groups()
  59. {
  60. $table_group = Database :: get_course_table(TABLE_GROUP);
  61. $course_id = api_get_course_int_id();
  62. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id ";
  63. $result = Database::query($sql);
  64. return Database::store_result($result, 'ASSOC');
  65. }
  66. /**
  67. * Get list of groups for current course.
  68. * @param int $category The id of the category from which the groups are
  69. * requested
  70. * @param string $course_code Default is current course
  71. * @return array An array with all information about the groups.
  72. */
  73. public static function get_group_list($category = null, $course_code = null)
  74. {
  75. $my_user_id = api_get_user_id();
  76. $course_info = api_get_course_info($course_code);
  77. $course_id = $course_info['real_id'];
  78. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  79. $table_group = Database :: get_course_table(TABLE_GROUP);
  80. //condition for the session
  81. $session_id = api_get_session_id();
  82. $my_status_of_user_in_course = CourseManager::get_user_in_course_status($my_user_id, $course_info['code']);
  83. $is_student_in_session = false;
  84. if (is_null($my_status_of_user_in_course) || $my_status_of_user_in_course == '') { //into session
  85. if ($session_id > 0) {
  86. $is_student_in_session = true;
  87. }
  88. }
  89. //COURSEMANAGER or STUDENT
  90. if ($my_status_of_user_in_course == COURSEMANAGER || api_is_allowed_to_edit(null, true) || api_is_drh()) {
  91. $sql = "SELECT g.id ,
  92. g.name ,
  93. g.description ,
  94. g.category_id,
  95. g.max_student maximum_number_of_members,
  96. g.secret_directory,
  97. g.self_registration_allowed,
  98. g.self_unregistration_allowed,
  99. g.session_id,
  100. ug.user_id is_member
  101. FROM $table_group g
  102. LEFT JOIN $table_group_user ug
  103. ON (ug.group_id = g.id AND ug.user_id = '".api_get_user_id(
  104. )."' AND ug.c_id = $course_id AND g.c_id = $course_id)";
  105. } elseif ($my_status_of_user_in_course == STUDENT || $is_student_in_session === true || $_SESSION['studentview'] == 'studentview') {
  106. $sql = "SELECT g.id,
  107. g.name,
  108. g.description,
  109. g.category_id,
  110. g.max_student maximum_number_of_members,
  111. g.secret_directory,
  112. g.self_registration_allowed,
  113. g.self_unregistration_allowed,
  114. g.session_id,
  115. ug.user_id is_member
  116. FROM $table_group g
  117. LEFT JOIN $table_group_user ug
  118. ON (ug.group_id = g.id AND ug.user_id = '".api_get_user_id(
  119. )."' AND ug.c_id = $course_id AND g.c_id = $course_id)";
  120. }
  121. $sql .= " WHERE 1=1 ";
  122. if ($category != null) {
  123. $sql .= " AND g.category_id = '".Database::escape_string($category)."' ";
  124. $session_condition = api_get_session_condition($session_id);
  125. if (!empty($session_condition)) {
  126. $sql .= $session_condition;
  127. }
  128. } else {
  129. $session_condition = api_get_session_condition($session_id, true);
  130. }
  131. $sql .= " AND g.c_id = $course_id ";
  132. if (!empty($session_condition)) {
  133. $sql .= $session_condition;
  134. }
  135. $sql .= " GROUP BY g.id ORDER BY UPPER(g.name)";
  136. if (!api_is_anonymous()) {
  137. $groupList = Database::query($sql);
  138. } else {
  139. return array();
  140. }
  141. $groups = array();
  142. $thisGroup = array();
  143. while ($thisGroup = Database::fetch_array($groupList)) {
  144. $thisGroup['number_of_members'] = count(self::get_subscribed_users($thisGroup['id']));
  145. if ($thisGroup['session_id'] != 0) {
  146. $sql_session = 'SELECT name FROM '.Database::get_main_table(
  147. TABLE_MAIN_SESSION
  148. ).' WHERE id='.$thisGroup['session_id'];
  149. $rs_session = Database::query($sql_session);
  150. if (Database::num_rows($rs_session) > 0) {
  151. $thisGroup['session_name'] = Database::result($rs_session, 0, 0);
  152. } else {
  153. //the session has probably been removed, so the group is now orphaned
  154. }
  155. }
  156. $groups[] = $thisGroup;
  157. }
  158. return $groups;
  159. }
  160. /**
  161. * Create a group
  162. * @param string $name The name for this group
  163. * @param int $tutor The user-id of the group's tutor
  164. * @param int $places How many people can subscribe to the new group
  165. */
  166. public static function create_group($name, $category_id, $tutor, $places)
  167. {
  168. global $_course;
  169. $table_group = Database :: get_course_table(TABLE_GROUP);
  170. $session_id = api_get_session_id();
  171. $course_id = api_get_course_int_id();
  172. $currentCourseRepository = $_course['path'];
  173. $category = self :: get_category($category_id);
  174. if (intval($places) == 0) {
  175. //if the amount of users per group is not filled in, use the setting from the category
  176. $places = $category['max_student'];
  177. } else {
  178. if ($places > $category['max_student'] && $category['max_student'] != 0) {
  179. $places = $category['max_student'];
  180. }
  181. }
  182. $sql = "INSERT INTO ".$table_group." SET
  183. c_id = $course_id ,
  184. category_id='".Database::escape_string($category_id)."',
  185. max_student = '".$places."',
  186. doc_state = '".$category['doc_state']."',
  187. calendar_state = '".$category['calendar_state']."',
  188. work_state = '".$category['work_state']."',
  189. announcements_state = '".$category['announcements_state']."',
  190. forum_state = '".$category['forum_state']."',
  191. wiki_state = '".$category['wiki_state']."',
  192. chat_state = '".$category['chat_state']."',
  193. self_registration_allowed = '".$category['self_reg_allowed']."',
  194. self_unregistration_allowed = '".$category['self_unreg_allowed']."',
  195. session_id='".Database::escape_string($session_id)."'";
  196. Database::query($sql);
  197. $lastId = Database::insert_id();
  198. //Fixing Id
  199. Database::update($table_group, array('id' => $lastId), array('iid = ? ' => $lastId));
  200. if ($lastId) {
  201. $desired_dir_name = '/'.replace_dangerous_char($name, 'strict').'_groupdocs';
  202. $my_path = api_get_path(SYS_COURSE_PATH).$currentCourseRepository.'/document';
  203. $unique_name = FileManager::create_unexisting_directory(
  204. $_course,
  205. api_get_user_id(),
  206. $session_id,
  207. $lastId,
  208. null,
  209. $my_path,
  210. $desired_dir_name
  211. );
  212. /* Stores the directory path into the group table */
  213. $sql = "UPDATE ".$table_group." SET name = '".Database::escape_string(
  214. $name
  215. )."', secret_directory = '".$unique_name."'
  216. WHERE c_id = $course_id AND id ='".$lastId."'";
  217. Database::query($sql);
  218. // create a forum if needed
  219. if ($category['forum_state'] >= 0) {
  220. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  221. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  222. $forum_categories = get_forum_categories();
  223. $values = array();
  224. $values['forum_title'] = $name;
  225. $values['group_id'] = $lastId;
  226. $counter = 0;
  227. foreach ($forum_categories as $key => $value) {
  228. if ($counter == 0) {
  229. $forum_category_id = $key;
  230. }
  231. $counter++;
  232. }
  233. // A sanity check.
  234. if (empty($forum_category_id)) {
  235. $forum_category_id = 0;
  236. }
  237. $values['forum_category'] = $forum_category_id;
  238. $values['allow_anonymous_group']['allow_anonymous'] = 0;
  239. $values['students_can_edit_group']['students_can_edit'] = 0;
  240. $values['approval_direct_group']['approval_direct'] = 0;
  241. $values['allow_attachments_group']['allow_attachments'] = 1;
  242. $values['allow_new_threads_group']['allow_new_threads'] = 1;
  243. $values['default_view_type_group']['default_view_type'] = api_get_setting('default_forum_view');
  244. $values['group_forum'] = $lastId;
  245. if ($category['forum_state'] == '1') {
  246. $values['public_private_group_forum_group']['public_private_group_forum'] = 'public';
  247. } elseif ($category['forum_state'] == '2') {
  248. $values['public_private_group_forum_group']['public_private_group_forum'] = 'private';
  249. } elseif ($category['forum_state'] == '0') {
  250. $values['public_private_group_forum_group']['public_private_group_forum'] = 'unavailable';
  251. }
  252. store_forum($values);
  253. }
  254. }
  255. return $lastId;
  256. }
  257. /**
  258. * Create subgroups.
  259. * This function creates new groups based on an existing group. It will
  260. * create the specified number of groups and fill those groups with users
  261. * from the base group
  262. * @param int $group_id The group from which subgroups have to be created.
  263. * @param int $number_of_groups The number of groups that have to be created
  264. */
  265. public static function create_subgroups($group_id, $number_of_groups)
  266. {
  267. $course_id = api_get_course_int_id();
  268. $table_group = Database :: get_course_table(TABLE_GROUP);
  269. $category_id = self :: create_category('Subgroups', '', self::TOOL_PRIVATE, self::TOOL_PRIVATE, 0, 0, 1, 1);
  270. $users = self :: get_users($group_id);
  271. $group_ids = array();
  272. for ($group_nr = 1; $group_nr <= $number_of_groups; $group_nr++) {
  273. $group_ids[] = self :: create_group('SUBGROUP '.$group_nr, $category_id, 0, 0);
  274. }
  275. $members = array();
  276. foreach ($users as $index => $user_id) {
  277. self :: subscribe_users($user_id, $group_ids[$index % $number_of_groups]);
  278. $members[$group_ids[$index % $number_of_groups]]++;
  279. }
  280. foreach ($members as $group_id => $places) {
  281. $sql = "UPDATE $table_group SET max_student = $places WHERE c_id = $course_id AND id = $group_id";
  282. Database::query($sql);
  283. }
  284. }
  285. /**
  286. * Create a group for every class subscribed to the current course
  287. * @param int $category_id The category in which the groups should be
  288. * created
  289. */
  290. public static function create_class_groups($category_id)
  291. {
  292. global $_course;
  293. $options['where'] = array(" usergroup.course_id = ? " => api_get_real_course_id());
  294. $obj = new UserGroup();
  295. $classes = $obj->get_usergroup_in_course($options);
  296. $group_ids = array();
  297. foreach ($classes as $index => $class) {
  298. $users_ids = $obj->get_users_by_usergroup($class['id']);
  299. $group_id = self::create_group($class['name'], $category_id, 0, count($users_ids));
  300. self::subscribe_users($users_ids, $group_id);
  301. $group_ids[] = $group_id;
  302. }
  303. return $group_ids;
  304. }
  305. /**
  306. * deletes groups and their data.
  307. * @author Christophe Gesche <christophe.gesche@claroline.net>
  308. * @author Hugues Peeters <hugues.peeters@claroline.net>
  309. * @author Bart Mollet
  310. * @param mixed $groupIdList - group(s) to delete. It can be a single id
  311. * (int) or a list of id (array).
  312. * @param string $course_code Default is current course
  313. * @return integer - number of groups deleted.
  314. */
  315. public static function delete_groups($group_ids, $course_code = null)
  316. {
  317. $course_info = api_get_course_info($course_code);
  318. $course_id = $course_info['real_id'];
  319. // Database table definitions
  320. $group_table = Database :: get_course_table(TABLE_GROUP);
  321. $forum_table = Database :: get_course_table(TABLE_FORUM);
  322. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  323. $group_ids = array_map('intval', $group_ids);
  324. if (api_is_course_coach()) {
  325. //a coach can only delete courses from his session
  326. for ($i = 0; $i < count($group_ids); $i++) {
  327. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  328. array_splice($group_ids, $i, 1);
  329. $i--;
  330. }
  331. }
  332. if (count($group_ids) == 0) {
  333. return 0;
  334. }
  335. }
  336. // Unsubscribe all users
  337. self :: unsubscribe_all_users($group_ids);
  338. $sql = "SELECT id, secret_directory, session_id FROM $group_table WHERE c_id = $course_id AND id IN (".implode(
  339. ' , ',
  340. $group_ids
  341. ).")";
  342. $db_result = Database::query($sql);
  343. $forum_ids = array();
  344. while ($group = Database::fetch_object($db_result)) {
  345. // move group-documents to garbage
  346. //$source_directory = api_get_path(SYS_COURSE_PATH).$course['path']."/group/".$group->secret_directory;
  347. $source_directory = api_get_path(SYS_COURSE_PATH).$course['path']."/document".$group->secret_directory;
  348. //File to renamed
  349. $destination_dir = api_get_path(
  350. SYS_COURSE_PATH
  351. ).$course['path']."/document".$group->secret_directory.'_DELETED_'.$group->id;
  352. if (!empty($group->secret_directory)) {
  353. //Deleting from document tool
  354. DocumentManager::delete_document($course, $group->secret_directory, $source_directory);
  355. if (file_exists($source_directory)) {
  356. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  357. //Delete
  358. FileManager::my_delete($source_directory);
  359. } else {
  360. //Rename
  361. rename($source_directory, $destination_dir);
  362. }
  363. }
  364. }
  365. //$forum_ids[] = $group->forum_id;
  366. }
  367. // delete the groups
  368. $sql = "DELETE FROM ".$group_table." WHERE c_id = $course_id AND id IN ('".implode("' , '", $group_ids)."')";
  369. Database::query($sql);
  370. $sql2 = "DELETE FROM ".$forum_table." WHERE c_id = $course_id AND forum_of_group IN ('".implode(
  371. "' , '",
  372. $group_ids
  373. )."')";
  374. Database::query($sql2);
  375. return Database::affected_rows();
  376. }
  377. /**
  378. * Get group properties
  379. * @param int $group_id The group from which properties are requested.
  380. * @return array All properties. Array-keys are name, tutor_id, description, maximum_number_of_students, directory and visibility of tools
  381. */
  382. public static function get_group_properties($group_id)
  383. {
  384. $course_id = api_get_course_int_id();
  385. if (empty($group_id) or !is_integer(intval($group_id))) {
  386. return null;
  387. }
  388. $result = array();
  389. $table_group = Database :: get_course_table(TABLE_GROUP);
  390. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id AND id = ".intval($group_id);
  391. $db_result = Database::query($sql);
  392. $db_object = Database::fetch_object($db_result);
  393. $result['id'] = $db_object->id;
  394. $result['name'] = $db_object->name;
  395. $result['tutor_id'] = isset($db_object->tutor_id) ? $db_object->tutor_id : null;
  396. $result['description'] = $db_object->description;
  397. $result['maximum_number_of_students'] = $db_object->max_student;
  398. $result['doc_state'] = $db_object->doc_state;
  399. $result['work_state'] = $db_object->work_state;
  400. $result['calendar_state'] = $db_object->calendar_state;
  401. $result['announcements_state'] = $db_object->announcements_state;
  402. $result['forum_state'] = $db_object->forum_state;
  403. $result['wiki_state'] = $db_object->wiki_state;
  404. $result['chat_state'] = $db_object->chat_state;
  405. $result['directory'] = $db_object->secret_directory;
  406. $result['self_registration_allowed'] = $db_object->self_registration_allowed;
  407. $result['self_unregistration_allowed'] = $db_object->self_unregistration_allowed;
  408. $result['count_users'] = count(self::get_subscribed_users($group_id));
  409. $result['count_tutor'] = count(self::get_subscribed_tutors($group_id));
  410. $result['count_all'] = $result['count_users'] + $result['count_tutor'];
  411. return $result;
  412. }
  413. /**
  414. * Set group properties
  415. * Changes the group's properties.
  416. * @param int Group Id
  417. * @param string Group name
  418. * @param string Group description
  419. * @param int Max number of students in group
  420. * @param int Document tool's visibility (0=none,1=private,2=public)
  421. * @param int Work tool's visibility (0=none,1=private,2=public)
  422. * @param int Calendar tool's visibility (0=none,1=private,2=public)
  423. * @param int Announcement tool's visibility (0=none,1=private,2=public)
  424. * @param int Forum tool's visibility (0=none,1=private,2=public)
  425. * @param int Wiki tool's visibility (0=none,1=private,2=public)
  426. * @param int Chat tool's visibility (0=none,1=private,2=public)
  427. * @param bool Whether self registration is allowed or not
  428. * @param bool Whether self unregistration is allowed or not
  429. * @return bool TRUE if properties are successfully changed, false otherwise
  430. */
  431. public static function set_group_properties(
  432. $group_id,
  433. $name,
  434. $description,
  435. $maximum_number_of_students,
  436. $doc_state,
  437. $work_state,
  438. $calendar_state,
  439. $announcements_state,
  440. $forum_state,
  441. $wiki_state,
  442. $chat_state,
  443. $self_registration_allowed,
  444. $self_unregistration_allowed
  445. ) {
  446. $table_group = Database :: get_course_table(TABLE_GROUP);
  447. $table_forum = Database :: get_course_table(TABLE_FORUM);
  448. //$forum_id = get_forums_of_group($group_id);
  449. $group_id = Database::escape_string($group_id);
  450. $category = self::get_category_from_group($group_id);
  451. if ($maximum_number_of_students > $category['max_student'] && $category['max_student'] != 0) {
  452. $maximum_number_of_students = $category['max_student'];
  453. }
  454. $course_id = api_get_course_int_id();
  455. $sql = "UPDATE ".$table_group."
  456. SET name='".Database::escape_string(trim($name))."',
  457. doc_state = '".Database::escape_string($doc_state)."',
  458. work_state = '".Database::escape_string($work_state)."',
  459. calendar_state = '".Database::escape_string($calendar_state)."',
  460. announcements_state = '".Database::escape_string($announcements_state)."',
  461. forum_state = '".Database::escape_string($forum_state)."',
  462. wiki_state = '".Database::escape_string($wiki_state)."',
  463. chat_state = '".Database::escape_string($chat_state)."',
  464. description='".Database::escape_string(trim($description))."',
  465. max_student=".Database::escape_string($maximum_number_of_students).",
  466. self_registration_allowed='".Database::escape_string($self_registration_allowed)."',
  467. self_unregistration_allowed='".Database::escape_string($self_unregistration_allowed)."'
  468. WHERE c_id = $course_id AND id=".$group_id;
  469. $result = Database::query($sql);
  470. //Here we are updating a field in the table forum_forum that perhaps duplicates the table group_info.forum_state cvargas
  471. $forum_state = (int)$forum_state;
  472. $sql2 = "UPDATE ".$table_forum." SET ";
  473. if ($forum_state === 1) {
  474. $sql2 .= " forum_group_public_private='public' ";
  475. } elseif ($forum_state === 2) {
  476. $sql2 .= " forum_group_public_private='private' ";
  477. } elseif ($forum_state === 0) {
  478. $sql2 .= " forum_group_public_private='unavailable' ";
  479. }
  480. $sql2 .= " WHERE c_id = $course_id AND forum_of_group=".$group_id;
  481. $result2 = Database::query($sql2);
  482. return $result;
  483. }
  484. /**
  485. * Get the total number of groups for the current course.
  486. * @return int The number of groups for the current course.
  487. */
  488. public static function get_number_of_groups()
  489. {
  490. $course_id = api_get_course_int_id();
  491. $table_group = Database :: get_course_table(TABLE_GROUP);
  492. $res = Database::query("SELECT COUNT(id) AS number_of_groups FROM $table_group WHERE c_id = $course_id ");
  493. $obj = Database::fetch_object($res);
  494. return $obj->number_of_groups;
  495. }
  496. //GROUPCATEGORY FUNCTIONS
  497. /**
  498. * Get all categories
  499. * @param string $course_code The cours (default = current course)
  500. */
  501. public static function get_categories($course_code = null)
  502. {
  503. $course_info = api_get_course_info($course_code);
  504. $course_id = $course_info['real_id'];
  505. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  506. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id ORDER BY display_order";
  507. $res = Database::query($sql);
  508. $cats = array();
  509. while ($cat = Database::fetch_array($res)) {
  510. $cats[] = $cat;
  511. }
  512. return $cats;
  513. }
  514. /**
  515. * Get a group category
  516. * @param int $id The category id
  517. * @param string $course_code The course (default = current course)
  518. */
  519. public static function get_category($id, $course_code = null)
  520. {
  521. if (empty($id)) {
  522. return array();
  523. }
  524. $course_info = api_get_course_info($course_code);
  525. $course_id = $course_info['real_id'];
  526. $id = Database::escape_string($id);
  527. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  528. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id AND id = $id LIMIT 1";
  529. $res = Database::query($sql);
  530. return Database::fetch_array($res);
  531. }
  532. /**
  533. * Get the unique category of a given group
  534. * @param int $group_id The id of the group
  535. * @param string $course_code The course in which the group is (default =
  536. * current course)
  537. * @return array The category
  538. */
  539. public static function get_category_from_group($group_id, $course_code = null)
  540. {
  541. $table_group = Database :: get_course_table(TABLE_GROUP);
  542. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  543. if (empty($group_id)) {
  544. return array();
  545. }
  546. $course_info = api_get_course_info($course_code);
  547. $course_id = $course_info['real_id'];
  548. $group_id = Database::escape_string($group_id);
  549. $sql = "SELECT gc.* FROM $table_group_cat gc, $table_group g
  550. WHERE gc.c_id = $course_id AND
  551. g.c_id = $course_id AND
  552. gc.id = g.category_id AND g.id= $group_id LIMIT 1";
  553. $res = Database::query($sql);
  554. $cat = array();
  555. if (Database::num_rows($res)) {
  556. $cat = Database::fetch_array($res);
  557. }
  558. return $cat;
  559. }
  560. /**
  561. * Delete a group category
  562. * @param int $cat_id The id of the category to delete
  563. * @param string $course_code The code in which the category should be
  564. * deleted (default = current course)
  565. */
  566. public static function delete_category($cat_id, $course_code = null)
  567. {
  568. $course_info = api_get_course_info($course_code);
  569. $course_id = $course_info['real_id'];
  570. $table_group = Database :: get_course_table(TABLE_GROUP);
  571. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  572. $cat_id = Database::escape_string($cat_id);
  573. $sql = "SELECT id FROM $table_group WHERE c_id = $course_id AND category_id='".$cat_id."'";
  574. $res = Database::query($sql);
  575. if (Database::num_rows($res) > 0) {
  576. $groups_to_delete = array();
  577. while ($group = Database::fetch_object($res)) {
  578. $groups_to_delete[] = $group->id;
  579. }
  580. self :: delete_groups($groups_to_delete);
  581. }
  582. $sql = "DELETE FROM $table_group_cat WHERE c_id = $course_id AND id='".$cat_id."'";
  583. Database::query($sql);
  584. }
  585. /**
  586. * Create group category
  587. * @param string $title The title of the new category
  588. * @param string $description The description of the new category
  589. * @param bool $self_registration_allowed
  590. * @param bool $self_unregistration_allowed
  591. * @param int $max_number_of_students
  592. * @param int $groups_per_user
  593. */
  594. public static function create_category(
  595. $title,
  596. $description,
  597. $doc_state,
  598. $work_state,
  599. $calendar_state,
  600. $announcements_state,
  601. $forum_state,
  602. $wiki_state,
  603. $chat_state = 1,
  604. $self_registration_allowed = 0,
  605. $self_unregistration_allowed = 0,
  606. $maximum_number_of_students = 8,
  607. $groups_per_user = 0
  608. ) {
  609. $table_group_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  610. $course_id = api_get_course_int_id();
  611. $sql = "SELECT MAX(display_order)+1 as new_order FROM $table_group_category WHERE c_id = $course_id ";
  612. $res = Database::query($sql);
  613. $obj = Database::fetch_object($res);
  614. if (!isset ($obj->new_order)) {
  615. $obj->new_order = 1;
  616. }
  617. $sql = "INSERT INTO ".$table_group_category." SET
  618. c_id = $course_id ,
  619. title='".Database::escape_string($title)."',
  620. display_order ='".$obj->new_order."',
  621. description='".Database::escape_string($description)."',
  622. doc_state = '".Database::escape_string($doc_state)."',
  623. work_state = '".Database::escape_string($work_state)."',
  624. calendar_state = '".Database::escape_string($calendar_state)."',
  625. announcements_state = '".Database::escape_string($announcements_state)."',
  626. forum_state = '".Database::escape_string($forum_state)."',
  627. wiki_state = '".Database::escape_string($wiki_state)."',
  628. chat_state = '".Database::escape_string($chat_state)."',
  629. groups_per_user = '".Database::escape_string($groups_per_user)."',
  630. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  631. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  632. max_student = '".Database::escape_string($maximum_number_of_students)."' ";
  633. Database::query($sql);
  634. $id = Database::insert_id();
  635. if ($id == self::VIRTUAL_COURSE_CATEGORY) {
  636. $sql = "UPDATE ".$table_group_category." SET id = ".($id + 1)." WHERE c_id = $course_id AND id = $id";
  637. Database::query($sql);
  638. return $id + 1;
  639. }
  640. return $id;
  641. }
  642. /**
  643. * Update group category
  644. * @param int $id The id of the category
  645. * @param string $title The title of the new category
  646. * @param string $description The description of the new category
  647. * @param bool $self_registration_allowed
  648. * @param bool $self_unregistration_allowed
  649. * @param int $max_number_of_students
  650. * @param int $groups_per_user
  651. */
  652. public static function update_category(
  653. $id,
  654. $title,
  655. $description,
  656. $doc_state,
  657. $work_state,
  658. $calendar_state,
  659. $announcements_state,
  660. $forum_state,
  661. $wiki_state,
  662. $chat_state,
  663. $self_registration_allowed,
  664. $self_unregistration_allowed,
  665. $maximum_number_of_students,
  666. $groups_per_user
  667. ) {
  668. $table_group_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  669. $id = Database::escape_string($id);
  670. $course_id = api_get_course_int_id();
  671. $sql = "UPDATE ".$table_group_category."
  672. SET title='".Database::escape_string($title)."',
  673. description='".Database::escape_string($description)."',
  674. doc_state = '".Database::escape_string($doc_state)."',
  675. work_state = '".Database::escape_string($work_state)."',
  676. calendar_state = '".Database::escape_string($calendar_state)."',
  677. announcements_state = '".Database::escape_string($announcements_state)."',
  678. forum_state = '".Database::escape_string($forum_state)."',
  679. wiki_state = '".Database::escape_string($wiki_state)."',
  680. chat_state = '".Database::escape_string($chat_state)."',
  681. groups_per_user = ".Database::escape_string($groups_per_user).",
  682. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  683. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  684. max_student = ".Database::escape_string($maximum_number_of_students)."
  685. WHERE c_id = $course_id AND id=$id";
  686. Database::query($sql);
  687. }
  688. /**
  689. * Returns the number of groups of the user with the greatest number of
  690. * subscribtions in the given category
  691. */
  692. public static function get_current_max_groups_per_user($category_id = null, $course_code = null)
  693. {
  694. $course_info = api_get_course_info($course_code);
  695. $group_table = Database :: get_course_table(TABLE_GROUP);
  696. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  697. $sql = 'SELECT COUNT(gu.group_id) AS current_max FROM '.$group_user_table.' gu, '.$group_table.' g
  698. WHERE g.c_id = '.$course_info['real_id'].' AND gu.group_id = g.id ';
  699. if ($category_id != null) {
  700. $category_id = Database::escape_string($category_id);
  701. $sql .= ' AND g.category_id = '.$category_id;
  702. }
  703. $sql .= ' GROUP BY gu.user_id ORDER BY current_max DESC LIMIT 1';
  704. $res = Database::query($sql);
  705. $obj = Database::fetch_object($res);
  706. return $obj->current_max;
  707. }
  708. /**
  709. * Swaps the display-order of two categories
  710. * @param int $id1 The id of the first category
  711. * @param int $id2 The id of the second category
  712. */
  713. public static function swap_category_order($id1, $id2)
  714. {
  715. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  716. $id1 = Database::escape_string($id1);
  717. $id2 = Database::escape_string($id2);
  718. $course_id = api_get_course_int_id();
  719. $sql = "SELECT id,display_order FROM $table_group_cat WHERE id IN ($id1,$id2) AND c_id = $course_id ";
  720. $res = Database::query($sql);
  721. $cat1 = Database::fetch_object($res);
  722. $cat2 = Database::fetch_object($res);
  723. $sql = "UPDATE $table_group_cat SET display_order=$cat2->display_order WHERE id = $cat1->id AND c_id = $course_id ";
  724. Database::query($sql);
  725. $sql = "UPDATE $table_group_cat SET display_order=$cat1->display_order WHERE id = $cat2->id AND c_id = $course_id ";
  726. Database::query($sql);
  727. }
  728. //GROUP USERS FUNCTIONS
  729. /**
  730. * Get all users from a given group
  731. * @param int $group_id The group
  732. * @return array list of user id
  733. */
  734. public static function get_users($group_id, $load_extra_info = false, $course_id = null)
  735. {
  736. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  737. $group_id = Database::escape_string($group_id);
  738. if (empty($course_id)) {
  739. $course_id = api_get_course_int_id();
  740. } else {
  741. $course_id = intval($course_id);
  742. }
  743. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  744. $res = Database::query($sql);
  745. $users = array();
  746. while ($obj = Database::fetch_object($res)) {
  747. if ($load_extra_info) {
  748. $users[] = api_get_user_info($obj->user_id);
  749. } else {
  750. $users[] = $obj->user_id;
  751. }
  752. }
  753. return $users;
  754. }
  755. public static function get_members_and_tutors($group_id)
  756. {
  757. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  758. $tutor_user_table = Database :: get_course_table(TABLE_GROUP_TUTOR);
  759. $course_id = api_get_course_int_id();
  760. $group_id = intval($group_id);
  761. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  762. $res = Database::query($sql);
  763. $users = array();
  764. while ($obj = Database::fetch_object($res)) {
  765. $users[] = api_get_user_info($obj->user_id);
  766. }
  767. $sql = "SELECT user_id FROM $tutor_user_table WHERE c_id = $course_id AND group_id = $group_id";
  768. $res = Database::query($sql);
  769. while ($obj = Database::fetch_object($res)) {
  770. $users[] = api_get_user_info($obj->user_id);
  771. }
  772. return $users;
  773. }
  774. /**
  775. * Returns users belonging to any of the group
  776. *
  777. * @param array $groups list of group ids
  778. * @return array list of user ids
  779. */
  780. public static function get_groups_users($groups = array())
  781. {
  782. $result = array();
  783. $tbl_group_user = Database::get_course_table(TABLE_GROUP_USER);
  784. $course_id = api_get_course_int_id();
  785. $groups = array_map('intval', $groups);
  786. $groups = implode(', ', $groups); //protect individual elements with surrounding quotes
  787. $sql = "SELECT DISTINCT user_id
  788. FROM $tbl_group_user gu
  789. WHERE c_id = $course_id AND gu.group_id IN ($groups)";
  790. $rs = Database::query($sql);
  791. while ($row = Database::fetch_array($rs)) {
  792. $result[] = $row['user_id'];
  793. }
  794. return $result;
  795. }
  796. /**
  797. * Fill the groups with students.
  798. * The algorithm takes care to first fill the groups with the least # of users.
  799. * Analysis
  800. * There was a problem with the "ALL" setting.
  801. * When max # of groups is set to all, the value is sometimes NULL and sometimes ALL
  802. * and in both cased the query does not work as expected.
  803. * Stupid solution (currently implemented: set ALL to a big number (INFINITE) and things are solved :)
  804. * Better solution: that's up to you.
  805. *
  806. * Note
  807. * Throughout Dokeos there is some confusion about "course id" and "course code"
  808. * The code is e.g. TEST101, but sometimes a variable that is called courseID also contains a course code string.
  809. * However, there is also a integer course_id that uniquely identifies the course.
  810. * ywarnier:> Now the course_id has been removed (25/1/2005)
  811. * The databases are als very inconsistent in this.
  812. *
  813. * @author Chrisptophe Gesche <christophe.geshe@claroline.net>,
  814. * Hugues Peeters <hugues.peeters@claroline.net> - original version
  815. * @author Roan Embrechts - virtual course support, code cleaning
  816. * @author Bart Mollet - code cleaning, use other GroupManager-functions
  817. * @return void
  818. */
  819. public static function fill_groups($group_ids)
  820. {
  821. global $_course;
  822. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  823. $group_ids = array_map('intval', $group_ids);
  824. if (api_is_course_coach()) {
  825. for ($i = 0; $i < count($group_ids); $i++) {
  826. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  827. array_splice($group_ids, $i, 1);
  828. $i--;
  829. }
  830. }
  831. if (count($group_ids) == 0) {
  832. return false;
  833. }
  834. }
  835. $category = self::get_category_from_group($group_ids[0]);
  836. $groups_per_user = $category['groups_per_user'];
  837. $group_table = Database :: get_course_table(TABLE_GROUP);
  838. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  839. $session_id = api_get_session_id();
  840. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['code'], true, $session_id);
  841. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  842. /*
  843. * Retrieve all the groups where enrollment is still allowed
  844. * (reverse) ordered by the number of place available
  845. */
  846. $course_id = api_get_course_int_id();
  847. $sql = "SELECT g.id gid, g.max_student-count(ug.user_id) nbPlaces, g.max_student
  848. FROM ".$group_table." g
  849. LEFT JOIN ".$group_user_table." ug ON
  850. g.c_id = $course_id AND ug.c_id = $course_id AND g.id = ug.group_id
  851. WHERE
  852. g.id IN (".implode(',', $group_ids).")
  853. GROUP BY (g.id)
  854. HAVING (nbPlaces > 0 OR g.max_student = ".self::MEMBER_PER_GROUP_NO_LIMIT.")
  855. ORDER BY nbPlaces DESC";
  856. $sql_result = Database::query($sql);
  857. $group_available_place = array();
  858. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  859. $group_available_place[$group['gid']] = $group['nbPlaces'];
  860. }
  861. /*
  862. * Retrieve course users (reverse) ordered by the number
  863. * of group they are already enrolled
  864. */
  865. for ($i = 0; $i < count($complete_user_list); $i++) {
  866. //find # of groups the user is enrolled in
  867. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"], $category['id']);
  868. //add # of groups to user list
  869. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  870. }
  871. //first sort by user_id to filter out duplicates
  872. $complete_user_list = TableSort :: sort_table($complete_user_list, 'user_id');
  873. $complete_user_list = self :: filter_duplicates($complete_user_list, 'user_id');
  874. $complete_user_list = self :: filter_only_students($complete_user_list);
  875. //now sort by # of group left
  876. $complete_user_list = TableSort :: sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  877. $userToken = array();
  878. foreach ($complete_user_list as $this_user) {
  879. if ($this_user['number_groups_left'] > 0) {
  880. $userToken[$this_user['user_id']] = $this_user['number_groups_left'];
  881. }
  882. }
  883. $changed = true;
  884. while ($changed) {
  885. $changed = false;
  886. reset($group_available_place);
  887. arsort($group_available_place);
  888. reset($userToken);
  889. arsort($userToken);
  890. foreach ($group_available_place as $group_id => $place) {
  891. foreach ($userToken as $user_id => $places) {
  892. if (self :: can_user_subscribe($user_id, $group_id)) {
  893. self :: subscribe_users($user_id, $group_id);
  894. $group_available_place[$group_id]--;
  895. //$userToken[$user_id]--;
  896. unset($userToken[$user_id]);
  897. $changed = true;
  898. break;
  899. }
  900. }
  901. if ($changed) {
  902. break;
  903. }
  904. }
  905. }
  906. }
  907. /**
  908. * Get the number of students in a group.
  909. * @param int $group_id
  910. * @return int Number of students in the given group.
  911. */
  912. public static function number_of_students($group_id, $course_id = null)
  913. {
  914. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  915. $group_id = Database::escape_string($group_id);
  916. if (empty($course_id)) {
  917. $course_id = api_get_course_int_id();
  918. } else {
  919. $course_id = intval($course_id);
  920. }
  921. $sql = "SELECT COUNT(*) AS number_of_students FROM $table_group_user WHERE c_id = $course_id AND group_id = $group_id";
  922. $db_result = Database::query($sql);
  923. $db_object = Database::fetch_object($db_result);
  924. return $db_object->number_of_students;
  925. }
  926. /**
  927. * Maximum number of students in a group
  928. * @param int $group_id
  929. * @return int Maximum number of students in the given group.
  930. */
  931. public static function maximum_number_of_students($group_id)
  932. {
  933. $table_group = Database :: get_course_table(TABLE_GROUP);
  934. $group_id = Database::escape_string($group_id);
  935. $course_id = api_get_course_int_id();
  936. $db_result = Database::query("SELECT max_student FROM $table_group WHERE c_id = $course_id AND id = $group_id");
  937. $db_object = Database::fetch_object($db_result);
  938. if ($db_object->max_student == 0) {
  939. return self::INFINITE;
  940. }
  941. return $db_object->max_student;
  942. }
  943. /**
  944. * Number of groups of a user
  945. * @param int $user_id
  946. * @return int The number of groups the user is subscribed in.
  947. */
  948. public static function user_in_number_of_groups($user_id, $cat_id = null)
  949. {
  950. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  951. $table_group = Database :: get_course_table(TABLE_GROUP);
  952. $user_id = Database::escape_string($user_id);
  953. $cat_id = Database::escape_string($cat_id);
  954. $course_id = api_get_course_int_id();
  955. $cat_condition = '';
  956. if (!empty($cat_id)) {
  957. $cat_condition = " AND g.category_id = $cat_id ";
  958. }
  959. $sql = "SELECT COUNT(*) AS number_of_groups FROM $table_group_user gu, $table_group g
  960. WHERE gu.c_id = $course_id AND
  961. g.c_id = $course_id AND
  962. gu.user_id = $user_id AND
  963. g.id = gu.group_id $cat_condition";
  964. $db_result = Database::query($sql);
  965. $db_object = Database::fetch_object($db_result);
  966. return $db_object->number_of_groups;
  967. }
  968. /**
  969. * Is sef-registration allowed?
  970. * @param int $user_id
  971. * @param int $group_id
  972. * @return bool TRUE if self-registration is allowed in the given group.
  973. */
  974. public static function is_self_registration_allowed($user_id, $group_id)
  975. {
  976. $course_id = api_get_course_int_id();
  977. if (!$user_id > 0) {
  978. return false;
  979. }
  980. $table_group = Database :: get_course_table(TABLE_GROUP);
  981. $group_id = intval($group_id);
  982. if (isset($group_id)) {
  983. $sql = "SELECT self_registration_allowed FROM $table_group WHERE c_id = $course_id AND id = $group_id";
  984. $db_result = Database::query($sql);
  985. $db_object = Database::fetch_object($db_result);
  986. return $db_object->self_registration_allowed == 1 && self :: can_user_subscribe($user_id, $group_id);
  987. } else {
  988. return false;
  989. }
  990. }
  991. /**
  992. * Is sef-unregistration allowed?
  993. * @param int $user_id
  994. * @param int $group_id
  995. * @return bool TRUE if self-unregistration is allowed in the given group.
  996. */
  997. public static function is_self_unregistration_allowed($user_id, $group_id)
  998. {
  999. if (!$user_id > 0) {
  1000. return false;
  1001. }
  1002. $table_group = Database :: get_course_table(TABLE_GROUP);
  1003. $group_id = Database::escape_string($group_id);
  1004. $course_id = api_get_course_int_id();
  1005. $db_result = Database::query(
  1006. 'SELECT self_unregistration_allowed FROM '.$table_group.' WHERE c_id = '.$course_id.' AND id = '.$group_id
  1007. );
  1008. $db_object = Database::fetch_object($db_result);
  1009. return $db_object->self_unregistration_allowed == 1 && self :: can_user_unsubscribe($user_id, $group_id);
  1010. }
  1011. /**
  1012. * Is user subscribed in group?
  1013. * @param int $user_id
  1014. * @param int $group_id
  1015. * @return bool TRUE if given user is subscribed in given group
  1016. */
  1017. public static function is_subscribed($user_id, $group_id)
  1018. {
  1019. if (empty($user_id) or empty($group_id)) {
  1020. return false;
  1021. }
  1022. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1023. $group_id = Database::escape_string($group_id);
  1024. $user_id = Database::escape_string($user_id);
  1025. $course_id = api_get_course_int_id();
  1026. $sql = 'SELECT 1 FROM '.$table_group_user.' WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id = '.$user_id;
  1027. $db_result = Database::query($sql);
  1028. return Database::num_rows($db_result) > 0;
  1029. }
  1030. /**
  1031. * Can a user subscribe to a specified group in a course
  1032. * @param int $user_id
  1033. * @param int $group_id
  1034. * @return bool TRUE if given user can be subscribed in given group
  1035. */
  1036. public static function can_user_subscribe($user_id, $group_id)
  1037. {
  1038. global $_course;
  1039. $course_code = $_course['sysCode'];
  1040. $category = self :: get_category_from_group($group_id);
  1041. $result = CourseManager :: is_user_subscribed_in_real_or_linked_course($user_id, $course_code);
  1042. $result = !self :: is_subscribed($user_id, $group_id);
  1043. $result &= (self :: number_of_students($group_id) < self :: maximum_number_of_students($group_id));
  1044. if ($category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT) {
  1045. $category['groups_per_user'] = self::INFINITE;
  1046. }
  1047. $result &= (self :: user_in_number_of_groups($user_id, $category['id']) < $category['groups_per_user']);
  1048. $result &= !self :: is_tutor_of_group($user_id, $group_id);
  1049. return $result;
  1050. }
  1051. /**
  1052. * Can a user unsubscribe to a specified group in a course
  1053. * @param int $user_id
  1054. * @param int $group_id
  1055. * @return bool TRUE if given user can be unsubscribed from given group
  1056. * @internal for now, same as GroupManager::is_subscribed($user_id,$group_id)
  1057. */
  1058. public static function can_user_unsubscribe($user_id, $group_id)
  1059. {
  1060. $result = self :: is_subscribed($user_id, $group_id);
  1061. return $result;
  1062. }
  1063. /**
  1064. * Get all subscribed users from a group
  1065. * @param int $group_id
  1066. * @return array An array with information of all users from the given group.
  1067. * (user_id, firstname, lastname, email)
  1068. */
  1069. public static function get_subscribed_users($group_id)
  1070. {
  1071. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1072. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1073. $order_clause = api_sort_by_first_name(
  1074. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1075. if (empty($group_id)) {
  1076. return array();
  1077. }
  1078. $group_id = intval($group_id);
  1079. $course_id = api_get_course_int_id();
  1080. $sql = "SELECT ug.id, u.user_id, u.lastname, u.firstname, u.email, u.username
  1081. FROM $table_user u INNER JOIN $table_group_user ug ON (ug.user_id = u.user_id)
  1082. WHERE ug.c_id = $course_id AND
  1083. ug.group_id = $group_id
  1084. $order_clause";
  1085. $db_result = Database::query($sql);
  1086. $users = array();
  1087. while ($user = Database::fetch_object($db_result)) {
  1088. $member['user_id'] = $user->user_id;
  1089. $member['firstname'] = $user->firstname;
  1090. $member['lastname'] = $user->lastname;
  1091. $member['email'] = $user->email;
  1092. $member['username'] = $user->username;
  1093. $users[$member['user_id']] = $member;
  1094. }
  1095. return $users;
  1096. }
  1097. /**
  1098. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1099. * Get all subscribed tutors of a group
  1100. * @param int $group_id
  1101. * @return array An array with information of all users from the given group.
  1102. * (user_id, firstname, lastname, email)
  1103. */
  1104. public static function get_subscribed_tutors($group_id, $id_only = false)
  1105. {
  1106. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1107. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1108. $order_clause = api_sort_by_first_name(
  1109. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1110. $group_id = Database::escape_string($group_id);
  1111. $course_id = api_get_course_int_id();
  1112. $sql = "SELECT tg.id, u.user_id, u.lastname, u.firstname, u.email
  1113. FROM ".$table_user." u, ".$table_group_tutor." tg
  1114. WHERE tg.c_id = $course_id AND
  1115. tg.group_id='".$group_id."' AND
  1116. tg.user_id=u.user_id".$order_clause;
  1117. $db_result = Database::query($sql);
  1118. $users = array();
  1119. while ($user = Database::fetch_object($db_result)) {
  1120. if (!$id_only) {
  1121. $member['user_id'] = $user->user_id;
  1122. $member['firstname'] = $user->firstname;
  1123. $member['lastname'] = $user->lastname;
  1124. $member['email'] = $user->email;
  1125. $users[] = $member;
  1126. } else {
  1127. $users[] = $user->user_id;
  1128. }
  1129. }
  1130. return $users;
  1131. }
  1132. /**
  1133. * Subscribe user(s) to a specified group in current course
  1134. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1135. * @param int $group_id
  1136. * @return bool TRUE if successfull
  1137. */
  1138. public static function subscribe_users($user_ids, $group_id)
  1139. {
  1140. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1141. $result = true;
  1142. $course_id = api_get_course_int_id();
  1143. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1144. if (!empty($user_ids)) {
  1145. foreach ($user_ids as $index => $user_id) {
  1146. $user_id = Database::escape_string($user_id);
  1147. $group_id = Database::escape_string($group_id);
  1148. $sql = "INSERT INTO ".$table_group_user." (c_id, user_id, group_id) VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1149. $result &= Database::query($sql);
  1150. }
  1151. }
  1152. return $result;
  1153. }
  1154. /**
  1155. * Subscribe tutor(s) to a specified group in current course
  1156. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1157. * @param int $group_id
  1158. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1159. * @see subscribe_users. This function is almost an exact copy of that function.
  1160. * @return bool TRUE if successfull
  1161. */
  1162. public static function subscribe_tutors($user_ids, $group_id)
  1163. {
  1164. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1165. $result = true;
  1166. $course_id = api_get_course_int_id();
  1167. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1168. foreach ($user_ids as $index => $user_id) {
  1169. $user_id = Database::escape_string($user_id);
  1170. $group_id = Database::escape_string($group_id);
  1171. $sql = "INSERT INTO ".$table_group_tutor." (c_id, user_id, group_id) VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1172. $result &= Database::query($sql);
  1173. }
  1174. return $result;
  1175. }
  1176. /**
  1177. * Unsubscribe user(s) from a specified group in current course
  1178. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1179. * @param int $group_id
  1180. * @return bool TRUE if successfull
  1181. */
  1182. public static function unsubscribe_users($user_ids, $group_id)
  1183. {
  1184. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1185. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1186. $group_id = Database::escape_string($group_id);
  1187. $course_id = api_get_course_int_id();
  1188. $result &= Database::query(
  1189. 'DELETE FROM '.$table_group_user.' WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id IN ('.implode(
  1190. ',',
  1191. $user_ids
  1192. ).')'
  1193. );
  1194. }
  1195. /**
  1196. * Unsubscribe all users from one or more groups
  1197. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1198. * @return bool TRUE if successfull
  1199. */
  1200. public static function unsubscribe_all_users($group_ids)
  1201. {
  1202. $course_id = api_get_course_int_id();
  1203. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1204. $group_ids = array_map('intval', $group_ids);
  1205. if (count($group_ids) > 0) {
  1206. if (api_is_course_coach()) {
  1207. for ($i = 0; $i < count($group_ids); $i++) {
  1208. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1209. array_splice($group_ids, $i, 1);
  1210. $i--;
  1211. }
  1212. }
  1213. if (count($group_ids) == 0) {
  1214. return false;
  1215. }
  1216. }
  1217. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1218. $sql = 'DELETE FROM '.$table_group_user.' WHERE group_id IN ('.implode(
  1219. ',',
  1220. $group_ids
  1221. ).') AND c_id = '.$course_id;
  1222. $result = Database::query($sql);
  1223. return $result;
  1224. }
  1225. return true;
  1226. }
  1227. /**
  1228. * Unsubscribe all tutors from one or more groups
  1229. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1230. * @see unsubscribe_all_users. This function is almost an exact copy of that function.
  1231. * @return bool TRUE if successfull
  1232. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1233. */
  1234. public static function unsubscribe_all_tutors($group_ids)
  1235. {
  1236. $course_id = api_get_course_int_id();
  1237. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1238. if (count($group_ids) > 0) {
  1239. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1240. $sql = 'DELETE FROM '.$table_group_tutor.' WHERE group_id IN ('.implode(
  1241. ',',
  1242. $group_ids
  1243. ).') AND c_id = '.$course_id;
  1244. $result = Database::query($sql);
  1245. return $result;
  1246. }
  1247. return true;
  1248. }
  1249. /**
  1250. * Is the user a tutor of this group?
  1251. * @param $user_id the id of the user
  1252. * @param $group_id the id of the group
  1253. * @return boolean true/false
  1254. * @todo use the function user_has_access that includes this function
  1255. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1256. */
  1257. public static function is_tutor_of_group($user_id, $group_id)
  1258. {
  1259. global $_course;
  1260. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1261. $user_id = Database::escape_string($user_id);
  1262. $group_id = Database::escape_string($group_id);
  1263. $course_id = api_get_course_int_id();
  1264. $sql = "SELECT * FROM ".$table_group_tutor." WHERE c_id = $course_id AND user_id='".$user_id."' AND group_id='".$group_id."'";
  1265. $result = Database::query($sql);
  1266. if (Database::num_rows($result) > 0) {
  1267. return true;
  1268. } else {
  1269. return false;
  1270. }
  1271. }
  1272. /**
  1273. * Is the user part of this group? This can be a tutor or a normal member
  1274. * you should use this function if the access to a tool or functionality is restricted to the people who are actually in the group
  1275. * before you had to check if the user was 1. a member of the group OR 2. a tutor of the group. This function combines both
  1276. * @param $user_id the id of the user
  1277. * @param $group_id the id of the group
  1278. * @return boolean true/false
  1279. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1280. */
  1281. public static function is_user_in_group($user_id, $group_id)
  1282. {
  1283. $member = self :: is_subscribed($user_id, $group_id);
  1284. $tutor = self :: is_tutor_of_group($user_id, $group_id);
  1285. if ($member OR $tutor) {
  1286. return true;
  1287. } else {
  1288. return false;
  1289. }
  1290. }
  1291. /**
  1292. * Get all tutors for the current course.
  1293. * @return array An array with firstname, lastname and user_id for all
  1294. * tutors in the current course.
  1295. * @deprecated this function uses the old tutor implementation
  1296. */
  1297. public static function get_all_tutors()
  1298. {
  1299. global $_course;
  1300. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1301. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1302. $sql = "SELECT user.user_id AS user_id, user.lastname AS lastname, user.firstname AS firstname
  1303. FROM ".$user_table." user, ".$course_user_table." cu
  1304. WHERE cu.user_id=user.user_id
  1305. AND cu.tutor_id='1'
  1306. AND cu.course_code='".$_course['sysCode']."'";
  1307. $resultTutor = Database::query($sql);
  1308. $tutors = array();
  1309. while ($tutor = Database::fetch_array($resultTutor)) {
  1310. $tutors[] = $tutor;
  1311. }
  1312. return $tutors;
  1313. }
  1314. /**
  1315. * Is user a tutor in current course
  1316. * @param int $user_id
  1317. * @return bool TRUE if given user is a tutor in the current course.
  1318. * @deprecated this function uses the old tutor implementation
  1319. */
  1320. public static function is_tutor($user_id)
  1321. {
  1322. global $_course;
  1323. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1324. $user_id = Database::escape_string($user_id);
  1325. $sql = "SELECT tutor_id FROM ".$course_user_table."
  1326. WHERE user_id='".$user_id."' AND course_code='".$_course['code']."'"."AND tutor_id=1";
  1327. $db_result = Database::query($sql);
  1328. $result = (Database::num_rows($db_result) > 0);
  1329. return $result;
  1330. }
  1331. /**
  1332. * Get all group's from a given course in which a given user is unsubscribed
  1333. * @author Patrick Cool
  1334. * @param int course id
  1335. * retrieve the groups for
  1336. * @param integer $user_id: the ID of the user you want to know all its
  1337. * group memberships
  1338. */
  1339. public static function get_group_ids($course_id, $user_id)
  1340. {
  1341. $groups = array();
  1342. $tbl_group = Database::get_course_table(TABLE_GROUP_USER);
  1343. $tbl_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1344. $user_id = intval($user_id);
  1345. $course_id = intval($course_id);
  1346. $sql = "SELECT group_id FROM $tbl_group WHERE c_id = $course_id AND user_id = '$user_id'";
  1347. $groupres = Database::query($sql);
  1348. if ($groupres) {
  1349. while ($myrow = Database::fetch_array($groupres)) {
  1350. $groups[] = $myrow['group_id'];
  1351. }
  1352. }
  1353. //Also loading if i'm the tutor
  1354. $sql = "SELECT group_id FROM $tbl_group_tutor WHERE c_id = $course_id AND user_id = '$user_id'";
  1355. $groupres = Database::query($sql);
  1356. if ($groupres) {
  1357. while ($myrow = Database::fetch_array($groupres)) {
  1358. $groups[] = $myrow['group_id'];
  1359. }
  1360. }
  1361. if (!empty($groups)) {
  1362. array_filter($groups);
  1363. }
  1364. return $groups;
  1365. }
  1366. /*
  1367. Group functions - these take virtual/linked courses into account when necessary
  1368. */
  1369. /**
  1370. * Get a combined list of all users of the real course $course_code
  1371. * and all users in virtual courses linked to this course $course_code
  1372. * Filter user list: remove duplicate users; plus
  1373. * remove users that
  1374. * - are already in the current group $group_id;
  1375. * - do not have student status in these courses;
  1376. * - are not appointed as tutor (group assistent) for this group;
  1377. * - have already reached their maximum # of groups in this course.
  1378. *
  1379. * Originally to get the correct list of users a big SQL statement was used,
  1380. * but this has become more complicated now there is not just one real course but many virtual courses.
  1381. * Still, that could have worked as well.
  1382. *
  1383. * @version 1.1.3
  1384. * @author Roan Embrechts
  1385. */
  1386. public static function get_complete_list_of_users_that_can_be_added_to_group($course_code, $group_id)
  1387. {
  1388. global $_course, $_user;
  1389. $category = self :: get_category_from_group($group_id, $course_code);
  1390. $number_of_groups_limit = $category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $category['groups_per_user'];
  1391. $real_course_code = $_course['sysCode'];
  1392. $real_course_info = Database :: get_course_info($real_course_code);
  1393. $real_course_user_list = CourseManager :: get_user_list_from_course_code($real_course_code);
  1394. //get list of all virtual courses
  1395. $user_subscribed_course_list = CourseManager :: get_list_of_virtual_courses_for_specific_user_and_real_course(
  1396. $_user['user_id'],
  1397. $real_course_code
  1398. );
  1399. //add real course to the list
  1400. $user_subscribed_course_list[] = $real_course_info;
  1401. if (!is_array($user_subscribed_course_list)) {
  1402. return;
  1403. }
  1404. //for all courses...
  1405. foreach ($user_subscribed_course_list as $this_course) {
  1406. $this_course_code = $this_course['code'];
  1407. $course_user_list = CourseManager :: get_user_list_from_course_code($this_course_code);
  1408. //for all users in the course
  1409. foreach ($course_user_list as $this_user) {
  1410. $user_id = $this_user['user_id'];
  1411. $loginname = $this_user['username'];
  1412. $lastname = $this_user['lastname'];
  1413. $firstname = $this_user['firstname'];
  1414. $status = $this_user['status'];
  1415. //$role = $this_user['role'];
  1416. $tutor_id = $this_user['tutor_id'];
  1417. $full_name = api_get_person_name($firstname, $lastname);
  1418. if ($lastname == "" || $firstname == '') {
  1419. $full_name = $loginname;
  1420. }
  1421. $complete_user['user_id'] = $user_id;
  1422. $complete_user['full_name'] = $full_name;
  1423. $complete_user['firstname'] = $firstname;
  1424. $complete_user['lastname'] = $lastname;
  1425. $complete_user['status'] = $status;
  1426. $complete_user['tutor_id'] = $tutor_id;
  1427. $student_number_of_groups = self :: user_in_number_of_groups($user_id, $category['id']);
  1428. //filter: only add users that have not exceeded their maximum amount of groups
  1429. if ($student_number_of_groups < $number_of_groups_limit) {
  1430. $complete_user_list[] = $complete_user;
  1431. }
  1432. }
  1433. }
  1434. if (is_array($complete_user_list)) {
  1435. //sort once, on array field "full_name"
  1436. $complete_user_list = TableSort :: sort_table($complete_user_list, "full_name");
  1437. //filter out duplicates, based on field "user_id"
  1438. $complete_user_list = self :: filter_duplicates($complete_user_list, "user_id");
  1439. $complete_user_list = self :: filter_users_already_in_group($complete_user_list, $group_id);
  1440. }
  1441. return $complete_user_list;
  1442. }
  1443. /**
  1444. * Filter out duplicates in a multidimensional array
  1445. * by comparing field $compare_field.
  1446. *
  1447. * @param $user_array_in list of users (must be sorted).
  1448. * @param string $compare_field, the field to be compared
  1449. */
  1450. public static function filter_duplicates($user_array_in, $compare_field)
  1451. {
  1452. $total_number = count($user_array_in);
  1453. $user_array_out[0] = $user_array_in[0];
  1454. $count_out = 0;
  1455. for ($count_in = 1; $count_in < $total_number; $count_in++) {
  1456. if ($user_array_in[$count_in][$compare_field] != $user_array_out[$count_out][$compare_field]) {
  1457. $count_out++;
  1458. $user_array_out[$count_out] = $user_array_in[$count_in];
  1459. }
  1460. }
  1461. return $user_array_out;
  1462. }
  1463. /**
  1464. * Filters from the array $user_array_in the users already in the group $group_id.
  1465. */
  1466. public static function filter_users_already_in_group($user_array_in, $group_id)
  1467. {
  1468. foreach ($user_array_in as $this_user) {
  1469. if (!self :: is_subscribed($this_user['user_id'], $group_id)) {
  1470. $user_array_out[] = $this_user;
  1471. }
  1472. }
  1473. return $user_array_out;
  1474. }
  1475. /**
  1476. * Remove all users that are not students and all users who have tutor status
  1477. * from the list.
  1478. */
  1479. public static function filter_only_students($user_array_in)
  1480. {
  1481. $user_array_out = array();
  1482. foreach ($user_array_in as $this_user) {
  1483. //if ($this_user['status_rel'] == STUDENT && $this_user['tutor_id'] == 0) {
  1484. if (api_get_session_id()) {
  1485. if ($this_user['status_session'] == 0) {
  1486. $user_array_out[] = $this_user;
  1487. }
  1488. } else {
  1489. if ($this_user['status_rel'] == STUDENT) {
  1490. $user_array_out[] = $this_user;
  1491. }
  1492. }
  1493. }
  1494. return $user_array_out;
  1495. }
  1496. /**
  1497. * Check if a user has access to a certain group tool
  1498. * @param int $user_id The user id
  1499. * @param int $group_id The group id
  1500. * @param constant $tool The tool to check the access rights. This should be
  1501. * one of constants: GROUP_TOOL_DOCUMENTS
  1502. * @return bool True if the given user has access to the given tool in the
  1503. * given course.
  1504. */
  1505. public static function user_has_access($user_id, $group_id, $tool)
  1506. {
  1507. //Admin have access everywhere
  1508. if (api_is_platform_admin()) {
  1509. return true;
  1510. }
  1511. //Course admin also have access to everything
  1512. if (api_is_allowed_to_edit()) {
  1513. return true;
  1514. }
  1515. switch ($tool) {
  1516. case self::GROUP_TOOL_FORUM :
  1517. $state_key = 'forum_state';
  1518. break;
  1519. case self::GROUP_TOOL_DOCUMENTS :
  1520. $state_key = 'doc_state';
  1521. break;
  1522. case self::GROUP_TOOL_CALENDAR :
  1523. $state_key = 'calendar_state';
  1524. break;
  1525. case self::GROUP_TOOL_ANNOUNCEMENT :
  1526. $state_key = 'announcements_state';
  1527. break;
  1528. case self::GROUP_TOOL_WORK :
  1529. $state_key = 'work_state';
  1530. break;
  1531. case self::GROUP_TOOL_WIKI :
  1532. $state_key = 'wiki_state';
  1533. break;
  1534. case self::GROUP_TOOL_CHAT :
  1535. $state_key = 'chat_state';
  1536. break;
  1537. default:
  1538. return false;
  1539. }
  1540. $user_is_in_group = self :: is_user_in_group($user_id, $group_id);
  1541. //Check group properties
  1542. $group_info = self :: get_group_properties($group_id);
  1543. if (api_get_setting('allow_group_categories') == 'true') {
  1544. //Check group category if exists
  1545. $category_group_info = self::get_category_from_group($group_id);
  1546. if (!empty($category_group_info)) {
  1547. //if exists check the category group status first
  1548. if ($category_group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1549. return false;
  1550. } elseif ($category_group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1551. return false;
  1552. }
  1553. }
  1554. }
  1555. //is_user_in_group() is more complete that the is_subscribed() function
  1556. if ($group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1557. return false;
  1558. } elseif ($group_info[$state_key] == self::TOOL_PUBLIC) {
  1559. return true;
  1560. } elseif (api_is_allowed_to_edit(false, true)) {
  1561. return true;
  1562. } elseif ($group_info['tutor_id'] == $user_id) { //this tutor implementation was dropped
  1563. return true;
  1564. } elseif ($group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1565. return false;
  1566. } else {
  1567. return $user_is_in_group;
  1568. }
  1569. }
  1570. /**
  1571. * Get all groups where a specific user is subscribed
  1572. */
  1573. public static function get_user_group_name($user_id)
  1574. {
  1575. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1576. $table_group = Database::get_course_table(TABLE_GROUP);
  1577. $user_id = intval($user_id);
  1578. $course_id = api_get_course_int_id();
  1579. $sql_groups = "SELECT name FROM $table_group g INNER JOIN $table_group_user gu
  1580. ON (gu.group_id=g.id)
  1581. WHERE gu.c_id= $course_id AND
  1582. g.c_id= $course_id AND
  1583. gu.user_id = $user_id";
  1584. $res = Database::query($sql_groups);
  1585. $groups = array();
  1586. while ($group = Database::fetch_array($res)) {
  1587. $groups[] .= $group['name'];
  1588. }
  1589. return $groups;
  1590. }
  1591. /**
  1592. * fill_groups_list
  1593. *
  1594. * see : fill_groups
  1595. * Fill the groups with students.
  1596. *
  1597. * note :
  1598. * optimize fill_groups_list <--> fill_groups
  1599. *
  1600. */
  1601. public static function fill_groups_list($group_ids)
  1602. {
  1603. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1604. $group_ids = array_map('intval', $group_ids);
  1605. if (api_is_course_coach()) {
  1606. for ($i = 0; $i < count($group_ids); $i++) {
  1607. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1608. array_splice($group_ids, $i, 1);
  1609. $i--;
  1610. }
  1611. }
  1612. if (count($group_ids) == 0) {
  1613. return false;
  1614. }
  1615. }
  1616. global $_course;
  1617. $category = self :: get_category_from_group($group_ids[0]);
  1618. $groups_per_user = $category['groups_per_user'];
  1619. $group_table = Database :: get_course_table(TABLE_GROUP);
  1620. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  1621. $session_id = api_get_session_id();
  1622. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['sysCode'], true, $session_id);
  1623. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  1624. $course_id = api_get_course_int_id();
  1625. /*
  1626. * Retrieve all the groups where enrollment is still allowed
  1627. * (reverse) ordered by the number of place available
  1628. */
  1629. $sql = "SELECT g.id gid, g.max_student - count(ug.user_id) nbPlaces, g.max_student
  1630. FROM ".$group_table." g
  1631. LEFT JOIN ".$group_user_table." ug
  1632. ON g.id = ug.group_id
  1633. WHERE g.c_id = $course_id AND
  1634. ug.c_id = $course_id AND
  1635. g.id IN (".implode(',', $group_ids).")
  1636. GROUP BY (g.id)
  1637. HAVING (nbPlaces > 0 OR g.max_student = ".self::MEMBER_PER_GROUP_NO_LIMIT.")
  1638. ORDER BY nbPlaces DESC";
  1639. $sql_result = Database::query($sql);
  1640. $group_available_place = array();
  1641. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  1642. $group_available_place[$group['gid']] = $group['nbPlaces'];
  1643. }
  1644. /*
  1645. * Retrieve course users (reverse) ordered by the number
  1646. * of group they are already enrolled
  1647. */
  1648. for ($i = 0; $i < count($complete_user_list); $i++) {
  1649. //find # of groups the user is enrolled in
  1650. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"], $category['id']);
  1651. //add # of groups to user list
  1652. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  1653. }
  1654. //first sort by user_id to filter out duplicates
  1655. $complete_user_list = TableSort :: sort_table($complete_user_list, 'user_id');
  1656. $complete_user_list = self :: filter_duplicates($complete_user_list, 'user_id');
  1657. //now sort by # of group left
  1658. $complete_user_list = TableSort :: sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  1659. return $complete_user_list;
  1660. }
  1661. static function process_groups($group_list, $category_id = null)
  1662. {
  1663. global $origin, $charset;
  1664. $category_id = intval($category_id);
  1665. $totalRegistered = 0;
  1666. $group_data = array();
  1667. $user_info = api_get_user_info();
  1668. $session_id = api_get_session_id();
  1669. $user_id = $user_info['user_id'];
  1670. $orig = isset($origin) ? $origin : null;
  1671. foreach ($group_list as $this_group) {
  1672. // Validacion when belongs to a session
  1673. $session_img = api_get_session_image($this_group['session_id'], $user_info['status']);
  1674. // All the tutors of this group
  1675. $tutorsids_of_group = self::get_subscribed_tutors($this_group['id'], true);
  1676. // Create a new table-row
  1677. $row = array();
  1678. // Checkbox
  1679. if (api_is_allowed_to_edit(false, true) && count($group_list) > 1) {
  1680. $row[] = $this_group['id'];
  1681. }
  1682. // Group name
  1683. if ((api_is_allowed_to_edit(false, true) ||
  1684. in_array($user_id, $tutorsids_of_group) ||
  1685. $this_group['is_member'] ||
  1686. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_FORUM) ||
  1687. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_DOCUMENTS) ||
  1688. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_CALENDAR) ||
  1689. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_ANNOUNCEMENT) ||
  1690. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WORK) ||
  1691. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WIKI))
  1692. && !(api_is_course_coach() && intval($this_group['session_id']) != $session_id)
  1693. ) {
  1694. $group_name = '<a href="group_space.php?cidReq='.api_get_course_id(
  1695. ).'&amp;origin='.$orig.'&amp;gidReq='.$this_group['id'].'">'.Security::remove_XSS(
  1696. $this_group['name']
  1697. ).'</a> ';
  1698. if (!empty($user_id) && !empty($this_group['id_tutor']) && $user_id == $this_group['id_tutor']) {
  1699. $group_name .= Display::label(get_lang('OneMyGroups'), 'success');
  1700. } elseif ($this_group['is_member']) {
  1701. $group_name .= Display::label(get_lang('MyGroup'), 'success');
  1702. }
  1703. if (api_is_allowed_to_edit() && !empty($this_group['session_name'])) {
  1704. $group_name .= ' ('.$this_group['session_name'].')';
  1705. }
  1706. $group_name .= $session_img;
  1707. $row[] = $group_name.'<br />'.stripslashes(trim($this_group['description']));
  1708. } else {
  1709. $row[] = $this_group['name'].'<br />'.stripslashes(trim($this_group['description']));
  1710. }
  1711. // Tutor name
  1712. $tutor_info = null;
  1713. if (count($tutorsids_of_group) > 0) {
  1714. foreach ($tutorsids_of_group as $tutor_id) {
  1715. $tutor = api_get_user_info($tutor_id);
  1716. $username = api_htmlentities(sprintf(get_lang('LoginX'), $tutor['username']), ENT_QUOTES);
  1717. if (api_get_setting('show_email_addresses') == 'true') {
  1718. $tutor_info .= Display::tag(
  1719. 'span',
  1720. Display::encrypted_mailto_link(
  1721. $tutor['mail'],
  1722. api_get_person_name($tutor['firstName'], $tutor['lastName'])
  1723. ),
  1724. array('title' => $username)
  1725. ).', ';
  1726. } else {
  1727. if (api_is_allowed_to_edit()) {
  1728. $tutor_info .= Display::tag(
  1729. 'span',
  1730. Display::encrypted_mailto_link(
  1731. $tutor['mail'],
  1732. api_get_person_name($tutor['firstName'], $tutor['lastName'])
  1733. ),
  1734. array('title' => $username)
  1735. ).', ';
  1736. } else {
  1737. $tutor_info .= Display::tag(
  1738. 'span',
  1739. api_get_person_name($tutor['firstName'], $tutor['lastName']),
  1740. array('title' => $username)
  1741. ).', ';
  1742. }
  1743. }
  1744. }
  1745. }
  1746. $tutor_info = api_substr($tutor_info, 0, api_strlen($tutor_info) - 2);
  1747. $row[] = $tutor_info;
  1748. // Max number of members in group
  1749. $max_members = ($this_group['maximum_number_of_members'] == self::MEMBER_PER_GROUP_NO_LIMIT ? ' ' : ' / '.$this_group['maximum_number_of_members']);
  1750. // Number of members in group
  1751. $row[] = $this_group['number_of_members'].$max_members;
  1752. // Self-registration / unregistration
  1753. if (!api_is_allowed_to_edit(false, true)) {
  1754. if (self :: is_self_registration_allowed($user_id, $this_group['id'])) {
  1755. $row[] = '<a class = "btn" href="group.php?'.api_get_cidreq(
  1756. ).'&category='.$category_id.'&amp;action=self_reg&amp;group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(
  1757. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)
  1758. )."'".')) return false;">'.get_lang('GroupSelfRegInf').'</a>';
  1759. } elseif (self :: is_self_unregistration_allowed($user_id, $this_group['id'])) {
  1760. $row[] = '<a class = "btn" href="group.php?'.api_get_cidreq(
  1761. ).'&category='.$category_id.'&amp;action=self_unreg&amp;group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(
  1762. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset)
  1763. )."'".')) return false;">'.get_lang('GroupSelfUnRegInf').'</a>';
  1764. } else {
  1765. $row[] = '-';
  1766. }
  1767. }
  1768. // Edit-links
  1769. if (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && intval(
  1770. $this_group['session_id']
  1771. ) != $session_id)
  1772. ) {
  1773. $edit_actions = '<a href="group_edit.php?'.api_get_cidreq(
  1774. true,
  1775. false
  1776. ).'&gidReq='.$this_group['id'].'" title="'.get_lang('Edit').'">'.Display::return_icon(
  1777. 'edit.png',
  1778. get_lang('EditGroup'),
  1779. '',
  1780. ICON_SIZE_SMALL
  1781. ).'</a>&nbsp;';
  1782. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  1783. true,
  1784. false
  1785. ).'&category='.$category_id.'&amp;action=empty_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(
  1786. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1787. )."'".')) return false;" title="'.get_lang('EmptyGroup').'">'.
  1788. Display::return_icon('clean.png', get_lang('EmptyGroup'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1789. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  1790. true,
  1791. false
  1792. ).'&category='.$category_id.'&amp;action=fill_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(
  1793. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1794. )."'".')) return false;" title="'.get_lang('FillGroup').'">'.
  1795. Display::return_icon('fill.png', get_lang('FillGroup'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1796. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(
  1797. true,
  1798. false
  1799. ).'&category='.$category_id.'&amp;action=delete_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(
  1800. api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
  1801. )."'".')) return false;" title="'.get_lang('Delete').'">'.
  1802. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1803. $row[] = $edit_actions;
  1804. }
  1805. if (!empty($this_group['nbMember'])) {
  1806. $totalRegistered = $totalRegistered + $this_group['nbMember'];
  1807. }
  1808. $group_data[] = $row;
  1809. } // end loop
  1810. if (isset($_GET['show_all'])) {
  1811. $paging_options = array('per_page' => count($group_data));
  1812. } else {
  1813. $paging_options = array();
  1814. }
  1815. $table = new SortableTableFromArrayConfig($group_data, 1, 20, 'group_category_'.$category_id);
  1816. //$my_cat = isset($_GET['category']) ? Security::remove_XSS($_GET['category']) : null;
  1817. $table->set_additional_parameters(array('category' => $category_id));
  1818. $column = 0;
  1819. if (api_is_allowed_to_edit(false, true) and count($group_list) > 1) {
  1820. $table->set_header($column++, '', false);
  1821. }
  1822. $table->set_header($column++, get_lang('Groups'));
  1823. $table->set_header($column++, get_lang('GroupTutor'));
  1824. $table->set_header($column++, get_lang('Registered'), false);
  1825. if (!api_is_allowed_to_edit(false, true)) { // If self-registration allowed
  1826. $table->set_header($column++, get_lang('GroupSelfRegistration'), false);
  1827. }
  1828. if (api_is_allowed_to_edit(false, true)) { // Only for course administrator
  1829. $table->set_header($column++, get_lang('Modify'), false);
  1830. $form_actions = array();
  1831. $form_actions['fill_selected'] = get_lang('FillGroup');
  1832. $form_actions['empty_selected'] = get_lang('EmptyGroup');
  1833. $form_actions['delete_selected'] = get_lang('Delete');
  1834. if (count($group_list) > 1) {
  1835. $table->set_form_actions($form_actions, 'group');
  1836. }
  1837. }
  1838. $table->display();
  1839. }
  1840. }