groupmanager.lib.php 54 KB

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