groupmanager.lib.php 67 KB

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