groupmanager.lib.php 49 KB

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