groupmanager.lib.php 104 KB

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