groupmanager.lib.php 65 KB

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