groupmanager.lib.php 47 KB

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