groupmanager.lib.php 107 KB

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