groupmanager.lib.php 78 KB

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