groupmanager.lib.php 107 KB

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