groupmanager.lib.php 57 KB

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