groupmanager.lib.php 58 KB

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