groupmanager.lib.php 104 KB

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