groupmanager.lib.php 59 KB

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