groupmanager.lib.php 55 KB

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