groupmanager.lib.php 59 KB

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