groupmanager.lib.php 57 KB

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