groupmanager.lib.php 73 KB

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