groupmanager.lib.php 108 KB

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