groupmanager.lib.php 108 KB

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