groupmanager.lib.php 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'fileManage.lib.php';
  4. require_once 'fileUpload.lib.php';
  5. require_once 'document.lib.php';
  6. /**
  7. * This library contains some functions for group-management.
  8. * @author Bart Mollet
  9. * @package chamilo.library
  10. * @todo Add $course_code parameter to all functions. So this GroupManager can
  11. * be used outside a session.
  12. */
  13. class GroupManager
  14. {
  15. /* VIRTUAL_COURSE_CATEGORY:
  16. in this category groups are created based on the virtual course of a course*/
  17. const VIRTUAL_COURSE_CATEGORY = 1;
  18. /* DEFAULT_GROUP_CATEGORY:
  19. When group categories aren't available (platform-setting),
  20. all groups are created in this 'dummy'-category*/
  21. const DEFAULT_GROUP_CATEGORY = 2;
  22. /**
  23. * infinite
  24. */
  25. const INFINITE = 99999;
  26. /**
  27. * No limit on the number of users in a group
  28. */
  29. const MEMBER_PER_GROUP_NO_LIMIT = 0;
  30. /**
  31. * No limit on the number of groups per user
  32. */
  33. const GROUP_PER_MEMBER_NO_LIMIT = 0;
  34. /**
  35. * The tools of a group can have 3 states
  36. * - not available
  37. * - public
  38. * - private
  39. */
  40. const TOOL_NOT_AVAILABLE = 0;
  41. const TOOL_PUBLIC = 1;
  42. const TOOL_PRIVATE = 2;
  43. /**
  44. * Constants for the available group tools
  45. */
  46. const GROUP_TOOL_FORUM = 0;
  47. const GROUP_TOOL_DOCUMENTS = 1;
  48. const GROUP_TOOL_CALENDAR = 2;
  49. const GROUP_TOOL_ANNOUNCEMENT = 3;
  50. const GROUP_TOOL_WORK = 4;
  51. const GROUP_TOOL_WIKI = 5;
  52. const GROUP_TOOL_CHAT = 6;
  53. /**
  54. *
  55. */
  56. private function __construct()
  57. {
  58. }
  59. /**
  60. * @return array
  61. */
  62. public static function get_groups()
  63. {
  64. $table_group = Database :: get_course_table(TABLE_GROUP);
  65. $course_id = api_get_course_int_id();
  66. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id ";
  67. $result = Database::query($sql);
  68. return Database::store_result($result, 'ASSOC');
  69. }
  70. /**
  71. * Get list of groups for current course.
  72. * @param int $category The id of the category from which the groups are
  73. * requested
  74. * @param string $course_code Default is current course
  75. * @return array An array with all information about the groups.
  76. */
  77. public static function get_group_list($category = null, $course_code = null)
  78. {
  79. $my_user_id = api_get_user_id();
  80. $course_info = api_get_course_info($course_code);
  81. $course_id = $course_info['real_id'];
  82. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  83. $table_group = Database :: get_course_table(TABLE_GROUP);
  84. //condition for the session
  85. $session_id = api_get_session_id();
  86. $sql = "SELECT g.id,
  87. g.name,
  88. g.description,
  89. g.category_id,
  90. g.max_student maximum_number_of_members,
  91. g.secret_directory,
  92. g.self_registration_allowed,
  93. g.self_unregistration_allowed,
  94. g.session_id,
  95. ug.user_id is_member
  96. FROM $table_group g
  97. LEFT JOIN $table_group_user ug
  98. ON (
  99. ug.group_id = g.id AND
  100. ug.user_id = '".api_get_user_id()."' AND
  101. ug.c_id = $course_id AND
  102. g.c_id = $course_id
  103. )";
  104. $sql .= " WHERE 1=1 ";
  105. if ($category != null) {
  106. $sql .= " AND g.category_id = '".Database::escape_string($category)."' ";
  107. $session_condition = api_get_session_condition($session_id);
  108. if (!empty($session_condition)) {
  109. $sql .= $session_condition;
  110. }
  111. } else {
  112. $session_condition = api_get_session_condition($session_id, true);
  113. }
  114. $sql .= " AND g.c_id = $course_id ";
  115. if (!empty($session_condition)) {
  116. $sql .= $session_condition;
  117. }
  118. $sql .= " GROUP BY g.id ORDER BY UPPER(g.name)";
  119. $groupList = Database::query($sql);
  120. $groups = array();
  121. while ($thisGroup = Database::fetch_array($groupList)) {
  122. $thisGroup['number_of_members'] = count(self::get_subscribed_users($thisGroup['id']));
  123. if ($thisGroup['session_id'] != 0) {
  124. $sql = 'SELECT name FROM '.Database::get_main_table(TABLE_MAIN_SESSION).'
  125. WHERE id='.$thisGroup['session_id'];
  126. $rs_session = Database::query($sql);
  127. if (Database::num_rows($rs_session) > 0) {
  128. $thisGroup['session_name'] = Database::result($rs_session, 0, 0);
  129. }
  130. }
  131. $groups[] = $thisGroup;
  132. }
  133. return $groups;
  134. }
  135. /**
  136. * Create a group
  137. * @param string $name The name for this group
  138. * @param int $tutor The user-id of the group's tutor
  139. * @param int $places How many people can subscribe to the new group
  140. */
  141. public static function create_group($name, $category_id, $tutor, $places)
  142. {
  143. $_course = api_get_course_info();
  144. $table_group = Database :: get_course_table(TABLE_GROUP);
  145. $session_id = api_get_session_id();
  146. $course_id = api_get_course_int_id();
  147. $currentCourseRepository = $_course['path'];
  148. $category = self :: get_category($category_id);
  149. $places = intval($places);
  150. if ($places == 0) {
  151. //if the amount of users per group is not filled in, use the setting from the category
  152. $places = $category['max_student'];
  153. } else {
  154. if ($places > $category['max_student'] && $category['max_student'] != 0) {
  155. $places = $category['max_student'];
  156. }
  157. }
  158. $sql = "INSERT INTO ".$table_group." SET
  159. c_id = $course_id ,
  160. category_id='".Database::escape_string($category_id)."',
  161. max_student = '".$places."',
  162. doc_state = '".$category['doc_state']."',
  163. calendar_state = '".$category['calendar_state']."',
  164. work_state = '".$category['work_state']."',
  165. announcements_state = '".$category['announcements_state']."',
  166. forum_state = '".$category['forum_state']."',
  167. wiki_state = '".$category['wiki_state']."',
  168. chat_state = '".$category['chat_state']."',
  169. self_registration_allowed = '".$category['self_reg_allowed']."',
  170. self_unregistration_allowed = '".$category['self_unreg_allowed']."',
  171. session_id='".Database::escape_string($session_id)."'";
  172. Database::query($sql);
  173. $lastId = Database::insert_id();
  174. if ($lastId) {
  175. $desired_dir_name= '/'.replace_dangerous_char($name,'strict').'_groupdocs';
  176. $my_path = api_get_path(SYS_COURSE_PATH).$currentCourseRepository.'/document';
  177. $unique_name = create_unexisting_directory($_course, api_get_user_id(), $session_id, $lastId, null, $my_path, $desired_dir_name, null, 1);
  178. /* Stores the directory path into the group table */
  179. $sql = "UPDATE ".$table_group." SET name = '".Database::escape_string($name)."', secret_directory = '".$unique_name."'
  180. WHERE c_id = $course_id AND id ='".$lastId."'";
  181. Database::query($sql);
  182. // create a forum if needed
  183. if ($category['forum_state'] >= 0) {
  184. require_once api_get_path(SYS_CODE_PATH).'forum/forumconfig.inc.php';
  185. require_once api_get_path(SYS_CODE_PATH).'forum/forumfunction.inc.php';
  186. $forum_categories = get_forum_categories();
  187. $values = array();
  188. $values['forum_title'] = $name;
  189. $values['group_id'] = $lastId;
  190. $counter = 0;
  191. foreach ($forum_categories as $key=>$value) {
  192. if ($counter==0) {
  193. $forum_category_id = $key;
  194. }
  195. $counter++;
  196. }
  197. // A sanity check.
  198. if (empty($forum_category_id)) {
  199. $forum_category_id = 0;
  200. }
  201. $values['forum_category'] = $forum_category_id;
  202. $values['allow_anonymous_group']['allow_anonymous'] = 0;
  203. $values['students_can_edit_group']['students_can_edit'] = 0;
  204. $values['approval_direct_group']['approval_direct'] = 0;
  205. $values['allow_attachments_group']['allow_attachments'] = 1;
  206. $values['allow_new_threads_group']['allow_new_threads'] = 1;
  207. $values['default_view_type_group']['default_view_type']=api_get_setting('default_forum_view');
  208. $values['group_forum'] = $lastId;
  209. if ($category['forum_state'] == '1') {
  210. $values['public_private_group_forum_group']['public_private_group_forum']='public';
  211. } elseif ($category['forum_state'] == '2') {
  212. $values['public_private_group_forum_group']['public_private_group_forum']='private';
  213. } elseif ($category['forum_state'] == '0') {
  214. $values['public_private_group_forum_group']['public_private_group_forum']='unavailable';
  215. }
  216. store_forum($values);
  217. }
  218. }
  219. return $lastId;
  220. }
  221. /**
  222. * Create subgroups.
  223. * This function creates new groups based on an existing group. It will
  224. * create the specified number of groups and fill those groups with users
  225. * from the base group
  226. * @param int $group_id The group from which subgroups have to be created.
  227. * @param int $number_of_groups The number of groups that have to be created
  228. */
  229. public static function create_subgroups($group_id, $number_of_groups)
  230. {
  231. $course_id = api_get_course_int_id();
  232. $table_group = Database::get_course_table(TABLE_GROUP);
  233. $category_id = self::create_category(
  234. get_lang('Subgroups'),
  235. '',
  236. self::TOOL_PRIVATE,
  237. self::TOOL_PRIVATE,
  238. 0,
  239. 0,
  240. 1,
  241. 1
  242. );
  243. $users = self::get_users($group_id);
  244. $group_ids = array ();
  245. for ($group_nr = 1; $group_nr <= $number_of_groups; $group_nr ++) {
  246. $group_ids[] = self::create_group(
  247. get_lang('Subgroup').' '.$group_nr,
  248. $category_id,
  249. 0,
  250. 0
  251. );
  252. }
  253. $members = array();
  254. foreach ($users as $index => $user_id) {
  255. self::subscribe_users(
  256. $user_id,
  257. $group_ids[$index % $number_of_groups]
  258. );
  259. $members[$group_ids[$index % $number_of_groups]]++;
  260. }
  261. foreach ($members as $group_id => $places) {
  262. $sql = "UPDATE $table_group SET max_student = $places
  263. WHERE c_id = $course_id AND id = $group_id";
  264. Database::query($sql);
  265. }
  266. }
  267. /**
  268. * Create groups from all virtual courses in the given course.
  269. * @deprecated
  270. */
  271. public static function create_groups_from_virtual_courses()
  272. {
  273. self :: delete_category(self::VIRTUAL_COURSE_CATEGORY);
  274. $id = self :: create_category(get_lang('GroupsFromVirtualCourses'), '', self::TOOL_NOT_AVAILABLE, self::TOOL_NOT_AVAILABLE, 0, 0, 1, 1);
  275. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  276. $course_id = api_get_course_int_id();
  277. $sql = "UPDATE ".$table_group_cat." SET id=".self::VIRTUAL_COURSE_CATEGORY." WHERE c_id = $course_id AND id=$id";
  278. Database::query($sql);
  279. $course = api_get_course_info();
  280. $course['code'] = $course['sysCode'];
  281. $course['title'] = $course['name'];
  282. $virtual_courses = CourseManager :: get_virtual_courses_linked_to_real_course($course['sysCode']);
  283. $group_courses = $virtual_courses;
  284. $group_courses[] = $course;
  285. $ids = array ();
  286. foreach ($group_courses as $index => $group_course) {
  287. $users = CourseManager :: get_user_list_from_course_code($group_course['code']);
  288. $members = array ();
  289. foreach ($users as $index => $user) {
  290. if ($user['status'] == 5 && $user['tutor_id'] == 0) {
  291. $members[] = $user['user_id'];
  292. }
  293. }
  294. $id = self :: create_group($group_course['code'], self::VIRTUAL_COURSE_CATEGORY, 0, count($members));
  295. self :: subscribe_users($members, $id);
  296. $ids[] = $id;
  297. }
  298. return $ids;
  299. }
  300. /**
  301. * Create a group for every class subscribed to the current course
  302. * @param int $category_id The category in which the groups should be created
  303. * @return array
  304. */
  305. public static function create_class_groups($category_id)
  306. {
  307. $options['where'] = array(" usergroup.course_id = ? " => api_get_real_course_id());
  308. $obj = new UserGroup();
  309. $classes = $obj->get_usergroup_in_course($options);
  310. $group_ids = array();
  311. foreach ($classes as $class) {
  312. $users_ids = $obj->get_users_by_usergroup($class['id']);
  313. $group_id = self::create_group($class['name'],$category_id,0,count($users_ids));
  314. self::subscribe_users($users_ids,$group_id);
  315. $group_ids[] = $group_id;
  316. }
  317. return $group_ids;
  318. }
  319. /**
  320. * deletes groups and their data.
  321. * @author Christophe Gesche <christophe.gesche@claroline.net>
  322. * @author Hugues Peeters <hugues.peeters@claroline.net>
  323. * @author Bart Mollet
  324. * @param mixed $groupIdList - group(s) to delete. It can be a single id
  325. * (int) or a list of id (array).
  326. * @param string $course_code Default is current course
  327. * @return integer - number of groups deleted.
  328. */
  329. public static function delete_groups($group_ids, $course_code = null)
  330. {
  331. $course_info = api_get_course_info($course_code);
  332. $course_id = $course_info['real_id'];
  333. // Database table definitions
  334. $group_table = Database :: get_course_table(TABLE_GROUP);
  335. $forum_table = Database :: get_course_table(TABLE_FORUM);
  336. $group_ids = is_array($group_ids) ? $group_ids : array ($group_ids);
  337. $group_ids = array_map('intval',$group_ids);
  338. if (api_is_course_coach()) {
  339. //a coach can only delete courses from his session
  340. for ($i=0 ; $i<count($group_ids) ; $i++) {
  341. if (!api_is_element_in_the_session(TOOL_GROUP,$group_ids[$i])) {
  342. array_splice($group_ids,$i,1);
  343. $i--;
  344. }
  345. }
  346. if (count($group_ids) == 0) {
  347. return 0;
  348. }
  349. }
  350. // Unsubscribe all users
  351. self :: unsubscribe_all_users($group_ids);
  352. $sql = "SELECT id, secret_directory, session_id FROM $group_table
  353. WHERE c_id = $course_id AND id IN (".implode(' , ', $group_ids).")";
  354. $db_result = Database::query($sql);
  355. while ($group = Database::fetch_object($db_result)) {
  356. // move group-documents to garbage
  357. $source_directory = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$group->secret_directory;
  358. //File to renamed
  359. $destination_dir = api_get_path(SYS_COURSE_PATH).$course_info['path']."/document".$group->secret_directory.'_DELETED_'.$group->id;
  360. if (!empty($group->secret_directory)) {
  361. //Deleting from document tool
  362. DocumentManager::delete_document($course_info, $group->secret_directory, $source_directory);
  363. if (file_exists($source_directory)) {
  364. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  365. // Delete
  366. my_delete($source_directory);
  367. } else {
  368. // Rename
  369. rename($source_directory, $destination_dir);
  370. }
  371. }
  372. }
  373. }
  374. // delete the groups
  375. $sql = "DELETE FROM ".$group_table." WHERE c_id = $course_id AND id IN ('".implode("' , '", $group_ids)."')";
  376. Database::query($sql);
  377. $sql = "DELETE FROM ".$forum_table." WHERE c_id = $course_id AND forum_of_group IN ('".implode("' , '", $group_ids)."')";
  378. Database::query($sql);
  379. return Database::affected_rows();
  380. }
  381. /**
  382. * Get group properties
  383. * @param int $group_id The group from which properties are requested.
  384. * @return array All properties. Array-keys are:
  385. * name, tutor_id, description, maximum_number_of_students,
  386. * directory and visibility of tools
  387. */
  388. public static function get_group_properties($group_id)
  389. {
  390. $course_id = api_get_course_int_id();
  391. if (empty($group_id) or !is_integer(intval($group_id))) {
  392. return null;
  393. }
  394. $table_group = Database :: get_course_table(TABLE_GROUP);
  395. $sql = "SELECT * FROM $table_group WHERE c_id = $course_id AND id = ".intval($group_id);
  396. $db_result = Database::query($sql);
  397. $db_object = Database::fetch_object($db_result);
  398. $result = array();
  399. if ($db_object) {
  400. $result['id'] = $db_object->id;
  401. $result['name'] = $db_object->name;
  402. $result['tutor_id'] = isset($db_object->tutor_id) ? $db_object->tutor_id : null;
  403. $result['description'] = $db_object->description;
  404. $result['maximum_number_of_students'] = $db_object->max_student;
  405. $result['max_student'] = $db_object->max_student;
  406. $result['doc_state'] = $db_object->doc_state;
  407. $result['work_state'] = $db_object->work_state;
  408. $result['calendar_state'] = $db_object->calendar_state;
  409. $result['announcements_state'] = $db_object->announcements_state;
  410. $result['forum_state'] = $db_object->forum_state;
  411. $result['wiki_state'] = $db_object->wiki_state;
  412. $result['chat_state'] = $db_object->chat_state;
  413. $result['directory'] = $db_object->secret_directory;
  414. $result['self_registration_allowed'] = $db_object->self_registration_allowed;
  415. $result['self_unregistration_allowed'] = $db_object->self_unregistration_allowed;
  416. $result['count_users'] = count(
  417. self::get_subscribed_users($group_id)
  418. );
  419. $result['count_tutor'] = count(
  420. self::get_subscribed_tutors($group_id)
  421. );
  422. $result['count_all'] = $result['count_users'] + $result['count_tutor'];
  423. }
  424. return $result;
  425. }
  426. /**
  427. * @param string $name
  428. * @param string $courseCode
  429. * @return array
  430. */
  431. public static function getGroupByName($name, $courseCode = null)
  432. {
  433. $name = trim($name);
  434. if (empty($name)) {
  435. return array();
  436. }
  437. $course_info = api_get_course_info($courseCode);
  438. $course_id = $course_info['real_id'];
  439. $name = Database::escape_string($name);
  440. $table_group = Database::get_course_table(TABLE_GROUP);
  441. $sql = "SELECT * FROM $table_group
  442. WHERE c_id = $course_id AND name = '$name' LIMIT 1";
  443. $res = Database::query($sql);
  444. $group = array();
  445. if (Database::num_rows($res)) {
  446. $group = Database::fetch_array($res, 'ASSOC');
  447. }
  448. return $group;
  449. }
  450. /**
  451. * @param int $courseId
  452. * @param int $categoryId
  453. * @param string $name
  454. * @return array
  455. */
  456. public static function getGroupListFilterByName($name, $categoryId, $courseId)
  457. {
  458. $name = trim($name);
  459. if (empty($name)) {
  460. return array();
  461. }
  462. $name = Database::escape_string($name);
  463. $courseId = intval($courseId);
  464. $table_group = Database::get_course_table(TABLE_GROUP);
  465. $sql = "SELECT * FROM $table_group
  466. WHERE c_id = $courseId AND name LIKE '%$name%'";
  467. if (!empty($categoryId)) {
  468. $categoryId = intval($categoryId);
  469. $sql .= " AND category_id = $categoryId";
  470. }
  471. $sql .= " ORDER BY name";
  472. $result = Database::query($sql);
  473. return Database::store_result($result, 'ASSOC');
  474. }
  475. /**
  476. * Set group properties
  477. * Changes the group's properties.
  478. * @param int Group Id
  479. * @param string Group name
  480. * @param string Group description
  481. * @param int Max number of students in group
  482. * @param int Document tool's visibility (0=none,1=private,2=public)
  483. * @param int Work tool's visibility (0=none,1=private,2=public)
  484. * @param int Calendar tool's visibility (0=none,1=private,2=public)
  485. * @param int Announcement tool's visibility (0=none,1=private,2=public)
  486. * @param int Forum tool's visibility (0=none,1=private,2=public)
  487. * @param int Wiki tool's visibility (0=none,1=private,2=public)
  488. * @param int Chat tool's visibility (0=none,1=private,2=public)
  489. * @param bool Whether self registration is allowed or not
  490. * @param bool Whether self unregistration is allowed or not
  491. * @param int $categoryId
  492. * @return bool TRUE if properties are successfully changed, false otherwise
  493. */
  494. public static function set_group_properties(
  495. $group_id,
  496. $name,
  497. $description,
  498. $maximum_number_of_students,
  499. $doc_state,
  500. $work_state,
  501. $calendar_state,
  502. $announcements_state,
  503. $forum_state,
  504. $wiki_state,
  505. $chat_state,
  506. $self_registration_allowed,
  507. $self_unregistration_allowed,
  508. $categoryId = null
  509. ) {
  510. $table_group = Database :: get_course_table(TABLE_GROUP);
  511. $table_forum = Database :: get_course_table(TABLE_FORUM);
  512. $categoryId = intval($categoryId);
  513. $group_id = Database::escape_string($group_id);
  514. $course_id = api_get_course_int_id();
  515. $sql = "UPDATE ".$table_group." SET
  516. name='".Database::escape_string(trim($name))."',
  517. doc_state = '".Database::escape_string($doc_state)."',
  518. work_state = '".Database::escape_string($work_state)."',
  519. calendar_state = '".Database::escape_string($calendar_state)."',
  520. announcements_state = '".Database::escape_string($announcements_state)."',
  521. forum_state = '".Database::escape_string($forum_state)."',
  522. wiki_state = '".Database::escape_string($wiki_state)."',
  523. chat_state = '".Database::escape_string($chat_state)."',
  524. description ='".Database::escape_string(trim($description))."',
  525. max_student = '".Database::escape_string($maximum_number_of_students)."',
  526. self_registration_allowed = '".Database::escape_string($self_registration_allowed)."',
  527. self_unregistration_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  528. category_id = '".Database::escape_string($categoryId)."'
  529. WHERE c_id = $course_id AND id=".$group_id;
  530. $result = Database::query($sql);
  531. /* Here we are updating a field in the table forum_forum that perhaps
  532. duplicates the table group_info.forum_state cvargas*/
  533. $forum_state = (int) $forum_state;
  534. $sql2 = "UPDATE ".$table_forum." SET ";
  535. if ($forum_state === 1) {
  536. $sql2 .= " forum_group_public_private='public' ";
  537. } elseif ($forum_state === 2) {
  538. $sql2 .= " forum_group_public_private='private' ";
  539. } elseif ($forum_state === 0) {
  540. $sql2 .= " forum_group_public_private='unavailable' ";
  541. }
  542. $sql2 .=" WHERE c_id = $course_id AND forum_of_group=".$group_id;
  543. Database::query($sql2);
  544. return $result;
  545. }
  546. /**
  547. * Get the total number of groups for the current course.
  548. * @return int The number of groups for the current course.
  549. */
  550. public static function get_number_of_groups()
  551. {
  552. $course_id = api_get_course_int_id();
  553. $table_group = Database :: get_course_table(TABLE_GROUP);
  554. $sql = "SELECT COUNT(id) AS number_of_groups FROM $table_group WHERE c_id = $course_id ";
  555. $res = Database::query($sql);
  556. $obj = Database::fetch_object($res);
  557. return $obj->number_of_groups;
  558. }
  559. /**
  560. * Get all categories
  561. * @param string $course_code The course (default = current course)
  562. * @return array
  563. */
  564. public static function get_categories($course_code = null)
  565. {
  566. $course_info = api_get_course_info($course_code);
  567. $course_id = $course_info['real_id'];
  568. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  569. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id ORDER BY display_order";
  570. $res = Database::query($sql);
  571. $cats = array ();
  572. while ($cat = Database::fetch_array($res)) {
  573. $cats[] = $cat;
  574. }
  575. return $cats;
  576. }
  577. /**
  578. * Get a group category
  579. * @param int $id The category id
  580. * @param string $course_code The course (default = current course)
  581. * @return array
  582. */
  583. public static function get_category($id, $course_code = null)
  584. {
  585. if (empty($id)) {
  586. return array();
  587. }
  588. $course_info = api_get_course_info($course_code);
  589. $course_id = $course_info['real_id'];
  590. $id = Database::escape_string($id);
  591. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  592. $sql = "SELECT * FROM $table_group_cat WHERE c_id = $course_id AND id = $id LIMIT 1";
  593. $res = Database::query($sql);
  594. return Database::fetch_array($res);
  595. }
  596. /**
  597. * Get a group category
  598. * @param string $title
  599. * @param string $course_code The course (default = current course)
  600. * @return array
  601. */
  602. public static function getCategoryByTitle($title, $course_code = null)
  603. {
  604. $title = trim($title);
  605. if (empty($title)) {
  606. return array();
  607. }
  608. $course_info = api_get_course_info($course_code);
  609. $course_id = $course_info['real_id'];
  610. $title = Database::escape_string($title);
  611. $table_group_cat = Database::get_course_table(TABLE_GROUP_CATEGORY);
  612. $sql = "SELECT * FROM $table_group_cat
  613. WHERE c_id = $course_id AND title = '$title' LIMIT 1";
  614. $res = Database::query($sql);
  615. $category = array();
  616. if (Database::num_rows($res)) {
  617. $category = Database::fetch_array($res, 'ASSOC');
  618. }
  619. return $category;
  620. }
  621. /**
  622. * Get the unique category of a given group
  623. * @param int $group_id The id of the group
  624. * @param string $course_code The course in which the group is (default =
  625. * current course)
  626. * @return array The category
  627. */
  628. public static function get_category_from_group($group_id, $course_code = null)
  629. {
  630. $table_group = Database :: get_course_table(TABLE_GROUP);
  631. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  632. if (empty($group_id)) {
  633. return array();
  634. }
  635. $course_info = api_get_course_info($course_code);
  636. $course_id = $course_info['real_id'];
  637. $group_id = Database::escape_string($group_id);
  638. $sql = "SELECT gc.* FROM $table_group_cat gc, $table_group g
  639. WHERE gc.c_id = $course_id AND
  640. g.c_id = $course_id AND
  641. gc.id = g.category_id AND g.id= $group_id LIMIT 1";
  642. $res = Database::query($sql);
  643. $cat = array();
  644. if (Database::num_rows($res)) {
  645. $cat = Database::fetch_array($res);
  646. }
  647. return $cat;
  648. }
  649. /**
  650. * Delete a group category
  651. * @param int $cat_id The id of the category to delete
  652. * @param string $course_code The code in which the category should be
  653. * deleted (default = current course)
  654. */
  655. public static function delete_category($cat_id, $course_code = null)
  656. {
  657. $course_info = api_get_course_info($course_code);
  658. $course_id = $course_info['real_id'];
  659. $table_group = Database :: get_course_table(TABLE_GROUP);
  660. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  661. $cat_id = Database::escape_string($cat_id);
  662. $sql = "SELECT id FROM $table_group WHERE c_id = $course_id AND category_id='".$cat_id."'";
  663. $res = Database::query($sql);
  664. if (Database::num_rows($res) > 0) {
  665. $groups_to_delete = array ();
  666. while ($group = Database::fetch_object($res)) {
  667. $groups_to_delete[] = $group->id;
  668. }
  669. self :: delete_groups($groups_to_delete);
  670. }
  671. $sql = "DELETE FROM $table_group_cat WHERE c_id = $course_id AND id='".$cat_id."'";
  672. Database::query($sql);
  673. }
  674. /**
  675. * Create group category
  676. * @param string $title The title of the new category
  677. * @param string $description The description of the new category
  678. * @param bool $self_registration_allowed
  679. * @param bool $self_unregistration_allowed
  680. * @param int $max_number_of_students
  681. * @param int $groups_per_user
  682. */
  683. public static function create_category(
  684. $title,
  685. $description,
  686. $doc_state,
  687. $work_state,
  688. $calendar_state,
  689. $announcements_state,
  690. $forum_state,
  691. $wiki_state,
  692. $chat_state = 1,
  693. $self_registration_allowed = 0,
  694. $self_unregistration_allowed = 0,
  695. $maximum_number_of_students = 8,
  696. $groups_per_user = 0
  697. ) {
  698. if (empty($title)) {
  699. return false;
  700. }
  701. $table_group_category = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  702. $course_id = api_get_course_int_id();
  703. $sql = "SELECT MAX(display_order)+1 as new_order FROM $table_group_category WHERE c_id = $course_id ";
  704. $res = Database::query($sql);
  705. $obj = Database::fetch_object($res);
  706. if (!isset ($obj->new_order)) {
  707. $obj->new_order = 1;
  708. }
  709. $sql = "INSERT INTO ".$table_group_category." SET
  710. c_id = $course_id ,
  711. title='".Database::escape_string($title)."',
  712. display_order ='".$obj->new_order."',
  713. description='".Database::escape_string($description)."',
  714. doc_state = '".Database::escape_string($doc_state)."',
  715. work_state = '".Database::escape_string($work_state)."',
  716. calendar_state = '".Database::escape_string($calendar_state)."',
  717. announcements_state = '".Database::escape_string($announcements_state)."',
  718. forum_state = '".Database::escape_string($forum_state)."',
  719. wiki_state = '".Database::escape_string($wiki_state)."',
  720. chat_state = '".Database::escape_string($chat_state)."',
  721. groups_per_user = '".Database::escape_string($groups_per_user)."',
  722. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  723. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  724. max_student = '".Database::escape_string($maximum_number_of_students)."' ";
  725. Database::query($sql);
  726. $categoryId = Database::insert_id();
  727. if ($categoryId == self::VIRTUAL_COURSE_CATEGORY) {
  728. $sql = "UPDATE ".$table_group_category." SET id = ". ($categoryId +1)." WHERE c_id = $course_id AND id = $categoryId";
  729. Database::query($sql);
  730. return $categoryId +1;
  731. }
  732. return $categoryId;
  733. }
  734. /**
  735. * Update group category
  736. *
  737. * @param int $id
  738. * @param string $title
  739. * @param string $description
  740. * @param $doc_state
  741. * @param $work_state
  742. * @param $calendar_state
  743. * @param $announcements_state
  744. * @param $forum_state
  745. * @param $wiki_state
  746. * @param $chat_state
  747. * @param $self_registration_allowed
  748. * @param $self_unregistration_allowed
  749. * @param $maximum_number_of_students
  750. * @param $groups_per_user
  751. */
  752. public static function update_category(
  753. $id,
  754. $title,
  755. $description,
  756. $doc_state,
  757. $work_state,
  758. $calendar_state,
  759. $announcements_state,
  760. $forum_state,
  761. $wiki_state,
  762. $chat_state,
  763. $self_registration_allowed,
  764. $self_unregistration_allowed,
  765. $maximum_number_of_students,
  766. $groups_per_user
  767. ) {
  768. $table_group_category = Database::get_course_table(TABLE_GROUP_CATEGORY);
  769. $id = Database::escape_string($id);
  770. $course_id = api_get_course_int_id();
  771. $sql = "UPDATE ".$table_group_category." SET
  772. title='".Database::escape_string($title)."',
  773. description='".Database::escape_string($description)."',
  774. doc_state = '".Database::escape_string($doc_state)."',
  775. work_state = '".Database::escape_string($work_state)."',
  776. calendar_state = '".Database::escape_string($calendar_state)."',
  777. announcements_state = '".Database::escape_string($announcements_state)."',
  778. forum_state = '".Database::escape_string($forum_state)."',
  779. wiki_state = '".Database::escape_string($wiki_state)."',
  780. chat_state = '".Database::escape_string($chat_state)."',
  781. groups_per_user = '".Database::escape_string($groups_per_user)."',
  782. self_reg_allowed = '".Database::escape_string($self_registration_allowed)."',
  783. self_unreg_allowed = '".Database::escape_string($self_unregistration_allowed)."',
  784. max_student = ".Database::escape_string($maximum_number_of_students)."
  785. WHERE c_id = $course_id AND id = $id";
  786. Database::query($sql);
  787. // Updating all groups inside this category
  788. $groups = self::get_group_list($id);
  789. if (!empty($groups)) {
  790. foreach ($groups as $group) {
  791. GroupManager::set_group_properties(
  792. $group['id'],
  793. $group['name'],
  794. $group['description'],
  795. $maximum_number_of_students,
  796. $doc_state,
  797. $work_state,
  798. $calendar_state,
  799. $announcements_state,
  800. $forum_state,
  801. $wiki_state,
  802. $chat_state,
  803. $self_registration_allowed,
  804. $self_unregistration_allowed,
  805. $id
  806. );
  807. }
  808. }
  809. }
  810. /**
  811. * Returns the number of groups of the user with the greatest number of
  812. * subscriptions in the given category
  813. */
  814. public static function get_current_max_groups_per_user($category_id = null, $course_code = null)
  815. {
  816. $course_info = api_get_course_info ($course_code);
  817. $group_table = Database :: get_course_table(TABLE_GROUP);
  818. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  819. $sql = 'SELECT COUNT(gu.group_id) AS current_max FROM '.$group_user_table.' gu, '.$group_table.' g
  820. WHERE g.c_id = '.$course_info['real_id'].'
  821. AND gu.c_id = g.c_id
  822. AND gu.group_id = g.id ';
  823. if ($category_id != null) {
  824. $category_id = Database::escape_string($category_id);
  825. $sql .= ' AND g.category_id = '.$category_id;
  826. }
  827. $sql .= ' GROUP BY gu.user_id ORDER BY current_max DESC LIMIT 1';
  828. $res = Database::query($sql);
  829. $obj = Database::fetch_object($res);
  830. return $obj->current_max;
  831. }
  832. /**
  833. * Swaps the display-order of two categories
  834. * @param int $id1 The id of the first category
  835. * @param int $id2 The id of the second category
  836. */
  837. public static function swap_category_order($id1, $id2)
  838. {
  839. $table_group_cat = Database :: get_course_table(TABLE_GROUP_CATEGORY);
  840. $id1 = Database::escape_string($id1);
  841. $id2 = Database::escape_string($id2);
  842. $course_id = api_get_course_int_id();
  843. $sql = "SELECT id,display_order FROM $table_group_cat WHERE id IN ($id1,$id2) AND c_id = $course_id ";
  844. $res = Database::query($sql);
  845. $cat1 = Database::fetch_object($res);
  846. $cat2 = Database::fetch_object($res);
  847. $sql = "UPDATE $table_group_cat SET display_order=$cat2->display_order WHERE id = $cat1->id AND c_id = $course_id ";
  848. Database::query($sql);
  849. $sql = "UPDATE $table_group_cat SET display_order=$cat1->display_order WHERE id = $cat2->id AND c_id = $course_id ";
  850. Database::query($sql);
  851. }
  852. /**
  853. * Get all users from a given group
  854. * @param int $group_id The group
  855. * @param bool $load_extra_info
  856. * @param int $start
  857. * @param int $limit
  858. * @param bool $getCount
  859. * @param int $courseId
  860. * @return array list of user id
  861. */
  862. public static function get_users(
  863. $group_id,
  864. $load_extra_info = false,
  865. $start = null,
  866. $limit = null,
  867. $getCount = false,
  868. $courseId = null,
  869. $column = null,
  870. $direction = null
  871. ) {
  872. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  873. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  874. $group_id = Database::escape_string($group_id);
  875. if (empty($courseId)) {
  876. $courseId = api_get_course_int_id();
  877. } else {
  878. $courseId = intval($courseId);
  879. }
  880. $select = " SELECT g.user_id, firstname, lastname ";
  881. if ($getCount) {
  882. $select = " SELECT count(u.user_id) count";
  883. }
  884. $sql = "$select
  885. FROM $group_user_table g
  886. INNER JOIN $user_table u
  887. ON (u.user_id = g.user_id)
  888. WHERE c_id = $courseId AND g.group_id = $group_id";
  889. if (!empty($column) && !empty($direction)) {
  890. $column = Database::escape_string($column);
  891. $direction = Database::escape_string($direction);
  892. $sql .= " ORDER BY $column $direction";
  893. }
  894. if (!empty($start) && !empty($limit)) {
  895. $start = intval($start);
  896. $limit = intval($limit);
  897. $sql .= " LIMIT $start, $limit";
  898. }
  899. $res = Database::query($sql);
  900. $users = array();
  901. while ($obj = Database::fetch_object($res)) {
  902. if ($getCount) {
  903. return $obj->count;
  904. break;
  905. }
  906. if ($load_extra_info) {
  907. $users[] = api_get_user_info($obj->user_id);
  908. } else {
  909. $users[] = $obj->user_id;
  910. }
  911. }
  912. return $users;
  913. }
  914. /**
  915. * @param int $group_id
  916. * @return array
  917. */
  918. public static function getStudentsAndTutors($group_id)
  919. {
  920. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  921. $tutor_user_table = Database :: get_course_table(TABLE_GROUP_TUTOR);
  922. $course_id = api_get_course_int_id();
  923. $group_id = intval($group_id);
  924. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  925. $res = Database::query($sql);
  926. $users = array();
  927. while ($obj = Database::fetch_object($res)) {
  928. $users[] = api_get_user_info($obj->user_id);
  929. }
  930. $sql = "SELECT user_id FROM $tutor_user_table WHERE c_id = $course_id AND group_id = $group_id";
  931. $res = Database::query($sql);
  932. while ($obj = Database::fetch_object($res)) {
  933. $users[] = api_get_user_info($obj->user_id);
  934. }
  935. return $users;
  936. }
  937. /**
  938. * Get only tutors from a group
  939. * @param int $group_id
  940. * @return array
  941. */
  942. public static function getTutors($group_id)
  943. {
  944. $tutor_user_table = Database :: get_course_table(TABLE_GROUP_TUTOR);
  945. $course_id = api_get_course_int_id();
  946. $group_id = intval($group_id);
  947. $sql = "SELECT user_id FROM $tutor_user_table WHERE c_id = $course_id AND group_id = $group_id";
  948. $res = Database::query($sql);
  949. while ($obj = Database::fetch_object($res)) {
  950. $users[] = api_get_user_info($obj->user_id);
  951. }
  952. return $users;
  953. }
  954. /**
  955. * Get only students from a group (not tutors)
  956. * @param int $group_id
  957. * @return array
  958. */
  959. public static function getStudents($group_id)
  960. {
  961. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  962. $course_id = api_get_course_int_id();
  963. $group_id = intval($group_id);
  964. $sql = "SELECT user_id FROM $group_user_table WHERE c_id = $course_id AND group_id = $group_id";
  965. $res = Database::query($sql);
  966. $users = array();
  967. while ($obj = Database::fetch_object($res)) {
  968. $users[] = api_get_user_info($obj->user_id);
  969. }
  970. return $users;
  971. }
  972. /**
  973. * Returns users belonging to any of the group
  974. *
  975. * @param array $groups list of group ids
  976. * @return array list of user ids
  977. */
  978. public static function get_groups_users($groups = array())
  979. {
  980. $result = array();
  981. $tbl_group_user = Database::get_course_table(TABLE_GROUP_USER);
  982. $course_id = api_get_course_int_id();
  983. $groups = array_map('intval', $groups);
  984. // protect individual elements with surrounding quotes
  985. $groups = implode(', ', $groups);
  986. $sql = "SELECT DISTINCT user_id
  987. FROM $tbl_group_user gu
  988. WHERE c_id = $course_id AND gu.group_id IN ($groups)";
  989. $rs = Database::query($sql);
  990. while ($row = Database::fetch_array($rs)) {
  991. $result[] = $row['user_id'];
  992. }
  993. return $result;
  994. }
  995. /**
  996. * Fill the groups with students.
  997. * The algorithm takes care to first fill the groups with the least # of users.
  998. * Analysis
  999. * There was a problem with the "ALL" setting.
  1000. * When max # of groups is set to all, the value is sometimes NULL and sometimes ALL
  1001. * and in both cased the query does not work as expected.
  1002. * Stupid solution (currently implemented: set ALL to a big number (INFINITE) and things are solved :)
  1003. * Better solution: that's up to you.
  1004. *
  1005. * Note
  1006. * Throughout Dokeos there is some confusion about "course id" and "course code"
  1007. * The code is e.g. TEST101, but sometimes a variable that is called courseID also contains a course code string.
  1008. * However, there is also a integer course_id that uniquely identifies the course.
  1009. * ywarnier:> Now the course_id has been removed (25/1/2005)
  1010. * The databases are als very inconsistent in this.
  1011. *
  1012. * @author Chrisptophe Gesche <christophe.geshe@claroline.net>,
  1013. * Hugues Peeters <hugues.peeters@claroline.net> - original version
  1014. * @author Roan Embrechts - virtual course support, code cleaning
  1015. * @author Bart Mollet - code cleaning, use other GroupManager-functions
  1016. * @return void
  1017. */
  1018. public static function fill_groups($group_ids)
  1019. {
  1020. $_course = api_get_course_info();
  1021. $group_ids = is_array($group_ids) ? $group_ids : array ($group_ids);
  1022. $group_ids = array_map('intval', $group_ids);
  1023. if (api_is_course_coach()) {
  1024. for ($i=0 ; $i< count($group_ids) ; $i++) {
  1025. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])){
  1026. array_splice($group_ids,$i,1);
  1027. $i--;
  1028. }
  1029. }
  1030. if (count($group_ids)==0) {
  1031. return false;
  1032. }
  1033. }
  1034. $category = self::get_category_from_group($group_ids[0]);
  1035. $groups_per_user = $category['groups_per_user'];
  1036. $group_table = Database :: get_course_table(TABLE_GROUP);
  1037. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  1038. $session_id = api_get_session_id();
  1039. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['code'], true, $session_id);
  1040. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  1041. /*
  1042. * Retrieve all the groups where enrollment is still allowed
  1043. * (reverse) ordered by the number of place available
  1044. */
  1045. $course_id = api_get_course_int_id();
  1046. $sql = "SELECT g.id gid, g.max_student-count(ug.user_id) nbPlaces, g.max_student
  1047. FROM ".$group_table." g
  1048. LEFT JOIN ".$group_user_table." ug ON
  1049. g.c_id = $course_id AND ug.c_id = $course_id AND g.id = ug.group_id
  1050. WHERE
  1051. g.id IN (".implode(',', $group_ids).")
  1052. GROUP BY (g.id)
  1053. HAVING (nbPlaces > 0 OR g.max_student = ".self::MEMBER_PER_GROUP_NO_LIMIT.")
  1054. ORDER BY nbPlaces DESC";
  1055. $sql_result = Database::query($sql);
  1056. $group_available_place = array();
  1057. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  1058. $group_available_place[$group['gid']] = $group['nbPlaces'];
  1059. }
  1060. /*
  1061. * Retrieve course users (reverse) ordered by the number
  1062. * of group they are already enrolled
  1063. */
  1064. for ($i = 0; $i < count($complete_user_list); $i ++) {
  1065. //find # of groups the user is enrolled in
  1066. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"], $category['id']);
  1067. //add # of groups to user list
  1068. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  1069. }
  1070. //first sort by user_id to filter out duplicates
  1071. $complete_user_list = TableSort :: sort_table($complete_user_list, 'user_id');
  1072. $complete_user_list = self :: filter_duplicates($complete_user_list, 'user_id');
  1073. $complete_user_list = self :: filter_only_students($complete_user_list);
  1074. //now sort by # of group left
  1075. $complete_user_list = TableSort :: sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  1076. $userToken = array ();
  1077. foreach ($complete_user_list as $this_user) {
  1078. if ($this_user['number_groups_left'] > 0) {
  1079. $userToken[$this_user['user_id']] = $this_user['number_groups_left'];
  1080. }
  1081. }
  1082. $changed = true;
  1083. while ($changed) {
  1084. $changed = false;
  1085. reset($group_available_place);
  1086. arsort($group_available_place);
  1087. reset($userToken);
  1088. arsort($userToken);
  1089. foreach ($group_available_place as $group_id => $place) {
  1090. foreach ($userToken as $user_id => $places) {
  1091. if (self :: can_user_subscribe($user_id, $group_id)) {
  1092. self :: subscribe_users($user_id, $group_id);
  1093. $group_available_place[$group_id]--;
  1094. unset($userToken[$user_id]);
  1095. $changed = true;
  1096. break;
  1097. }
  1098. }
  1099. if ($changed) {
  1100. break;
  1101. }
  1102. }
  1103. }
  1104. }
  1105. /**
  1106. * Get the number of students in a group.
  1107. * @param int $group_id
  1108. * @return int Number of students in the given group.
  1109. */
  1110. public static function number_of_students($group_id, $course_id = null)
  1111. {
  1112. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1113. $group_id = Database::escape_string($group_id);
  1114. if (empty($course_id)) {
  1115. $course_id = api_get_course_int_id();
  1116. } else {
  1117. $course_id = intval($course_id);
  1118. }
  1119. $sql = "SELECT COUNT(*) AS number_of_students FROM $table_group_user WHERE c_id = $course_id AND group_id = $group_id";
  1120. $db_result = Database::query($sql);
  1121. $db_object = Database::fetch_object($db_result);
  1122. return $db_object->number_of_students;
  1123. }
  1124. /**
  1125. * Maximum number of students in a group
  1126. * @param int $group_id
  1127. * @return int Maximum number of students in the given group.
  1128. */
  1129. public static function maximum_number_of_students($group_id)
  1130. {
  1131. $table_group = Database :: get_course_table(TABLE_GROUP);
  1132. $group_id = Database::escape_string($group_id);
  1133. $course_id = api_get_course_int_id();
  1134. $db_result = Database::query("SELECT max_student FROM $table_group WHERE c_id = $course_id AND id = $group_id");
  1135. $db_object = Database::fetch_object($db_result);
  1136. if ($db_object->max_student == 0) {
  1137. return self::INFINITE;
  1138. }
  1139. return $db_object->max_student;
  1140. }
  1141. /**
  1142. * Number of groups of a user
  1143. * @param int $user_id
  1144. * @return int The number of groups the user is subscribed in.
  1145. */
  1146. public static function user_in_number_of_groups($user_id, $cat_id = null)
  1147. {
  1148. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1149. $table_group = Database :: get_course_table(TABLE_GROUP);
  1150. $user_id = Database::escape_string($user_id);
  1151. $cat_id = Database::escape_string($cat_id);
  1152. $course_id = api_get_course_int_id();
  1153. $cat_condition = '';
  1154. if (!empty($cat_id)) {
  1155. $cat_condition = " AND g.category_id = $cat_id ";
  1156. }
  1157. $sql = "SELECT COUNT(*) AS number_of_groups FROM $table_group_user gu, $table_group g
  1158. WHERE gu.c_id = $course_id AND
  1159. g.c_id = $course_id AND
  1160. gu.user_id = $user_id AND
  1161. g.id = gu.group_id $cat_condition";
  1162. $db_result = Database::query($sql);
  1163. $db_object = Database::fetch_object($db_result);
  1164. return $db_object->number_of_groups;
  1165. }
  1166. /**
  1167. * Is sef-registration allowed?
  1168. * @param int $user_id
  1169. * @param int $group_id
  1170. * @return bool TRUE if self-registration is allowed in the given group.
  1171. */
  1172. public static function is_self_registration_allowed($user_id, $group_id)
  1173. {
  1174. $course_id = api_get_course_int_id();
  1175. if (!$user_id > 0) {
  1176. return false;
  1177. }
  1178. $table_group = Database :: get_course_table(TABLE_GROUP);
  1179. $group_id = intval($group_id);
  1180. if (isset($group_id)) {
  1181. $sql = "SELECT self_registration_allowed FROM $table_group WHERE c_id = $course_id AND id = $group_id";
  1182. $db_result = Database::query($sql);
  1183. $db_object = Database::fetch_object($db_result);
  1184. return $db_object->self_registration_allowed == 1 && self :: can_user_subscribe($user_id, $group_id);
  1185. } else {
  1186. return false;
  1187. }
  1188. }
  1189. /**
  1190. * Is sef-unregistration allowed?
  1191. * @param int $user_id
  1192. * @param int $group_id
  1193. * @return bool TRUE if self-unregistration is allowed in the given group.
  1194. */
  1195. public static function is_self_unregistration_allowed($user_id, $group_id)
  1196. {
  1197. if (!$user_id > 0) {
  1198. return false;
  1199. }
  1200. $table_group = Database :: get_course_table(TABLE_GROUP);
  1201. $group_id = Database::escape_string($group_id);
  1202. $course_id = api_get_course_int_id();
  1203. $db_result = Database::query(
  1204. 'SELECT self_unregistration_allowed FROM '.$table_group.' WHERE c_id = '.$course_id.' AND id = '.$group_id
  1205. );
  1206. $db_object = Database::fetch_object($db_result);
  1207. return $db_object->self_unregistration_allowed == 1 && self :: can_user_unsubscribe($user_id, $group_id);
  1208. }
  1209. /**
  1210. * Is user subscribed in group?
  1211. * @param int $user_id
  1212. * @param int $group_id
  1213. * @return bool TRUE if given user is subscribed in given group
  1214. */
  1215. public static function is_subscribed($user_id, $group_id)
  1216. {
  1217. if (empty($user_id) or empty($group_id)) {
  1218. return false;
  1219. }
  1220. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1221. $group_id = Database::escape_string($group_id);
  1222. $user_id = Database::escape_string($user_id);
  1223. $course_id = api_get_course_int_id();
  1224. $sql = 'SELECT 1 FROM '.$table_group_user.'
  1225. WHERE
  1226. c_id = '.$course_id.' AND
  1227. group_id = '.$group_id.' AND
  1228. user_id = '.$user_id;
  1229. $result = Database::query($sql);
  1230. return Database::num_rows($result) > 0;
  1231. }
  1232. /**
  1233. * Can a user subscribe to a specified group in a course
  1234. * @param int $user_id
  1235. * @param int $group_id
  1236. * @return bool TRUE if given user can be subscribed in given group
  1237. */
  1238. public static function can_user_subscribe($user_id, $group_id)
  1239. {
  1240. $category = self :: get_category_from_group($group_id);
  1241. $result = !self :: is_subscribed($user_id, $group_id);
  1242. $result &= (self :: number_of_students($group_id) < self :: maximum_number_of_students($group_id));
  1243. if ($category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT) {
  1244. $category['groups_per_user'] = self::INFINITE;
  1245. }
  1246. $result &= (self :: user_in_number_of_groups($user_id, $category['id']) < $category['groups_per_user']);
  1247. $result &= !self :: is_tutor_of_group($user_id, $group_id);
  1248. return $result;
  1249. }
  1250. /**
  1251. * Can a user unsubscribe to a specified group in a course
  1252. * @param int $user_id
  1253. * @param int $group_id
  1254. * @return bool TRUE if given user can be unsubscribed from given group
  1255. * @internal for now, same as GroupManager::is_subscribed($user_id,$group_id)
  1256. */
  1257. public static function can_user_unsubscribe($user_id, $group_id)
  1258. {
  1259. $result = self :: is_subscribed($user_id, $group_id);
  1260. return $result;
  1261. }
  1262. /**
  1263. * Get all subscribed users (members) from a group
  1264. * @param int $group_id
  1265. * @return array An array with information of all users from the given group.
  1266. * (user_id, firstname, lastname, email)
  1267. */
  1268. public static function get_subscribed_users($group_id)
  1269. {
  1270. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1271. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1272. $order_clause = api_sort_by_first_name() ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1273. if (empty($group_id)) {
  1274. return array();
  1275. }
  1276. $group_id = intval($group_id);
  1277. $course_id = api_get_course_int_id();
  1278. $sql = "SELECT ug.id, u.user_id, u.lastname, u.firstname, u.email, u.username
  1279. FROM $table_user u INNER JOIN $table_group_user ug
  1280. ON (ug.user_id = u.user_id)
  1281. WHERE ug.c_id = $course_id AND
  1282. ug.group_id = $group_id
  1283. $order_clause";
  1284. $db_result = Database::query($sql);
  1285. $users = array();
  1286. while ($user = Database::fetch_object($db_result)) {
  1287. $users[$user->user_id] = array(
  1288. 'user_id' => $user->user_id,
  1289. 'firstname' => $user->firstname,
  1290. 'lastname' => $user->lastname,
  1291. 'email' => $user->email,
  1292. 'username' => $user->username
  1293. );
  1294. }
  1295. return $users;
  1296. }
  1297. /**
  1298. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1299. * Get all subscribed tutors of a group
  1300. * @param int $group_id
  1301. * @return array An array with information of all users from the given group.
  1302. * (user_id, firstname, lastname, email)
  1303. */
  1304. public static function get_subscribed_tutors($group_id, $id_only = false)
  1305. {
  1306. $table_user = Database :: get_main_table(TABLE_MAIN_USER);
  1307. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1308. $order_clause = api_sort_by_first_name(
  1309. ) ? ' ORDER BY u.firstname, u.lastname' : ' ORDER BY u.lastname, u.firstname';
  1310. $group_id = Database::escape_string($group_id);
  1311. $course_id = api_get_course_int_id();
  1312. $sql = "SELECT tg.id, u.user_id, u.lastname, u.firstname, u.email
  1313. FROM ".$table_user." u, ".$table_group_tutor." tg
  1314. WHERE tg.c_id = $course_id AND
  1315. tg.group_id='".$group_id."' AND
  1316. tg.user_id=u.user_id".$order_clause;
  1317. $db_result = Database::query($sql);
  1318. $users = array ();
  1319. while ($user = Database::fetch_object($db_result)) {
  1320. if (!$id_only) {
  1321. $member['user_id'] = $user->user_id;
  1322. $member['firstname'] = $user->firstname;
  1323. $member['lastname'] = $user->lastname;
  1324. $member['email'] = $user->email;
  1325. $users[] = $member;
  1326. } else {
  1327. $users[]=$user->user_id;
  1328. }
  1329. }
  1330. return $users;
  1331. }
  1332. /**
  1333. * Subscribe user(s) to a specified group in current course
  1334. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1335. * @param int $group_id
  1336. * @return bool TRUE if successfull
  1337. */
  1338. public static function subscribe_users($user_ids, $group_id)
  1339. {
  1340. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1341. $result = true;
  1342. $course_id = api_get_course_int_id();
  1343. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1344. if (!empty($user_ids)) {
  1345. foreach ($user_ids as $user_id) {
  1346. if (self::can_user_subscribe($user_id, $group_id)) {
  1347. $user_id = Database::escape_string($user_id);
  1348. $group_id = Database::escape_string($group_id);
  1349. $sql = "INSERT INTO ".$table_group_user." (c_id, user_id, group_id)
  1350. VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1351. $result &= Database::query($sql);
  1352. }
  1353. }
  1354. }
  1355. return $result;
  1356. }
  1357. /**
  1358. * Subscribe tutor(s) to a specified group in current course
  1359. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1360. * @param int $group_id
  1361. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1362. * @see subscribe_users. This function is almost an exact copy of that function.
  1363. * @return bool TRUE if successful
  1364. */
  1365. public static function subscribe_tutors($user_ids, $group_id)
  1366. {
  1367. $user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
  1368. $result = true;
  1369. $course_id = api_get_course_int_id();
  1370. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1371. foreach ($user_ids as $user_id) {
  1372. $user_id = Database::escape_string($user_id);
  1373. $group_id = Database::escape_string($group_id);
  1374. $sql = "INSERT INTO ".$table_group_tutor." (c_id, user_id, group_id)
  1375. VALUES ('$course_id', '".$user_id."', '".$group_id."')";
  1376. $result &= Database::query($sql);
  1377. }
  1378. return $result;
  1379. }
  1380. /**
  1381. * Unsubscribe user(s) from a specified group in current course
  1382. * @param mixed $user_ids Can be an array with user-id's or a single user-id
  1383. * @param int $group_id
  1384. * @return bool TRUE if successful
  1385. */
  1386. public static function unsubscribe_users($user_ids, $group_id)
  1387. {
  1388. $user_ids = is_array($user_ids) ? $user_ids : array ($user_ids);
  1389. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1390. $group_id = Database::escape_string($group_id);
  1391. $course_id = api_get_course_int_id();
  1392. $sql = 'DELETE FROM '.$table_group_user.'
  1393. WHERE c_id = '.$course_id.' AND group_id = '.$group_id.' AND user_id IN ('.implode(',', $user_ids).')';
  1394. Database::query($sql);
  1395. }
  1396. /**
  1397. * Unsubscribe all users from one or more groups
  1398. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1399. * @return bool TRUE if successful
  1400. */
  1401. public static function unsubscribe_all_users($group_ids)
  1402. {
  1403. $course_id = api_get_course_int_id();
  1404. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1405. $group_ids = array_map('intval', $group_ids);
  1406. if (count($group_ids) > 0) {
  1407. if (api_is_course_coach()) {
  1408. for ($i = 0; $i < count($group_ids); $i++) {
  1409. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1410. array_splice($group_ids, $i, 1);
  1411. $i--;
  1412. }
  1413. }
  1414. if (count($group_ids) == 0) {
  1415. return false;
  1416. }
  1417. }
  1418. $table_group_user = Database :: get_course_table(TABLE_GROUP_USER);
  1419. $sql = 'DELETE FROM '.$table_group_user.' WHERE group_id IN ('.implode(',', $group_ids).') AND c_id = '.$course_id;
  1420. $result = Database::query($sql);
  1421. return $result;
  1422. }
  1423. return true;
  1424. }
  1425. /**
  1426. * Unsubscribe all tutors from one or more groups
  1427. * @param mixed $group_id Can be an array with group-id's or a single group-id
  1428. * @see unsubscribe_all_users. This function is almost an exact copy of that function.
  1429. * @return bool TRUE if successful
  1430. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1431. */
  1432. public static function unsubscribe_all_tutors($group_ids)
  1433. {
  1434. $course_id = api_get_course_int_id();
  1435. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1436. if (count($group_ids) > 0) {
  1437. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1438. $sql = 'DELETE FROM '.$table_group_tutor.' WHERE group_id IN ('.implode(',', $group_ids).') AND c_id = '.$course_id;
  1439. $result = Database::query($sql);
  1440. return $result;
  1441. }
  1442. return true;
  1443. }
  1444. /**
  1445. * Is the user a tutor of this group?
  1446. * @param $user_id the id of the user
  1447. * @param $group_id the id of the group
  1448. * @return boolean true/false
  1449. * @todo use the function user_has_access that includes this function
  1450. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1451. */
  1452. public static function is_tutor_of_group($user_id, $group_id)
  1453. {
  1454. $table_group_tutor = Database :: get_course_table(TABLE_GROUP_TUTOR);
  1455. $user_id = Database::escape_string($user_id);
  1456. $group_id = Database::escape_string($group_id);
  1457. $course_id = api_get_course_int_id();
  1458. $sql = "SELECT * FROM ".$table_group_tutor." WHERE c_id = $course_id AND user_id='".$user_id."' AND group_id='".$group_id."'";
  1459. $result = Database::query($sql);
  1460. if (Database::num_rows($result) > 0) {
  1461. return true;
  1462. } else {
  1463. return false;
  1464. }
  1465. }
  1466. /**
  1467. * Is the user part of this group? This can be a tutor or a normal member
  1468. * you should use this function if the access to a tool or functionality is restricted to the people who are actually in the group
  1469. * 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
  1470. * @param int $user_id the id of the user
  1471. * @param int $group_id the id of the group
  1472. * @return boolean true/false
  1473. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1474. */
  1475. public static function is_user_in_group($user_id, $group_id)
  1476. {
  1477. $member = self :: is_subscribed($user_id, $group_id);
  1478. $tutor = self :: is_tutor_of_group($user_id, $group_id);
  1479. if ($member OR $tutor) {
  1480. return true;
  1481. } else {
  1482. return false;
  1483. }
  1484. }
  1485. /**
  1486. * Get all tutors for the current course.
  1487. * @return array An array with firstname, lastname and user_id for all
  1488. * tutors in the current course.
  1489. * @deprecated this function uses the old tutor implementation
  1490. */
  1491. public static function get_all_tutors()
  1492. {
  1493. $course_user_table = Database :: get_main_table(TABLE_MAIN_COURSE_USER);
  1494. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  1495. $sql = "SELECT user.user_id AS user_id, user.lastname AS lastname, user.firstname AS firstname
  1496. FROM ".$user_table." user, ".$course_user_table." cu
  1497. WHERE cu.user_id=user.user_id
  1498. AND cu.tutor_id='1'
  1499. AND cu.c_id='".api_get_course_int_id()."'";
  1500. $resultTutor = Database::query($sql);
  1501. $tutors = array();
  1502. while ($tutor = Database::fetch_array($resultTutor)) {
  1503. $tutors[] = $tutor;
  1504. }
  1505. return $tutors;
  1506. }
  1507. /**
  1508. * Is user a tutor in current course
  1509. * @param int $user_id
  1510. * @return bool TRUE if given user is a tutor in the current course.
  1511. * @deprecated this function uses the old tutor implementation
  1512. */
  1513. public static function is_tutor($user_id)
  1514. {
  1515. $course_user_table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1516. $user_id = Database::escape_string($user_id);
  1517. $sql = "SELECT tutor_id FROM ".$course_user_table."
  1518. WHERE user_id = '".$user_id."' AND c_id ='".api_get_course_int_id()."'"."AND tutor_id=1";
  1519. $db_result = Database::query($sql);
  1520. $result = (Database::num_rows($db_result) > 0);
  1521. return $result;
  1522. }
  1523. /**
  1524. * Get all group's from a given course in which a given user is unsubscribed
  1525. * @author Patrick Cool
  1526. * @param int course id
  1527. * retrieve the groups for
  1528. * @param integer $user_id: the ID of the user you want to know all its
  1529. * group memberships
  1530. */
  1531. public static function get_group_ids($course_id, $user_id)
  1532. {
  1533. $groups = array();
  1534. $tbl_group = Database::get_course_table(TABLE_GROUP_USER);
  1535. $tbl_group_tutor = Database::get_course_table(TABLE_GROUP_TUTOR);
  1536. $user_id = intval($user_id);
  1537. $course_id = intval($course_id);
  1538. $sql = "SELECT group_id FROM $tbl_group WHERE c_id = $course_id AND user_id = '$user_id'";
  1539. $groupres = Database::query($sql);
  1540. if ($groupres) {
  1541. while ($myrow = Database::fetch_array($groupres)) {
  1542. $groups[] = $myrow['group_id'];
  1543. }
  1544. }
  1545. //Also loading if i'm the tutor
  1546. $sql = "SELECT group_id FROM $tbl_group_tutor WHERE c_id = $course_id AND user_id = '$user_id'";
  1547. $groupres = Database::query($sql);
  1548. if ($groupres) {
  1549. while ($myrow = Database::fetch_array($groupres)) {
  1550. $groups[] = $myrow['group_id'];
  1551. }
  1552. }
  1553. if (!empty($groups)) {
  1554. array_filter($groups);
  1555. }
  1556. return $groups;
  1557. }
  1558. /*
  1559. Group functions - these take virtual/linked courses into account when necessary
  1560. */
  1561. /**
  1562. * Get a combined list of all users of the real course $course_code
  1563. * and all users in virtual courses linked to this course $course_code
  1564. * Filter user list: remove duplicate users; plus
  1565. * remove users that
  1566. * - are already in the current group $group_id;
  1567. * - do not have student status in these courses;
  1568. * - are not appointed as tutor (group assistent) for this group;
  1569. * - have already reached their maximum # of groups in this course.
  1570. *
  1571. * Originally to get the correct list of users a big SQL statement was used,
  1572. * but this has become more complicated now there is not just one real course but many virtual courses.
  1573. * Still, that could have worked as well.
  1574. *
  1575. * @version 1.1.3
  1576. * @author Roan Embrechts
  1577. */
  1578. public static function get_complete_list_of_users_that_can_be_added_to_group ($course_code, $group_id)
  1579. {
  1580. $_course = api_get_course_info();
  1581. $_user = api_get_user_info();
  1582. $category = self :: get_category_from_group($group_id, $course_code);
  1583. $number_of_groups_limit = $category['groups_per_user'] == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $category['groups_per_user'];
  1584. $real_course_code = $_course['sysCode'];
  1585. $real_course_info = Database :: get_course_info($real_course_code);
  1586. $real_course_user_list = CourseManager :: get_user_list_from_course_code($real_course_code);
  1587. //get list of all virtual courses
  1588. $user_subscribed_course_list = CourseManager :: get_list_of_virtual_courses_for_specific_user_and_real_course($_user['user_id'], $real_course_code);
  1589. //add real course to the list
  1590. $user_subscribed_course_list[] = $real_course_info;
  1591. if (!is_array($user_subscribed_course_list)) {
  1592. return;
  1593. }
  1594. //for all courses...
  1595. foreach ($user_subscribed_course_list as $this_course) {
  1596. $this_course_code = $this_course['code'];
  1597. $course_user_list = CourseManager :: get_user_list_from_course_code($this_course_code);
  1598. //for all users in the course
  1599. foreach ($course_user_list as $this_user) {
  1600. $user_id = $this_user['user_id'];
  1601. $loginname = $this_user['username'];
  1602. $lastname = $this_user['lastname'];
  1603. $firstname = $this_user['firstname'];
  1604. $status = $this_user['status'];
  1605. //$role = $this_user['role'];
  1606. $tutor_id = $this_user['tutor_id'];
  1607. $full_name = api_get_person_name($firstname, $lastname);
  1608. if ($lastname == "" || $firstname == '') {
  1609. $full_name = $loginname;
  1610. }
  1611. $complete_user['user_id'] = $user_id;
  1612. $complete_user['full_name'] = $full_name;
  1613. $complete_user['firstname'] = $firstname;
  1614. $complete_user['lastname'] = $lastname;
  1615. $complete_user['status'] = $status;
  1616. $complete_user['tutor_id'] = $tutor_id;
  1617. $student_number_of_groups = self :: user_in_number_of_groups($user_id, $category['id']);
  1618. //filter: only add users that have not exceeded their maximum amount of groups
  1619. if ($student_number_of_groups < $number_of_groups_limit) {
  1620. $complete_user_list[] = $complete_user;
  1621. }
  1622. }
  1623. }
  1624. if (is_array($complete_user_list)) {
  1625. //sort once, on array field "full_name"
  1626. $complete_user_list = TableSort :: sort_table($complete_user_list, "full_name");
  1627. //filter out duplicates, based on field "user_id"
  1628. $complete_user_list = self :: filter_duplicates($complete_user_list, "user_id");
  1629. $complete_user_list = self :: filter_users_already_in_group($complete_user_list, $group_id);
  1630. }
  1631. return $complete_user_list;
  1632. }
  1633. /**
  1634. * Filter out duplicates in a multidimensional array
  1635. * by comparing field $compare_field.
  1636. *
  1637. * @param $user_array_in list of users (must be sorted).
  1638. * @param string $compare_field, the field to be compared
  1639. */
  1640. public static function filter_duplicates($user_array_in, $compare_field)
  1641. {
  1642. $total_number = count($user_array_in);
  1643. $user_array_out[0] = $user_array_in[0];
  1644. $count_out = 0;
  1645. for ($count_in = 1; $count_in < $total_number; $count_in++) {
  1646. if ($user_array_in[$count_in][$compare_field] != $user_array_out[$count_out][$compare_field]) {
  1647. $count_out++;
  1648. $user_array_out[$count_out] = $user_array_in[$count_in];
  1649. }
  1650. }
  1651. return $user_array_out;
  1652. }
  1653. /**
  1654. * Filters from the array $user_array_in the users already in the group $group_id.
  1655. */
  1656. public static function filter_users_already_in_group($user_array_in, $group_id)
  1657. {
  1658. foreach ($user_array_in as $this_user) {
  1659. if (!self :: is_subscribed($this_user['user_id'], $group_id)) {
  1660. $user_array_out[] = $this_user;
  1661. }
  1662. }
  1663. return $user_array_out;
  1664. }
  1665. /**
  1666. * Remove all users that are not students and all users who have tutor status
  1667. * from the list.
  1668. */
  1669. public static function filter_only_students($user_array_in)
  1670. {
  1671. $user_array_out = array();
  1672. foreach ($user_array_in as $this_user) {
  1673. //if ($this_user['status_rel'] == STUDENT && $this_user['tutor_id'] == 0) {
  1674. if (api_get_session_id()) {
  1675. if ($this_user['status_session'] == 0) {
  1676. $user_array_out[] = $this_user;
  1677. }
  1678. } else {
  1679. if ($this_user['status_rel'] == STUDENT) {
  1680. $user_array_out[] = $this_user;
  1681. }
  1682. }
  1683. }
  1684. return $user_array_out;
  1685. }
  1686. /**
  1687. * Check if a user has access to a certain group tool
  1688. * @param int $user_id The user id
  1689. * @param int $group_id The group id
  1690. * @param constant $tool The tool to check the access rights. This should be
  1691. * one of constants: GROUP_TOOL_DOCUMENTS
  1692. * @return bool True if the given user has access to the given tool in the
  1693. * given course.
  1694. */
  1695. public static function user_has_access($user_id, $group_id, $tool)
  1696. {
  1697. // Admin have access everywhere
  1698. if (api_is_platform_admin()) {
  1699. return true;
  1700. }
  1701. // Course admin also have access to everything
  1702. if (api_is_allowed_to_edit()) {
  1703. return true;
  1704. }
  1705. switch ($tool) {
  1706. case self::GROUP_TOOL_FORUM :
  1707. $state_key = 'forum_state';
  1708. break;
  1709. case self::GROUP_TOOL_DOCUMENTS :
  1710. $state_key = 'doc_state';
  1711. break;
  1712. case self::GROUP_TOOL_CALENDAR :
  1713. $state_key = 'calendar_state';
  1714. break;
  1715. case self::GROUP_TOOL_ANNOUNCEMENT :
  1716. $state_key = 'announcements_state';
  1717. break;
  1718. case self::GROUP_TOOL_WORK :
  1719. $state_key = 'work_state';
  1720. break;
  1721. case self::GROUP_TOOL_WIKI :
  1722. $state_key = 'wiki_state';
  1723. break;
  1724. case self::GROUP_TOOL_CHAT :
  1725. $state_key = 'chat_state';
  1726. break;
  1727. default:
  1728. return false;
  1729. }
  1730. $user_is_in_group = self :: is_user_in_group($user_id, $group_id);
  1731. // Check group properties
  1732. $group_info = self :: get_group_properties($group_id);
  1733. if (empty($group_info)) {
  1734. return false;
  1735. }
  1736. if ($group_info[$state_key] == self::TOOL_NOT_AVAILABLE) {
  1737. return false;
  1738. } elseif ($group_info[$state_key] == self::TOOL_PUBLIC) {
  1739. return true;
  1740. } elseif (api_is_allowed_to_edit(false, true)) {
  1741. return true;
  1742. } elseif ($group_info['tutor_id'] == $user_id) { //this tutor implementation was dropped
  1743. return true;
  1744. } elseif ($group_info[$state_key] == self::TOOL_PRIVATE && !$user_is_in_group) {
  1745. return false;
  1746. } else {
  1747. return $user_is_in_group;
  1748. }
  1749. }
  1750. /**
  1751. * Get all groups where a specific user is subscribed
  1752. * @param int $user_id
  1753. * @return array
  1754. */
  1755. public static function get_user_group_name($user_id)
  1756. {
  1757. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1758. $table_group = Database::get_course_table(TABLE_GROUP);
  1759. $user_id = intval($user_id);
  1760. $course_id = api_get_course_int_id();
  1761. $sql = "SELECT name
  1762. FROM $table_group g INNER JOIN $table_group_user gu
  1763. ON (gu.group_id=g.id)
  1764. WHERE
  1765. gu.c_id= $course_id AND
  1766. g.c_id= $course_id AND
  1767. gu.user_id = $user_id";
  1768. $res = Database::query($sql);
  1769. $groups = array();
  1770. while ($group = Database::fetch_array($res)) {
  1771. $groups[] .= $group['name'];
  1772. }
  1773. return $groups;
  1774. }
  1775. /**
  1776. * Get all groups where a specific user is subscribed
  1777. * @param int $user_id
  1778. * @return array
  1779. */
  1780. public static function getAllGroupPerUserSubscription($user_id)
  1781. {
  1782. $table_group_user = Database::get_course_table(TABLE_GROUP_USER);
  1783. $table_tutor_user = Database::get_course_table(TABLE_GROUP_TUTOR);
  1784. $table_group = Database::get_course_table(TABLE_GROUP);
  1785. $user_id = intval($user_id);
  1786. $course_id = api_get_course_int_id();
  1787. $sql = "SELECT DISTINCT name
  1788. FROM $table_group g
  1789. LEFT JOIN $table_group_user gu
  1790. ON (gu.group_id = g.id AND g.c_id = gu.c_id)
  1791. LEFT JOIN $table_tutor_user tu
  1792. ON (tu.group_id = g.id AND g.c_id = tu.c_id)
  1793. WHERE
  1794. g.c_id = $course_id AND
  1795. (gu.user_id = $user_id OR tu.user_id = $user_id) ";
  1796. $res = Database::query($sql);
  1797. $groups = array();
  1798. while ($group = Database::fetch_array($res)) {
  1799. $groups[] = $group['name'];
  1800. }
  1801. return $groups;
  1802. }
  1803. /**
  1804. *
  1805. * See : fill_groups
  1806. * Fill the groups with students.
  1807. *
  1808. * note : optimize fill_groups_list <--> fill_groups
  1809. * @param array $group_ids
  1810. * @return array|bool
  1811. */
  1812. public static function fill_groups_list($group_ids)
  1813. {
  1814. $group_ids = is_array($group_ids) ? $group_ids : array($group_ids);
  1815. $group_ids = array_map('intval', $group_ids);
  1816. if (api_is_course_coach()) {
  1817. for ($i = 0; $i < count($group_ids); $i++) {
  1818. if (!api_is_element_in_the_session(TOOL_GROUP, $group_ids[$i])) {
  1819. array_splice($group_ids, $i, 1);
  1820. $i--;
  1821. }
  1822. }
  1823. if (count($group_ids) == 0) {
  1824. return false;
  1825. }
  1826. }
  1827. $_course = api_get_course_info();
  1828. $category = self :: get_category_from_group($group_ids[0]);
  1829. $groups_per_user = $category['groups_per_user'];
  1830. $group_table = Database :: get_course_table(TABLE_GROUP);
  1831. $group_user_table = Database :: get_course_table(TABLE_GROUP_USER);
  1832. $session_id = api_get_session_id();
  1833. $complete_user_list = CourseManager :: get_real_and_linked_user_list($_course['sysCode'], true, $session_id);
  1834. $number_groups_per_user = ($groups_per_user == self::GROUP_PER_MEMBER_NO_LIMIT ? self::INFINITE : $groups_per_user);
  1835. $course_id = api_get_course_int_id();
  1836. /*
  1837. * Retrieve all the groups where enrollment is still allowed
  1838. * (reverse) ordered by the number of place available
  1839. */
  1840. $sql = "SELECT g.id gid, count(ug.user_id) count_users, g.max_student
  1841. FROM ".$group_table." g
  1842. LEFT JOIN ".$group_user_table." ug
  1843. ON g.id = ug.group_id
  1844. WHERE g.c_id = $course_id AND
  1845. ug.c_id = $course_id AND
  1846. g.id IN (".implode(',', $group_ids).")
  1847. GROUP BY (g.id)";
  1848. $sql_result = Database::query($sql);
  1849. $group_available_place = array();
  1850. while ($group = Database::fetch_array($sql_result, 'ASSOC')) {
  1851. if (!empty($group['max_student'])) {
  1852. $places = intval($group['max_student'] - $group['count_users']);
  1853. } else {
  1854. $places = self::MEMBER_PER_GROUP_NO_LIMIT;
  1855. }
  1856. $group_available_place[$group['gid']] = $places;
  1857. }
  1858. /*
  1859. * Retrieve course users (reverse) ordered by the number
  1860. * of group they are already enrolled
  1861. */
  1862. for ($i = 0; $i < count($complete_user_list); $i ++) {
  1863. //find # of groups the user is enrolled in
  1864. $number_of_groups = self :: user_in_number_of_groups($complete_user_list[$i]["user_id"],$category['id']);
  1865. //add # of groups to user list
  1866. $complete_user_list[$i]['number_groups_left'] = $number_groups_per_user - $number_of_groups;
  1867. }
  1868. //first sort by user_id to filter out duplicates
  1869. $complete_user_list = TableSort::sort_table($complete_user_list, 'user_id');
  1870. $complete_user_list = self::filter_duplicates($complete_user_list, 'user_id');
  1871. //$complete_user_list = self :: filter_only_students($complete_user_list);
  1872. //now sort by # of group left
  1873. $complete_user_list = TableSort::sort_table($complete_user_list, 'number_groups_left', SORT_DESC);
  1874. return $complete_user_list;
  1875. }
  1876. /**
  1877. * @param array $group_list
  1878. * @param int $category_id
  1879. */
  1880. static function process_groups($group_list, $category_id = null)
  1881. {
  1882. global $origin, $charset;
  1883. $category_id = intval($category_id);
  1884. $totalRegistered = 0;
  1885. $group_data = array();
  1886. $user_info = api_get_user_info();
  1887. $session_id = api_get_session_id();
  1888. $user_id = $user_info['user_id'];
  1889. $orig = isset($origin) ? $origin : null;
  1890. foreach ($group_list as $this_group) {
  1891. // Validation when belongs to a session
  1892. $session_img = api_get_session_image($this_group['session_id'], $user_info['status']);
  1893. // All the tutors of this group
  1894. $tutorsids_of_group = self::get_subscribed_tutors($this_group['id'], true);
  1895. // Create a new table-row
  1896. $row = array();
  1897. // Checkbox
  1898. if (api_is_allowed_to_edit(false, true) && count($group_list) > 1) {
  1899. $row[] = $this_group['id'];
  1900. }
  1901. // Group name
  1902. if ((api_is_allowed_to_edit(false, true) ||
  1903. in_array($user_id, $tutorsids_of_group) ||
  1904. $this_group['is_member'] ||
  1905. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_FORUM) ||
  1906. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_DOCUMENTS) ||
  1907. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_CALENDAR) ||
  1908. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_ANNOUNCEMENT) ||
  1909. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WORK) ||
  1910. self::user_has_access($user_id, $this_group['id'], self::GROUP_TOOL_WIKI))
  1911. && !(api_is_course_coach() && intval($this_group['session_id']) != $session_id)
  1912. ) {
  1913. $group_name = '<a href="group_space.php?cidReq='.api_get_course_id().'&amp;origin='.$orig.'&amp;gidReq='.$this_group['id'].'">'.
  1914. Security::remove_XSS($this_group['name']).'</a> ';
  1915. if (!empty($user_id) && !empty($this_group['id_tutor']) && $user_id == $this_group['id_tutor']) {
  1916. $group_name .= Display::label(get_lang('OneMyGroups'), 'success');
  1917. } elseif ($this_group['is_member']) {
  1918. $group_name .= Display::label(get_lang('MyGroup'), 'success');
  1919. }
  1920. if (api_is_allowed_to_edit() && !empty($this_group['session_name'])) {
  1921. $group_name .= ' ('.$this_group['session_name'].')';
  1922. }
  1923. $group_name .= $session_img;
  1924. $row[] = $group_name.'<br />'.stripslashes(trim($this_group['description']));
  1925. } else {
  1926. $row[] = $this_group['name'].'<br />'.stripslashes(trim($this_group['description']));
  1927. }
  1928. // Tutor name
  1929. $tutor_info = null;
  1930. if (count($tutorsids_of_group) > 0) {
  1931. foreach ($tutorsids_of_group as $tutor_id) {
  1932. $tutor = api_get_user_info($tutor_id);
  1933. $username = api_htmlentities(sprintf(get_lang('LoginX'), $tutor['username']), ENT_QUOTES);
  1934. if (api_get_setting('show_email_addresses') == 'true') {
  1935. $tutor_info .= Display::tag('span', Display::encrypted_mailto_link($tutor['mail'], api_get_person_name($tutor['firstName'], $tutor['lastName'])), array('title'=>$username)).', ';
  1936. } else {
  1937. if (api_is_allowed_to_edit()) {
  1938. $tutor_info .= Display::tag('span', Display::encrypted_mailto_link($tutor['mail'], api_get_person_name($tutor['firstName'], $tutor['lastName'])), array('title'=>$username)).', ';
  1939. } else {
  1940. $tutor_info .= Display::tag('span', api_get_person_name($tutor['firstName'], $tutor['lastName']), array('title'=>$username)).', ';
  1941. }
  1942. }
  1943. }
  1944. }
  1945. $tutor_info = api_substr($tutor_info, 0, api_strlen($tutor_info) - 2);
  1946. $row[] = $tutor_info;
  1947. // Max number of members in group
  1948. $max_members = ($this_group['maximum_number_of_members'] == self::MEMBER_PER_GROUP_NO_LIMIT ? ' ' : ' / '.$this_group['maximum_number_of_members']);
  1949. // Number of members in group
  1950. $row[] = $this_group['number_of_members'].$max_members;
  1951. // Self-registration / unregistration
  1952. if (!api_is_allowed_to_edit(false, true)) {
  1953. if (self :: is_self_registration_allowed($user_id, $this_group['id'])) {
  1954. $row[] = '<a class = "btn" href="group.php?'.api_get_cidreq().'&category='.$category_id.'&amp;action=self_reg&amp;group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."'".')) return false;">'.get_lang('GroupSelfRegInf').'</a>';
  1955. } elseif (self :: is_self_unregistration_allowed($user_id, $this_group['id'])) {
  1956. $row[] = '<a class = "btn" href="group.php?'.api_get_cidreq().'&category='.$category_id.'&amp;action=self_unreg&amp;group_id='.$this_group['id'].'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."'".')) return false;">'.get_lang('GroupSelfUnRegInf').'</a>';
  1957. } else {
  1958. $row[] = '-';
  1959. }
  1960. }
  1961. $url = api_get_path(WEB_CODE_PATH).'group/';
  1962. // Edit-links
  1963. if (api_is_allowed_to_edit(false, true) && !(api_is_course_coach() && intval($this_group['session_id']) != $session_id)) {
  1964. $edit_actions = '<a href="'.$url.'settings.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'" title="'.get_lang('Edit').'">'.
  1965. Display::return_icon('edit.png', get_lang('EditGroup'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  1966. $edit_actions .= '<a href="'.$url.'member_settings.php?'.api_get_cidreq(true, false).'&gidReq='.$this_group['id'].'" title="'.get_lang('GroupMembers').'">'.
  1967. Display::return_icon('user.png', get_lang('GroupMembers'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1968. $edit_actions .= '<a href="'.$url.'group_overview.php?action=export&type=xls&'.api_get_cidreq(true, false).'&id='.$this_group['id'].'" title="'.get_lang('ExportUsers').'">'.
  1969. Display::return_icon('export_excel.png', get_lang('Export'), '', ICON_SIZE_SMALL).'</a>&nbsp;';
  1970. /*$edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&amp;action=empty_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('EmptyGroup').'">'.
  1971. Display::return_icon('clean.png',get_lang('EmptyGroup'),'',ICON_SIZE_SMALL).'</a>&nbsp;';*/
  1972. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&amp;action=fill_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('FillGroup').'">'.
  1973. Display::return_icon('fill.png',get_lang('FillGroup'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  1974. $edit_actions .= '<a href="'.api_get_self().'?'.api_get_cidreq(true, false).'&category='.$category_id.'&amp;action=delete_one&amp;id='.$this_group['id'].'" onclick="javascript: if(!confirm('."'".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES))."'".')) return false;" title="'.get_lang('Delete').'">'.
  1975. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).'</a>&nbsp;';
  1976. $row[] = $edit_actions;
  1977. }
  1978. if (!empty($this_group['nbMember'])) {
  1979. $totalRegistered = $totalRegistered + $this_group['nbMember'];
  1980. }
  1981. $group_data[] = $row;
  1982. } // end loop
  1983. $table = new SortableTableFromArrayConfig($group_data, 1, 20, 'group_category_'.$category_id);
  1984. $table->set_additional_parameters(array('category' => $category_id));
  1985. $column = 0;
  1986. if (api_is_allowed_to_edit(false, true) and count($group_list) > 1) {
  1987. $table->set_header($column++, '', false);
  1988. }
  1989. $table->set_header($column++, get_lang('Groups'));
  1990. $table->set_header($column++, get_lang('GroupTutor'));
  1991. $table->set_header($column++, get_lang('Registered'), false);
  1992. if (!api_is_allowed_to_edit(false, true)) { // If self-registration allowed
  1993. $table->set_header($column++, get_lang('GroupSelfRegistration'), false);
  1994. }
  1995. if (api_is_allowed_to_edit(false, true)) { // Only for course administrator
  1996. $table->set_header($column++, get_lang('Modify'), false);
  1997. $form_actions = array();
  1998. $form_actions['fill_selected'] = get_lang('FillGroup');
  1999. $form_actions['empty_selected'] = get_lang('EmptyGroup');
  2000. $form_actions['delete_selected'] = get_lang('Delete');
  2001. if (count($group_list) > 1) {
  2002. $table->set_form_actions($form_actions, 'group');
  2003. }
  2004. }
  2005. $table->display();
  2006. }
  2007. /**
  2008. *
  2009. * @param array $groupData
  2010. * @param bool $deleteNotInArray
  2011. * @return array
  2012. */
  2013. public static function importCategoriesAndGroupsFromArray($groupData, $deleteNotInArray = false)
  2014. {
  2015. $result = array();
  2016. $elementsFound = array(
  2017. 'categories' => array(),
  2018. 'groups' => array()
  2019. );
  2020. $groupCategories = GroupManager::get_categories();
  2021. if (empty($groupCategories)) {
  2022. $result['error'][] = get_lang('CreateACategory');
  2023. return $result;
  2024. }
  2025. foreach ($groupData as $data) {
  2026. $isCategory = empty($data['group']) ? true : false;
  2027. if ($isCategory) {
  2028. $categoryInfo = self::getCategoryByTitle($data['category']);
  2029. $categoryId = $categoryInfo['id'];
  2030. if (!empty($categoryInfo)) {
  2031. // Update
  2032. self::update_category(
  2033. $categoryId,
  2034. $data['category'],
  2035. $data['description'],
  2036. $data['doc_state'],
  2037. $data['work_state'],
  2038. $data['calendar_state'],
  2039. $data['announcements_state'],
  2040. $data['forum_state'],
  2041. $data['wiki_state'],
  2042. $data['chat_state'],
  2043. $data['self_reg_allowed'],
  2044. $data['self_unreg_allowed'],
  2045. $data['max_student'],
  2046. $data['groups_per_user']
  2047. );
  2048. $data['category_id'] = $categoryId;
  2049. $result['updated']['category'][] = $data;
  2050. } else {
  2051. // Add
  2052. $categoryId = self::create_category(
  2053. $data['category'],
  2054. $data['description'],
  2055. $data['doc_state'],
  2056. $data['work_state'],
  2057. $data['calendar_state'],
  2058. $data['announcements_state'],
  2059. $data['forum_state'],
  2060. $data['wiki_state'],
  2061. $data['chat_state'],
  2062. $data['self_reg_allowed'],
  2063. $data['self_unreg_allowed'],
  2064. $data['max_student'],
  2065. $data['groups_per_user']
  2066. );
  2067. if ($categoryId) {
  2068. $data['category_id'] = $categoryId;
  2069. $result['added']['category'][] = $data;
  2070. }
  2071. }
  2072. $elementsFound['categories'][] = $categoryId;
  2073. } else {
  2074. $groupInfo = self::getGroupByName($data['group']);
  2075. $categoryInfo = self::getCategoryByTitle($data['category']);
  2076. $categoryId = null;
  2077. if (!empty($categoryInfo)) {
  2078. $categoryId = $categoryInfo['id'];
  2079. } else {
  2080. if (!empty($groupCategories) && isset($groupCategories[0])) {
  2081. $defaultGroupCategory = $groupCategories[0];
  2082. $categoryId = $defaultGroupCategory['id'];
  2083. }
  2084. }
  2085. if (empty($groupInfo)) {
  2086. // Add
  2087. $groupId = self::create_group(
  2088. $data['group'],
  2089. $categoryId,
  2090. null,
  2091. $data['max_students']
  2092. );
  2093. if ($groupId) {
  2094. self::set_group_properties(
  2095. $groupId,
  2096. $data['group'],
  2097. $data['description'],
  2098. $data['max_students'],
  2099. $data['doc_state'],
  2100. $data['work_state'],
  2101. $data['calendar_state'],
  2102. $data['announcements_state'],
  2103. $data['forum_state'],
  2104. $data['wiki_state'],
  2105. $data['chat_state'],
  2106. $data['self_reg_allowed'],
  2107. $data['self_unreg_allowed'],
  2108. $categoryId
  2109. );
  2110. $data['group_id'] = $groupId;
  2111. $result['added']['group'][] = $data;
  2112. }
  2113. } else {
  2114. // Update
  2115. $groupId = $groupInfo['id'];
  2116. self::set_group_properties(
  2117. $groupId,
  2118. $data['group'],
  2119. $data['description'],
  2120. $data['max_students'],
  2121. $data['doc_state'],
  2122. $data['work_state'],
  2123. $data['calendar_state'],
  2124. $data['announcements_state'],
  2125. $data['forum_state'],
  2126. $data['wiki_state'],
  2127. $data['chat_state'],
  2128. $data['self_reg_allowed'],
  2129. $data['self_unreg_allowed'],
  2130. $categoryId
  2131. );
  2132. $data['group_id'] = $groupId;
  2133. $result['updated']['group'][] = $data;
  2134. }
  2135. $elementsFound['groups'][] = $groupId;
  2136. }
  2137. }
  2138. if ($deleteNotInArray) {
  2139. // Check categories
  2140. $categories = GroupManager::get_categories();
  2141. foreach ($categories as $category) {
  2142. if (!in_array($category['id'], $elementsFound['categories'])) {
  2143. GroupManager::delete_category($category['id']);
  2144. $category['category'] = $category['title'];
  2145. $result['deleted']['category'][] = $category;
  2146. }
  2147. }
  2148. $groups = GroupManager::get_groups();
  2149. foreach ($groups as $group) {
  2150. if (!in_array($group['id'], $elementsFound['groups'])) {
  2151. GroupManager::delete_groups(array($group['id']));
  2152. $group['group'] = $group['name'];
  2153. $result['deleted']['group'][] = $group;
  2154. }
  2155. }
  2156. }
  2157. return $result;
  2158. }
  2159. /**
  2160. * Export all categories/group from a course to an array.
  2161. * This function works only in a context of a course.
  2162. * @return array
  2163. */
  2164. public static function exportCategoriesAndGroupsToArray()
  2165. {
  2166. $data = array();
  2167. $data[] = array(
  2168. 'category',
  2169. 'group',
  2170. 'description',
  2171. 'announcements_state',
  2172. 'calendar_state',
  2173. 'chat_state',
  2174. 'doc_state',
  2175. 'forum_state',
  2176. 'work_state',
  2177. 'wiki_state',
  2178. 'max_student',
  2179. 'self_reg_allowed',
  2180. 'self_unreg_allowed',
  2181. 'groups_per_user'
  2182. );
  2183. $categories = GroupManager::get_categories();
  2184. foreach ($categories as $categoryInfo) {
  2185. $data[] = array(
  2186. $categoryInfo['title'],
  2187. null,
  2188. $categoryInfo['description'],
  2189. $categoryInfo['announcements_state'],
  2190. $categoryInfo['calendar_state'],
  2191. $categoryInfo['chat_state'],
  2192. $categoryInfo['doc_state'],
  2193. $categoryInfo['forum_state'],
  2194. $categoryInfo['work_state'],
  2195. $categoryInfo['wiki_state'],
  2196. $categoryInfo['max_student'],
  2197. $categoryInfo['self_reg_allowed'],
  2198. $categoryInfo['self_unreg_allowed'],
  2199. $categoryInfo['groups_per_user']
  2200. );
  2201. }
  2202. $groups = GroupManager::get_group_list();
  2203. foreach ($groups as $groupInfo) {
  2204. $categoryTitle = null;
  2205. $categoryInfo = GroupManager::get_category($groupInfo['category_id']);
  2206. $groupSettings = GroupManager::get_group_properties($groupInfo['id']);
  2207. if (!empty($categoryInfo)) {
  2208. $categoryTitle = $categoryInfo['title'];
  2209. }
  2210. $data[] = array(
  2211. $categoryTitle,
  2212. $groupSettings['name'],
  2213. $groupSettings['description'],
  2214. $groupSettings['announcements_state'],
  2215. $groupSettings['calendar_state'],
  2216. $groupSettings['chat_state'],
  2217. $groupSettings['doc_state'],
  2218. $groupSettings['forum_state'],
  2219. $groupSettings['work_state'],
  2220. $groupSettings['wiki_state'],
  2221. $groupSettings['maximum_number_of_students'],
  2222. $groupSettings['self_registration_allowed'],
  2223. $groupSettings['self_unregistration_allowed'],
  2224. );
  2225. }
  2226. return $data;
  2227. }
  2228. /**
  2229. * @param string $default
  2230. */
  2231. static function getSettingBar($default)
  2232. {
  2233. $activeSettings = null;
  2234. $activeTutor = null;
  2235. $activeMember = null;
  2236. switch($default) {
  2237. case 'settings':
  2238. $activeSettings = 'active';
  2239. break;
  2240. case'tutor':
  2241. $activeTutor = 'active';
  2242. break;
  2243. case 'member':
  2244. $activeMember = 'active';
  2245. break;
  2246. }
  2247. $url = api_get_path(WEB_CODE_PATH).'group/%s?'.api_get_cidreq();
  2248. echo '
  2249. <ul class="nav nav-tabs">
  2250. <li class="'.$activeSettings.'">
  2251. <a href="'.sprintf($url, 'settings.php').'">
  2252. '.Display::return_icon('settings.png').' '.get_lang('Settings').'
  2253. </a>
  2254. </li>
  2255. <li class="'.$activeMember.'">
  2256. <a href="'.sprintf($url, 'member_settings.php').'">
  2257. '.Display::return_icon('user.png').' '.get_lang('GroupMembers').'
  2258. </a>
  2259. </li>
  2260. <li class="'.$activeTutor.'">
  2261. <a href="'.sprintf($url, 'tutor_settings.php').'">
  2262. '.Display::return_icon('teacher.png').' '.get_lang('GroupTutors').'
  2263. </a>
  2264. </li>
  2265. </ul>';
  2266. }
  2267. /**
  2268. * @param int $courseId
  2269. * @param string $keyword
  2270. * @return string
  2271. */
  2272. public static function getOverview($courseId, $keyword = null)
  2273. {
  2274. $content = null;
  2275. $categories = GroupManager::get_categories();
  2276. if (!empty($categories)) {
  2277. foreach ($categories as $category) {
  2278. if (api_get_setting('allow_group_categories') == 'true') {
  2279. $content .= '<h2>'.$category['title'].'</h2>';
  2280. }
  2281. if (!empty($keyword)) {
  2282. $groups = GroupManager::getGroupListFilterByName($keyword, $category['id'], $courseId);
  2283. } else {
  2284. $groups = GroupManager::get_group_list($category['id']);
  2285. }
  2286. $content .= '<ul>';
  2287. if (!empty($groups)) {
  2288. foreach ($groups as $group) {
  2289. $content .= '<li>';
  2290. $content .= Display::tag('h3', Security::remove_XSS($group['name']));
  2291. $users = GroupManager::getTutors($group['id']);
  2292. if (!empty($users)) {
  2293. $content .= '<ul>';
  2294. $content .= "<li>".Display::tag('h4', get_lang('Tutors'))."</li><ul>";
  2295. foreach ($users as $user) {
  2296. $user_info = api_get_user_info($user['user_id']);
  2297. $content .= '<li title="'.$user_info['username'].'">'.$user_info['complete_name_with_username'].'</li>';
  2298. }
  2299. $content .= '</ul>';
  2300. $content .= '</ul>';
  2301. }
  2302. $users = GroupManager::getStudents($group['id']);
  2303. if (!empty($users)) {
  2304. $content .= '<ul>';
  2305. $content .= "<li>".Display::tag('h4', get_lang('Students'))."</li><ul>";
  2306. foreach ($users as $user) {
  2307. $user_info = api_get_user_info($user['user_id']);
  2308. $content .= '<li title="'.$user_info['username'].'">'.$user_info['complete_name_with_username'].'</li>';
  2309. }
  2310. $content .= '</ul>';
  2311. $content .= '</ul>';
  2312. }
  2313. $content .= '</li>';
  2314. }
  2315. }
  2316. $content .= '</ul>';
  2317. }
  2318. }
  2319. return $content;
  2320. }
  2321. /**
  2322. * Returns the search form
  2323. * @return string
  2324. */
  2325. public static function getSearchForm()
  2326. {
  2327. $url = api_get_path(WEB_CODE_PATH).'group/group_overview.php?'.api_get_cidreq();
  2328. $form = new FormValidator('search_groups', 'get', $url, null, array('class' => 'form-search'));
  2329. $form->addElement('text', 'keyword');
  2330. $form->addElement('button', 'submit', get_lang('Search'));
  2331. return $form->toHtml();
  2332. }
  2333. }