AnnouncementManager.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CourseBundle\Entity\CAnnouncement;
  4. use Chamilo\CourseBundle\Entity\CItemProperty;
  5. /**
  6. * Include file with functions for the announcements module.
  7. * @author jmontoya
  8. * @package chamilo.announcements
  9. * @todo use OOP
  10. */
  11. class AnnouncementManager
  12. {
  13. /**
  14. * Constructor
  15. */
  16. public function __construct()
  17. {
  18. }
  19. /**
  20. * @return array
  21. */
  22. public static function getTags()
  23. {
  24. $tags = [
  25. '((user_name))',
  26. '((user_firstname))',
  27. '((user_lastname))',
  28. '((user_official_code))',
  29. '((course_title))',
  30. '((course_link))'
  31. ];
  32. $tags[] = '((teachers))';
  33. if (!empty(api_get_session_id())) {
  34. $tags[] = '((coaches))';
  35. $tags[] = '((general_coach))';
  36. $tags[] = '((general_coach_email))';
  37. }
  38. return $tags;
  39. }
  40. /**
  41. * @param int $userId
  42. * @param string $content
  43. * @param string $courseCode
  44. * @param int $sessionId
  45. *
  46. * @return string
  47. */
  48. public static function parse_content(
  49. $userId,
  50. $content,
  51. $courseCode,
  52. $sessionId = 0
  53. ) {
  54. $readerInfo = api_get_user_info($userId);
  55. $courseInfo = api_get_course_info($courseCode);
  56. $teacherList = CourseManager::get_teacher_list_from_course_code_to_string(
  57. $courseInfo['code']
  58. );
  59. $generalCoachName = '';
  60. $generalCoachEmail = '';
  61. $coaches = '';
  62. if (!empty($sessionId)) {
  63. $sessionInfo = api_get_session_info($sessionId);
  64. $coaches = CourseManager::get_coachs_from_course_to_string(
  65. $sessionId,
  66. $courseInfo['real_id']
  67. );
  68. $generalCoach = api_get_user_info($sessionInfo['id_coach']);
  69. $generalCoachName = $generalCoach['complete_name'];
  70. $generalCoachEmail = $generalCoach['email'];
  71. }
  72. $data = [];
  73. $data['user_name'] = '';
  74. $data['user_firstname'] = '';
  75. $data['user_lastname'] = '';
  76. $data['user_official_code'] = '';
  77. if (!empty($readerInfo)) {
  78. $data['user_name'] = $readerInfo['username'];
  79. $data['user_firstname'] = $readerInfo['firstname'];
  80. $data['user_lastname'] = $readerInfo['lastname'];
  81. $data['user_official_code'] = $readerInfo['official_code'];
  82. }
  83. $data['course_title'] = $courseInfo['name'];
  84. $courseLink = api_get_course_url($courseCode, $sessionId);
  85. $data['course_link'] = Display::url($courseLink, $courseLink);
  86. $data['teachers'] = $teacherList;
  87. if (!empty(api_get_session_id())) {
  88. $data['coaches'] = $coaches;
  89. $data['general_coach'] = $generalCoachName;
  90. $data['general_coach_email'] = $generalCoachEmail;
  91. }
  92. $content = str_replace(self::getTags(), $data, $content);
  93. return $content;
  94. }
  95. /**
  96. * Gets all announcements from a course
  97. * @param array $course_info
  98. * @param int $session_id
  99. * @return array html with the content and count of announcements or false otherwise
  100. */
  101. public static function get_all_annoucement_by_course($course_info, $session_id = 0)
  102. {
  103. $session_id = intval($session_id);
  104. $course_id = $course_info['real_id'];
  105. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  106. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  107. $sql = "SELECT DISTINCT
  108. announcement.id,
  109. announcement.title,
  110. announcement.content
  111. FROM $tbl_announcement announcement
  112. INNER JOIN $tbl_item_property i
  113. ON (announcement.id = i.ref AND announcement.c_id = i.c_id)
  114. WHERE
  115. i.tool='announcement' AND
  116. announcement.session_id = '$session_id' AND
  117. announcement.c_id = $course_id AND
  118. i.c_id = $course_id
  119. ORDER BY display_order DESC";
  120. $rs = Database::query($sql);
  121. $num_rows = Database::num_rows($rs);
  122. if ($num_rows > 0) {
  123. $list = array();
  124. while ($row = Database::fetch_array($rs)) {
  125. $list[] = $row;
  126. }
  127. return $list;
  128. }
  129. return false;
  130. }
  131. /**
  132. * This functions switches the visibility a course resource
  133. * using the visibility field in 'item_property'
  134. * @param array $_course
  135. * @param int $id ID of the element of the corresponding type
  136. * @return bool False on failure, True on success
  137. */
  138. public static function change_visibility_announcement($_course, $id)
  139. {
  140. $session_id = api_get_session_id();
  141. $item_visibility = api_get_item_visibility(
  142. $_course,
  143. TOOL_ANNOUNCEMENT,
  144. $id,
  145. $session_id
  146. );
  147. if ($item_visibility == '1') {
  148. api_item_property_update(
  149. $_course,
  150. TOOL_ANNOUNCEMENT,
  151. $id,
  152. 'invisible',
  153. api_get_user_id()
  154. );
  155. } else {
  156. api_item_property_update(
  157. $_course,
  158. TOOL_ANNOUNCEMENT,
  159. $id,
  160. 'visible',
  161. api_get_user_id()
  162. );
  163. }
  164. return true;
  165. }
  166. /**
  167. * Deletes an announcement
  168. * @param array $_course the course array
  169. * @param int $id the announcement id
  170. */
  171. public static function delete_announcement($_course, $id)
  172. {
  173. api_item_property_update(
  174. $_course,
  175. TOOL_ANNOUNCEMENT,
  176. $id,
  177. 'delete',
  178. api_get_user_id()
  179. );
  180. }
  181. /**
  182. * Deletes all announcements by course
  183. * @param array $_course the course array
  184. */
  185. public static function delete_all_announcements($_course)
  186. {
  187. $announcements = self::get_all_annoucement_by_course($_course, api_get_session_id());
  188. if (!empty($announcements)) {
  189. foreach ($announcements as $annon) {
  190. api_item_property_update(
  191. $_course,
  192. TOOL_ANNOUNCEMENT,
  193. $annon['id'],
  194. 'delete',
  195. api_get_user_id()
  196. );
  197. }
  198. }
  199. }
  200. /**
  201. * @param string $title
  202. * @param int $courseId
  203. * @param int $sessionId
  204. * @param int $visibility 1 or 0
  205. *
  206. * @return mixed
  207. */
  208. public static function getAnnouncementsByTitle(
  209. $title,
  210. $courseId,
  211. $sessionId = 0,
  212. $visibility = 1
  213. ) {
  214. $dql = "SELECT a
  215. FROM ChamiloCourseBundle:CAnnouncement a
  216. JOIN ChamiloCourseBundle:CItemProperty ip
  217. WITH a.id = ip.ref AND a.cId = ip.course
  218. WHERE
  219. ip.tool = 'announcement' AND
  220. a.cId = :course AND
  221. a.sessionId = :session AND
  222. a.title like :title AND
  223. ip.visibility = :visibility
  224. ORDER BY a.displayOrder DESC";
  225. $qb = Database::getManager()->createQuery($dql);
  226. $result = $qb->execute(
  227. [
  228. 'course' => $courseId,
  229. 'session' => $sessionId,
  230. 'visibility' => $visibility,
  231. 'title' => "%$title%",
  232. ]
  233. );
  234. return $result;
  235. }
  236. /**
  237. * @param int $announcementId
  238. * @param int $courseId
  239. * @param int $userId
  240. *
  241. * @return array
  242. */
  243. public static function getAnnouncementInfoById(
  244. $announcementId,
  245. $courseId,
  246. $userId
  247. ) {
  248. if (api_is_allowed_to_edit(false, true) ||
  249. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  250. ) {
  251. $dql = "SELECT a, ip
  252. FROM ChamiloCourseBundle:CAnnouncement a
  253. JOIN ChamiloCourseBundle:CItemProperty ip
  254. WITH a.id = ip.ref AND a.cId = ip.course
  255. WHERE
  256. a.id = :announcement AND
  257. ip.tool = 'announcement' AND
  258. a.cId = :course
  259. ORDER BY a.displayOrder DESC";
  260. } else {
  261. $group_list = GroupManager::get_group_ids($courseId, api_get_user_id());
  262. if (empty($group_list)) {
  263. $group_list[] = 0;
  264. }
  265. if (api_get_user_id() != 0) {
  266. $dql = "SELECT a, ip
  267. FROM ChamiloCourseBundle:CAnnouncement a
  268. JOIN ChamiloCourseBundle:CItemProperty ip
  269. WITH a.id = ip.ref AND a.cId = ip.course
  270. WHERE
  271. a.id = :announcement AND
  272. ip.tool='announcement' AND
  273. (
  274. ip.toUser = $userId OR
  275. ip.group IN ('0', '".implode("', '", $group_list)."') OR
  276. ip.group IS NULL
  277. ) AND
  278. ip.visibility = '1' AND
  279. ip.course = :course
  280. ORDER BY a.displayOrder DESC";
  281. } else {
  282. $dql = "SELECT a, ip
  283. FROM ChamiloCourseBundle:CAnnouncement a
  284. JOIN ChamiloCourseBundle:CItemProperty ip
  285. WITH a.id = ip.ref AND a.cId = ip.course
  286. WHERE
  287. a.id = :announcement AND
  288. ip.tool = 'announcement' AND
  289. (ip.group = '0' OR ip.group IS NULL) AND
  290. ip.visibility = '1' AND
  291. ip.course = :course";
  292. }
  293. }
  294. $qb = Database::getManager()->createQuery($dql);
  295. $result = $qb->execute(
  296. [
  297. 'announcement' => $announcementId,
  298. 'course' => $courseId,
  299. ]
  300. );
  301. return [
  302. 'announcement' => $result[0],
  303. 'item_property' => $result[1]
  304. ];
  305. }
  306. /**
  307. * Displays one specific announcement
  308. * @param int $id, the id of the announcement you want to display
  309. *
  310. * @return string
  311. */
  312. public static function displayAnnouncement($id)
  313. {
  314. if ($id != strval(intval($id))) {
  315. return null;
  316. }
  317. global $charset;
  318. $html = '';
  319. $result = self::getAnnouncementInfoById(
  320. $id,
  321. api_get_course_int_id(),
  322. api_get_user_id()
  323. );
  324. /** @var CAnnouncement $announcement */
  325. $announcement = $result['announcement'];
  326. /** @var CItemProperty $itemProperty */
  327. $itemProperty = $result['item_property'];
  328. if (empty($announcement) || empty($itemProperty)) {
  329. return '';
  330. }
  331. $title = $announcement->getTitle();
  332. $content = $announcement->getContent();
  333. $html .= "<table height=\"100\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\" class=\"data_table\">";
  334. $html .= "<tr><td><h2>".$title."</h2></td></tr>";
  335. if (api_is_allowed_to_edit(false, true) ||
  336. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  337. ) {
  338. $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$id."\">".
  339. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL)."</a>";
  340. if ($itemProperty->getVisibility() === 1) {
  341. $image_visibility = 'visible';
  342. $alt_visibility = get_lang('Hide');
  343. } else {
  344. $image_visibility = 'invisible';
  345. $alt_visibility = get_lang('Visible');
  346. }
  347. global $stok;
  348. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=showhide&id=".$id."&sec_token=".$stok."\">".
  349. Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>";
  350. if (api_is_allowed_to_edit(false, true)) {
  351. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=delete&id=".$id."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, $charset))."')) return false;\">".
  352. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).
  353. "</a>";
  354. }
  355. $html .= "<tr><th style='text-align:right'>$modify_icons</th></tr>";
  356. }
  357. $toUser = $itemProperty->getToUser();
  358. $toUserId = !empty($toUser) ? $toUser->getId() : 0;
  359. $content = self::parse_content(
  360. $toUserId,
  361. $content,
  362. api_get_course_id(),
  363. api_get_session_id()
  364. );
  365. $html .= "<tr><td>$content</td></tr>";
  366. $html .= "<tr>";
  367. $html .= "<td class=\"announcements_datum\">".get_lang('LastUpdateDate')." : ";
  368. $lastEdit = $itemProperty->getLasteditDate();
  369. $html .= Display::dateToStringAgoAndLongDate($lastEdit);
  370. $html .= "</td></tr>";
  371. if (api_is_allowed_to_edit(false, true)) {
  372. $sent_to = self::sent_to('announcement', $id);
  373. $sent_to_form = self::sent_to_form($sent_to);
  374. $html .= Display::tag(
  375. 'td',
  376. get_lang('SentTo').': '.$sent_to_form,
  377. array('class' => 'announcements_datum')
  378. );
  379. }
  380. $attachment_list = self::get_attachment($id);
  381. if (count($attachment_list) > 0) {
  382. $html .= "<tr><td>";
  383. $realname = $attachment_list['path'];
  384. $user_filename = $attachment_list['filename'];
  385. $full_file_name = 'download.php?'.api_get_cidreq().'&file='.$realname;
  386. $html .= '<br/>';
  387. $html .= Display::return_icon('attachment.gif', get_lang('Attachment'));
  388. $html .= '<a href="'.$full_file_name.' "> '.$user_filename.' </a>';
  389. $html .= ' - <span class="forum_attach_comment" >'.$attachment_list['comment'].'</span>';
  390. if (api_is_allowed_to_edit(false, true)) {
  391. $html .= Display::url(
  392. Display::return_icon(
  393. 'delete.png',
  394. get_lang('Delete'),
  395. '',
  396. 16
  397. ),
  398. api_get_self()."?".api_get_cidreq()."&action=delete_attachment&id_attach=".$attachment_list['id']."&sec_token=".$stok
  399. );
  400. }
  401. $html .= '</td></tr>';
  402. }
  403. $html .= "</table>";
  404. return $html;
  405. }
  406. /**
  407. * @param array $courseInfo
  408. *
  409. * @return int
  410. */
  411. public static function get_last_announcement_order($courseInfo)
  412. {
  413. if (empty($courseInfo)) {
  414. return 0;
  415. }
  416. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  417. $course_id = $courseInfo['real_id'];
  418. $sql = "SELECT MAX(display_order)
  419. FROM $tbl_announcement
  420. WHERE c_id = $course_id ";
  421. $res_max = Database::query($sql);
  422. $order = 0;
  423. if (Database::num_rows($res_max)) {
  424. $row_max = Database::fetch_array($res_max);
  425. $order = intval($row_max[0]) + 1;
  426. }
  427. return $order;
  428. }
  429. /**
  430. * Store an announcement in the database (including its attached file if any)
  431. * @param array $courseInfo
  432. * @param int $sessionId
  433. * @param string $title Announcement title (pure text)
  434. * @param string $newContent Content of the announcement (can be HTML)
  435. * @param array $sentTo Array of users and groups to send the announcement to
  436. * @param array $file uploaded file $_FILES
  437. * @param string $file_comment Comment describing the attachment
  438. * @param string $end_date
  439. * @param bool $sendToUsersInSession
  440. * @param int $authorId
  441. *
  442. * @return int false on failure, ID of the announcement on success
  443. */
  444. public static function add_announcement(
  445. $courseInfo,
  446. $sessionId,
  447. $title,
  448. $newContent,
  449. $sentTo,
  450. $file = array(),
  451. $file_comment = null,
  452. $end_date = null,
  453. $sendToUsersInSession = false,
  454. $authorId = 0
  455. ) {
  456. if (empty($courseInfo)) {
  457. return false;
  458. }
  459. $course_id = $courseInfo['real_id'];
  460. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  461. $authorId = empty($authorId) ? api_get_user_id() : $authorId;
  462. if (empty($end_date)) {
  463. $end_date = api_get_utc_datetime();
  464. }
  465. $order = self::get_last_announcement_order($courseInfo);
  466. // store in the table announcement
  467. $params = array(
  468. 'c_id' => $course_id,
  469. 'content' => $newContent,
  470. 'title' => $title,
  471. 'end_date' => $end_date,
  472. 'display_order' => $order,
  473. 'session_id' => (int) $sessionId
  474. );
  475. $last_id = Database::insert($tbl_announcement, $params);
  476. if (empty($last_id)) {
  477. return false;
  478. } else {
  479. $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
  480. Database::query($sql);
  481. if (!empty($file)) {
  482. self::add_announcement_attachment_file(
  483. $last_id,
  484. $file_comment,
  485. $_FILES['user_upload']
  486. );
  487. }
  488. // store in item_property (first the groups, then the users
  489. if (empty($sentTo) || (!empty($sentTo) && isset($sentTo[0]) && $sentTo[0] == 'everyone')) {
  490. // The message is sent to EVERYONE, so we set the group to 0
  491. api_item_property_update(
  492. $courseInfo,
  493. TOOL_ANNOUNCEMENT,
  494. $last_id,
  495. 'AnnouncementAdded',
  496. $authorId,
  497. '0',
  498. null,
  499. null,
  500. null,
  501. $sessionId
  502. );
  503. } else {
  504. $send_to = CourseManager::separateUsersGroups($sentTo);
  505. $batchSize = 20;
  506. $em = Database::getManager();
  507. // Storing the selected groups
  508. if (is_array($send_to['groups']) && !empty($send_to['groups'])) {
  509. $counter = 1;
  510. foreach ($send_to['groups'] as $group) {
  511. $groupInfo = GroupManager::get_group_properties($group);
  512. api_item_property_update(
  513. $courseInfo,
  514. TOOL_ANNOUNCEMENT,
  515. $last_id,
  516. 'AnnouncementAdded',
  517. $authorId,
  518. $groupInfo
  519. );
  520. if (($counter % $batchSize) === 0) {
  521. $em->flush();
  522. $em->clear();
  523. }
  524. $counter++;
  525. }
  526. }
  527. // Storing the selected users
  528. if (is_array($send_to['users'])) {
  529. $counter = 1;
  530. foreach ($send_to['users'] as $user) {
  531. api_item_property_update(
  532. $courseInfo,
  533. TOOL_ANNOUNCEMENT,
  534. $last_id,
  535. 'AnnouncementAdded',
  536. $authorId,
  537. '',
  538. $user
  539. );
  540. if (($counter % $batchSize) === 0) {
  541. $em->flush();
  542. $em->clear();
  543. }
  544. $counter++;
  545. }
  546. }
  547. }
  548. if ($sendToUsersInSession) {
  549. self::addAnnouncementToAllUsersInSessions($last_id);
  550. }
  551. return $last_id;
  552. }
  553. }
  554. /**
  555. * @param $title
  556. * @param $newContent
  557. * @param $to
  558. * @param $to_users
  559. * @param array $file
  560. * @param string $file_comment
  561. * @param bool $sendToUsersInSession
  562. *
  563. * @return bool|int
  564. */
  565. public static function add_group_announcement(
  566. $title,
  567. $newContent,
  568. $to,
  569. $to_users,
  570. $file = array(),
  571. $file_comment = '',
  572. $sendToUsersInSession = false
  573. ) {
  574. $_course = api_get_course_info();
  575. // Database definitions
  576. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  577. $order = self::get_last_announcement_order($_course);
  578. $now = api_get_utc_datetime();
  579. $course_id = api_get_course_int_id();
  580. // store in the table announcement
  581. $params = [
  582. 'c_id' => $course_id,
  583. 'content' => $newContent,
  584. 'title' => $title,
  585. 'end_date' => $now,
  586. 'display_order' => $order,
  587. 'session_id' => api_get_session_id()
  588. ];
  589. $last_id = Database::insert($tbl_announcement, $params);
  590. // Store the attach file
  591. if ($last_id) {
  592. $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
  593. Database::query($sql);
  594. if (!empty($file)) {
  595. self::add_announcement_attachment_file(
  596. $last_id,
  597. $file_comment,
  598. $file
  599. );
  600. }
  601. // Store in item_property (first the groups, then the users)
  602. //if (!isset($to_users)) {
  603. if (isset($to_users[0]) && $to_users[0] === 'everyone') {
  604. // when no user is selected we send it to everyone
  605. $send_to = CourseManager::separateUsersGroups($to);
  606. // storing the selected groups
  607. if (is_array($send_to['groups'])) {
  608. foreach ($send_to['groups'] as $group) {
  609. $groupInfo = GroupManager::get_group_properties($group);
  610. api_item_property_update(
  611. $_course,
  612. TOOL_ANNOUNCEMENT,
  613. $last_id,
  614. 'AnnouncementAdded',
  615. api_get_user_id(),
  616. $groupInfo
  617. );
  618. }
  619. }
  620. } else {
  621. $send_to_groups = CourseManager::separateUsersGroups($to);
  622. $send_to_users = CourseManager::separateUsersGroups($to_users);
  623. $to_groups = $send_to_groups['groups'];
  624. $to_users = $send_to_users['users'];
  625. // storing the selected users
  626. if (is_array($to_users) && is_array($to_groups)) {
  627. foreach ($to_groups as $group) {
  628. $groupInfo = GroupManager::get_group_properties($group);
  629. foreach ($to_users as $user) {
  630. api_item_property_update(
  631. $_course,
  632. TOOL_ANNOUNCEMENT,
  633. $last_id,
  634. 'AnnouncementAdded',
  635. api_get_user_id(),
  636. $groupInfo,
  637. $user
  638. );
  639. }
  640. }
  641. }
  642. }
  643. if ($sendToUsersInSession) {
  644. self::addAnnouncementToAllUsersInSessions($last_id);
  645. }
  646. }
  647. return $last_id;
  648. }
  649. /**
  650. * This function stores the announcement item in the announcement table
  651. * and updates the item_property table
  652. *
  653. * @param int $id id of the announcement
  654. * @param string $title
  655. * @param string $newContent
  656. * @param array $to users that will receive the announcement
  657. * @param mixed $file attachment
  658. * @param string $file_comment file comment
  659. * @param bool $sendToUsersInSession
  660. */
  661. public static function edit_announcement(
  662. $id,
  663. $title,
  664. $newContent,
  665. $to,
  666. $file = array(),
  667. $file_comment = '',
  668. $sendToUsersInSession = false
  669. ) {
  670. $_course = api_get_course_info();
  671. $course_id = api_get_course_int_id();
  672. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  673. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  674. $id = intval($id);
  675. $params = [
  676. 'title' => $title,
  677. 'content' => $newContent
  678. ];
  679. Database::update(
  680. $tbl_announcement,
  681. $params,
  682. ['c_id = ? AND id = ?' => [$course_id, $id]]
  683. );
  684. // save attachment file
  685. $row_attach = self::get_attachment($id);
  686. $id_attach = 0;
  687. if ($row_attach) {
  688. $id_attach = intval($row_attach['id']);
  689. }
  690. if (!empty($file)) {
  691. if (empty($id_attach)) {
  692. self::add_announcement_attachment_file($id, $file_comment, $file);
  693. } else {
  694. self::edit_announcement_attachment_file($id_attach, $file, $file_comment);
  695. }
  696. }
  697. // We remove everything from item_property for this
  698. $sql = "DELETE FROM $tbl_item_property
  699. WHERE c_id = $course_id AND ref='$id' AND tool='announcement'";
  700. Database::query($sql);
  701. if ($sendToUsersInSession) {
  702. self::addAnnouncementToAllUsersInSessions($id);
  703. }
  704. // store in item_property (first the groups, then the users
  705. if (!is_null($to)) {
  706. // !is_null($to): when no user is selected we send it to everyone
  707. $send_to = CourseManager::separateUsersGroups($to);
  708. // storing the selected groups
  709. if (is_array($send_to['groups'])) {
  710. foreach ($send_to['groups'] as $group) {
  711. $groupInfo = GroupManager::get_group_properties($group);
  712. api_item_property_update(
  713. $_course,
  714. TOOL_ANNOUNCEMENT,
  715. $id,
  716. 'AnnouncementUpdated',
  717. api_get_user_id(),
  718. $groupInfo
  719. );
  720. }
  721. }
  722. // storing the selected users
  723. if (is_array($send_to['users'])) {
  724. foreach ($send_to['users'] as $user) {
  725. api_item_property_update(
  726. $_course,
  727. TOOL_ANNOUNCEMENT,
  728. $id,
  729. 'AnnouncementUpdated',
  730. api_get_user_id(),
  731. 0,
  732. $user
  733. );
  734. }
  735. }
  736. // Send to everyone
  737. if (isset($to[0]) && $to[0] === 'everyone') {
  738. api_item_property_update(
  739. $_course,
  740. TOOL_ANNOUNCEMENT,
  741. $id,
  742. 'AnnouncementUpdated',
  743. api_get_user_id(),
  744. 0
  745. );
  746. }
  747. } else {
  748. // the message is sent to everyone, so we set the group to 0
  749. api_item_property_update(
  750. $_course,
  751. TOOL_ANNOUNCEMENT,
  752. $id,
  753. 'AnnouncementUpdated',
  754. api_get_user_id(),
  755. 0
  756. );
  757. }
  758. }
  759. /**
  760. * @param int $announcementId
  761. */
  762. public static function addAnnouncementToAllUsersInSessions($announcementId)
  763. {
  764. $courseCode = api_get_course_id();
  765. $_course = api_get_course_info();
  766. $sessionList = SessionManager::get_session_by_course(api_get_course_int_id());
  767. if (!empty($sessionList)) {
  768. foreach ($sessionList as $sessionInfo) {
  769. $sessionId = $sessionInfo['id'];
  770. $userList = CourseManager::get_user_list_from_course_code(
  771. $courseCode,
  772. $sessionId
  773. );
  774. if (!empty($userList)) {
  775. foreach ($userList as $user) {
  776. api_item_property_update(
  777. $_course,
  778. TOOL_ANNOUNCEMENT,
  779. $announcementId,
  780. "AnnouncementUpdated",
  781. api_get_user_id(),
  782. 0,
  783. $user['user_id'],
  784. 0,
  785. 0,
  786. $sessionId
  787. );
  788. }
  789. }
  790. }
  791. }
  792. }
  793. /**
  794. * @param int $insert_id
  795. * @return bool
  796. */
  797. public static function update_mail_sent($insert_id)
  798. {
  799. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  800. if ($insert_id != strval(intval($insert_id))) {
  801. return false;
  802. }
  803. $insert_id = intval($insert_id);
  804. $course_id = api_get_course_int_id();
  805. // store the modifications in the table tbl_annoucement
  806. $sql = "UPDATE $tbl_announcement SET email_sent='1'
  807. WHERE c_id = $course_id AND id = $insert_id";
  808. Database::query($sql);
  809. }
  810. /**
  811. * Gets all announcements from a user by course
  812. * @param string course db
  813. * @param int user id
  814. * @return array html with the content and count of announcements or false otherwise
  815. */
  816. public static function get_all_annoucement_by_user_course($course_code, $user_id)
  817. {
  818. $course_info = api_get_course_info($course_code);
  819. $course_id = $course_info['real_id'];
  820. if (empty($user_id)) {
  821. return false;
  822. }
  823. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  824. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  825. if (!empty($user_id) && is_numeric($user_id)) {
  826. $user_id = (int) $user_id;
  827. $sql = "SELECT DISTINCT
  828. announcement.title,
  829. announcement.content,
  830. display_order
  831. FROM $tbl_announcement announcement
  832. INNER JOIN $tbl_item_property ip
  833. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  834. WHERE
  835. announcement.c_id = $course_id AND
  836. ip.c_id = $course_id AND
  837. ip.tool='announcement' AND
  838. (
  839. ip.insert_user_id='$user_id' AND
  840. (ip.to_group_id='0' OR ip.to_group_id IS NULL)
  841. )
  842. AND ip.visibility='1'
  843. AND announcement.session_id = 0
  844. ORDER BY display_order DESC";
  845. $rs = Database::query($sql);
  846. $num_rows = Database::num_rows($rs);
  847. $content = '';
  848. $i = 0;
  849. $result = array();
  850. if ($num_rows > 0) {
  851. while ($myrow = Database::fetch_array($rs)) {
  852. $content .= '<strong>'.$myrow['title'].'</strong><br /><br />';
  853. $content .= $myrow['content'];
  854. $i++;
  855. }
  856. $result['content'] = $content;
  857. $result['count'] = $i;
  858. return $result;
  859. }
  860. return false;
  861. }
  862. return false;
  863. }
  864. /**
  865. * Returns announcement info from its id
  866. *
  867. * @param int $course_id
  868. * @param int $annoucement_id
  869. * @return array
  870. */
  871. public static function get_by_id($course_id, $annoucement_id)
  872. {
  873. $annoucement_id = intval($annoucement_id);
  874. $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
  875. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  876. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  877. $sql = "SELECT DISTINCT
  878. announcement.id,
  879. announcement.title,
  880. announcement.content
  881. FROM $tbl_announcement announcement
  882. INNER JOIN $tbl_item_property ip
  883. ON
  884. announcement.id = ip.ref AND
  885. announcement.c_id = ip.c_id
  886. WHERE
  887. announcement.c_id = $course_id AND
  888. ip.tool='announcement' AND
  889. announcement.id = $annoucement_id
  890. ";
  891. $result = Database::query($sql);
  892. if (Database::num_rows($result)) {
  893. return Database::fetch_array($result);
  894. }
  895. return [];
  896. }
  897. /**
  898. * this function gets all the groups of the course,
  899. * not including linked courses
  900. */
  901. public static function get_course_groups()
  902. {
  903. $session_id = api_get_session_id();
  904. if ($session_id != 0) {
  905. $new_group_list = CourseManager::get_group_list_of_course(
  906. api_get_course_id(),
  907. $session_id,
  908. 1
  909. );
  910. } else {
  911. $new_group_list = CourseManager::get_group_list_of_course(
  912. api_get_course_id(),
  913. 0,
  914. 1
  915. );
  916. }
  917. return $new_group_list;
  918. }
  919. /**
  920. * This tools loads all the users and all the groups who have received
  921. * a specific item (in this case an announcement item)
  922. * @param string $tool
  923. * @param int $id
  924. * @return array
  925. */
  926. public static function load_edit_users($tool, $id)
  927. {
  928. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  929. $tool = Database::escape_string($tool);
  930. $id = intval($id);
  931. $course_id = api_get_course_int_id();
  932. $sql = "SELECT * FROM $tbl_item_property
  933. WHERE c_id = $course_id AND tool='$tool' AND ref = $id";
  934. $result = Database::query($sql);
  935. $to = array();
  936. while ($row = Database::fetch_array($result)) {
  937. $to_group = $row['to_group_id'];
  938. switch ($to_group) {
  939. // it was send to one specific user
  940. case null:
  941. $to[] = "USER:".$row['to_user_id'];
  942. break;
  943. // it was sent to everyone
  944. case 0:
  945. return "everyone";
  946. break;
  947. default:
  948. $to[] = "GROUP:".$row['to_group_id'];
  949. }
  950. }
  951. return $to;
  952. }
  953. /**
  954. * constructs the form to display all the groups and users the message has been sent to
  955. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  956. * the first level is a distinction between groups and users:
  957. * $sent_to_array['groups'] * and $sent_to_array['users']
  958. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  959. * containing all the id's of the groups (resp. users) who have received this message.
  960. * @author Patrick Cool <patrick.cool@>
  961. */
  962. public static function sent_to_form($sent_to_array)
  963. {
  964. // we find all the names of the groups
  965. $group_names = self::get_course_groups();
  966. // we count the number of users and the number of groups
  967. if (isset($sent_to_array['users'])) {
  968. $number_users = count($sent_to_array['users']);
  969. } else {
  970. $number_users = 0;
  971. }
  972. if (isset($sent_to_array['groups'])) {
  973. $number_groups = count($sent_to_array['groups']);
  974. } else {
  975. $number_groups = 0;
  976. }
  977. $total_numbers = $number_users + $number_groups;
  978. // starting the form if there is more than one user/group
  979. $output = array();
  980. if ($total_numbers > 1) {
  981. // outputting the name of the groups
  982. if (is_array($sent_to_array['groups'])) {
  983. foreach ($sent_to_array['groups'] as $group_id) {
  984. $output[] = $group_names[$group_id]['name'];
  985. }
  986. }
  987. if (isset($sent_to_array['users'])) {
  988. if (is_array($sent_to_array['users'])) {
  989. foreach ($sent_to_array['users'] as $user_id) {
  990. $user_info = api_get_user_info($user_id);
  991. $output[] = $user_info['complete_name_with_username'];
  992. }
  993. }
  994. }
  995. } else {
  996. // there is only one user/group
  997. if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) {
  998. $user_info = api_get_user_info($sent_to_array['users'][0]);
  999. $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  1000. }
  1001. if (isset($sent_to_array['groups']) and
  1002. is_array($sent_to_array['groups']) and
  1003. isset($sent_to_array['groups'][0]) and
  1004. $sent_to_array['groups'][0] !== 0
  1005. ) {
  1006. $group_id = $sent_to_array['groups'][0];
  1007. $output[] = "&nbsp;".$group_names[$group_id]['name'];
  1008. }
  1009. if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) {
  1010. $output[] = "&nbsp;".get_lang('Everybody');
  1011. }
  1012. }
  1013. if (!empty($output)) {
  1014. $output = array_filter($output);
  1015. if (count($output) > 0) {
  1016. $output = implode(', ', $output);
  1017. }
  1018. return $output;
  1019. }
  1020. }
  1021. /**
  1022. * Returns all the users and all the groups a specific announcement item
  1023. * has been sent to
  1024. * @param string The tool (announcement, agenda, ...)
  1025. * @param int ID of the element of the corresponding type
  1026. * @return array Array of users and groups to whom the element has been sent
  1027. */
  1028. public static function sent_to($tool, $id)
  1029. {
  1030. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1031. $tool = Database::escape_string($tool);
  1032. $id = (int) $id;
  1033. $sent_to_group = array();
  1034. $sent_to = array();
  1035. $course_id = api_get_course_int_id();
  1036. $sql = "SELECT to_group_id, to_user_id
  1037. FROM $tbl_item_property
  1038. WHERE c_id = $course_id AND tool = '$tool' AND ref=".$id;
  1039. $result = Database::query($sql);
  1040. while ($row = Database::fetch_array($result)) {
  1041. // if to_user_id <> 0 then it is sent to a specific user
  1042. if ($row['to_user_id'] <> 0) {
  1043. $sent_to_user[] = $row['to_user_id'];
  1044. continue;
  1045. }
  1046. // if to_group_id is null then it is sent to a specific user
  1047. // if to_group_id = 0 then it is sent to everybody
  1048. if ($row['to_group_id'] != 0) {
  1049. $sent_to_group[] = $row['to_group_id'];
  1050. }
  1051. }
  1052. if (isset($sent_to_group)) {
  1053. $sent_to['groups'] = $sent_to_group;
  1054. }
  1055. if (isset($sent_to_user)) {
  1056. $sent_to['users'] = $sent_to_user;
  1057. }
  1058. return $sent_to;
  1059. }
  1060. /**
  1061. * Show a list with all the attachments according to the post's id
  1062. * @param int $announcementId
  1063. * @return array with the post info
  1064. * @author Arthur Portugal
  1065. * @version November 2009, dokeos 1.8.6.2
  1066. */
  1067. public static function get_attachment($announcementId)
  1068. {
  1069. $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1070. $announcementId = intval($announcementId);
  1071. $course_id = api_get_course_int_id();
  1072. $row = array();
  1073. $sql = 'SELECT id, path, filename, comment
  1074. FROM '.$table.'
  1075. WHERE c_id = ' . $course_id.' AND announcement_id = '.$announcementId;
  1076. $result = Database::query($sql);
  1077. if (Database::num_rows($result) != 0) {
  1078. $row = Database::fetch_array($result, 'ASSOC');
  1079. }
  1080. return $row;
  1081. }
  1082. /**
  1083. * This function add a attachment file into announcement
  1084. * @param int announcement id
  1085. * @param string file comment
  1086. * @param array uploaded file $_FILES
  1087. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  1088. */
  1089. public static function add_announcement_attachment_file(
  1090. $announcement_id,
  1091. $file_comment,
  1092. $file
  1093. ) {
  1094. $_course = api_get_course_info();
  1095. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1096. $return = 0;
  1097. $announcement_id = intval($announcement_id);
  1098. $course_id = api_get_course_int_id();
  1099. if (is_array($file) && $file['error'] == 0) {
  1100. // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1101. $courseDir = $_course['path'].'/upload/announcements';
  1102. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1103. $updir = $sys_course_path.$courseDir;
  1104. // Try to add an extension to the file if it hasn't one
  1105. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1106. // user's file name
  1107. $file_name = $file['name'];
  1108. if (!filter_extension($new_file_name)) {
  1109. $return = -1;
  1110. echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
  1111. } else {
  1112. $new_file_name = uniqid('');
  1113. $new_path = $updir.'/'.$new_file_name;
  1114. // This file is copy here but its cleaned in api_mail_html in api.lib.php
  1115. copy($file['tmp_name'], $new_path);
  1116. $params = [
  1117. 'c_id' => $course_id,
  1118. 'filename' => $file_name,
  1119. 'comment' => $file_comment,
  1120. 'path' => $new_file_name,
  1121. 'announcement_id' => $announcement_id,
  1122. 'size' => intval($file['size']),
  1123. ];
  1124. $insertId = Database::insert($tbl_announcement_attachment, $params);
  1125. if ($insertId) {
  1126. $sql = "UPDATE $tbl_announcement_attachment SET id = iid
  1127. WHERE iid = $insertId";
  1128. Database::query($sql);
  1129. }
  1130. $return = 1;
  1131. }
  1132. }
  1133. return $return;
  1134. }
  1135. /**
  1136. * This function edit a attachment file into announcement
  1137. * @param int attach id
  1138. * @param array uploaded file $_FILES
  1139. * @param string file comment
  1140. * @return int
  1141. */
  1142. public static function edit_announcement_attachment_file(
  1143. $id_attach,
  1144. $file,
  1145. $file_comment
  1146. ) {
  1147. $_course = api_get_course_info();
  1148. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1149. $return = 0;
  1150. $course_id = api_get_course_int_id();
  1151. if (is_array($file) && $file['error'] == 0) {
  1152. // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1153. $courseDir = $_course['path'].'/upload/announcements';
  1154. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1155. $updir = $sys_course_path.$courseDir;
  1156. // Try to add an extension to the file if it hasn't one
  1157. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1158. // user's file name
  1159. $file_name = $file ['name'];
  1160. if (!filter_extension($new_file_name)) {
  1161. $return = -1;
  1162. echo Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'error');
  1163. } else {
  1164. $new_file_name = uniqid('');
  1165. $new_path = $updir.'/'.$new_file_name;
  1166. copy($file['tmp_name'], $new_path);
  1167. $safe_file_comment = Database::escape_string($file_comment);
  1168. $safe_file_name = Database::escape_string($file_name);
  1169. $safe_new_file_name = Database::escape_string($new_file_name);
  1170. $id_attach = intval($id_attach);
  1171. $sql = "UPDATE $tbl_announcement_attachment SET
  1172. filename = '$safe_file_name',
  1173. comment = '$safe_file_comment',
  1174. path = '$safe_new_file_name',
  1175. size ='".intval($file['size'])."'
  1176. WHERE c_id = $course_id AND id = '$id_attach'";
  1177. $result = Database::query($sql);
  1178. if ($result === false) {
  1179. $return = -1;
  1180. echo Display::return_message(get_lang('UplUnableToSaveFile'), 'error');
  1181. } else {
  1182. $return = 1;
  1183. }
  1184. }
  1185. }
  1186. return $return;
  1187. }
  1188. /**
  1189. * This function delete a attachment file by id
  1190. * @param integer $id attachment file Id
  1191. * @return bool
  1192. */
  1193. public static function delete_announcement_attachment_file($id)
  1194. {
  1195. $table = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1196. $id = intval($id);
  1197. $course_id = api_get_course_int_id();
  1198. if (empty($course_id) || empty($id)) {
  1199. return false;
  1200. }
  1201. $sql = "DELETE FROM $table
  1202. WHERE c_id = $course_id AND id = $id";
  1203. Database::query($sql);
  1204. return true;
  1205. }
  1206. /**
  1207. * @param array $courseInfo
  1208. * @param int $sessionId
  1209. * @param int $id
  1210. * @param bool $sendToUsersInSession
  1211. * @param bool $sendToDrhUsers
  1212. */
  1213. public static function sendEmail(
  1214. $courseInfo,
  1215. $sessionId,
  1216. $id,
  1217. $sendToUsersInSession = false,
  1218. $sendToDrhUsers = false
  1219. ) {
  1220. $email = AnnouncementEmail::create($courseInfo, $sessionId, $id);
  1221. $email->send($sendToUsersInSession, $sendToDrhUsers);
  1222. }
  1223. /**
  1224. * @param $stok
  1225. * @param $announcement_number
  1226. * @param bool $getCount
  1227. * @param null $start
  1228. * @param null $limit
  1229. * @param string $sidx
  1230. * @param string $sord
  1231. * @param string $titleToSearch
  1232. * @param int $userIdToSearch
  1233. * @param int $userId
  1234. * @param int $courseId
  1235. * @param int $sessionId
  1236. * @return array
  1237. */
  1238. public static function getAnnouncements(
  1239. $stok,
  1240. $announcement_number,
  1241. $getCount = false,
  1242. $start = null,
  1243. $limit = null,
  1244. $sidx = '',
  1245. $sord = '',
  1246. $titleToSearch = '',
  1247. $userIdToSearch = 0,
  1248. $userId = 0,
  1249. $courseId = 0,
  1250. $sessionId = 0
  1251. ) {
  1252. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1253. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1254. $user_id = $userId ?: api_get_user_id();
  1255. $group_id = api_get_group_id();
  1256. $session_id = $sessionId ?: api_get_session_id();
  1257. $condition_session = api_get_session_condition(
  1258. $session_id,
  1259. true,
  1260. true,
  1261. 'announcement.session_id'
  1262. );
  1263. $course_id = $courseId ?: api_get_course_int_id();
  1264. $_course = api_get_course_info();
  1265. $group_memberships = GroupManager::get_group_ids(
  1266. $course_id,
  1267. api_get_user_id()
  1268. );
  1269. $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement');
  1270. $select = ' DISTINCT
  1271. announcement.*,
  1272. ip.visibility,
  1273. ip.to_group_id,
  1274. ip.insert_user_id,
  1275. ip.insert_date,
  1276. ip.lastedit_date';
  1277. if ($getCount) {
  1278. $select = ' COUNT(DISTINCT announcement.iid) count';
  1279. }
  1280. $searchCondition = '';
  1281. if (!empty($titleToSearch)) {
  1282. $titleToSearch = Database::escape_string($titleToSearch);
  1283. $searchCondition .= " AND (title LIKE '%$titleToSearch%')";
  1284. }
  1285. if (!empty($userIdToSearch)) {
  1286. $userIdToSearch = intval($userIdToSearch);
  1287. $searchCondition .= " AND (ip.insert_user_id = $userIdToSearch)";
  1288. }
  1289. $allowOnlyGroup = api_get_configuration_value('hide_base_course_announcements_in_group');
  1290. $extraGroupCondition = '';
  1291. if ($allowOnlyGroup) {
  1292. $extraGroupCondition = " AND ip.to_group_id = $group_id ";
  1293. }
  1294. if (api_is_allowed_to_edit(false, true) ||
  1295. ($allowUserEditSetting && !api_is_anonymous())
  1296. ) {
  1297. // A.1. you are a course admin with a USER filter
  1298. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  1299. //if (!empty($user_id)) {
  1300. if (0) {
  1301. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1302. $sql = "SELECT $select
  1303. FROM $tbl_announcement announcement
  1304. INNER JOIN $tbl_item_property ip
  1305. ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id)
  1306. WHERE
  1307. announcement.c_id = $course_id AND
  1308. ip.c_id = $course_id AND
  1309. ip.tool = 'announcement' AND
  1310. (
  1311. ip.to_user_id = $user_id OR
  1312. ip.to_group_id IS NULL OR
  1313. ip.to_group_id IN (0, ".implode(", ", $group_memberships).")
  1314. ) AND
  1315. ip.visibility IN ('1', '0')
  1316. $condition_session
  1317. $searchCondition
  1318. ORDER BY display_order DESC";
  1319. } else {
  1320. $sql = "SELECT $select
  1321. FROM $tbl_announcement announcement
  1322. INNER JOIN $tbl_item_property ip
  1323. ON (announcement.id = ip.ref AND ip.c_id = announcement.c_id)
  1324. WHERE
  1325. announcement.c_id = $course_id AND
  1326. ip.c_id = $course_id AND
  1327. ip.tool ='announcement' AND
  1328. (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND
  1329. ip.visibility IN ('1', '0')
  1330. $condition_session
  1331. $searchCondition
  1332. ORDER BY display_order DESC";
  1333. }
  1334. } elseif ($group_id != 0) {
  1335. // A.2. you are a course admin with a GROUP filter
  1336. // => see only the messages of this specific group
  1337. $sql = "SELECT $select
  1338. FROM $tbl_announcement announcement
  1339. INNER JOIN $tbl_item_property ip
  1340. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1341. WHERE
  1342. ip.tool='announcement' AND
  1343. announcement.c_id = $course_id AND
  1344. ip.c_id = $course_id AND
  1345. ip.visibility<>'2' AND
  1346. (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  1347. $condition_session
  1348. $searchCondition
  1349. $extraGroupCondition
  1350. ORDER BY display_order DESC";
  1351. //GROUP BY ip.ref
  1352. } else {
  1353. // A.3 you are a course admin without any group or user filter
  1354. // A.3.a you are a course admin without user or group filter but WITH studentview
  1355. // => see all the messages of all the users and groups without editing possibilities
  1356. if (isset($isStudentView) && $isStudentView == 'true') {
  1357. $sql = "SELECT $select
  1358. FROM $tbl_announcement announcement
  1359. INNER JOIN $tbl_item_property ip
  1360. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1361. WHERE
  1362. ip.tool='announcement' AND
  1363. announcement.c_id = $course_id AND
  1364. ip.c_id = $course_id AND
  1365. ip.visibility='1'
  1366. $condition_session
  1367. $searchCondition
  1368. ORDER BY display_order DESC";
  1369. //GROUP BY ip.ref
  1370. } else {
  1371. // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view)
  1372. // => see all the messages of all the users and groups with editing possibilities
  1373. $sql = "SELECT $select
  1374. FROM $tbl_announcement announcement
  1375. INNER JOIN $tbl_item_property ip
  1376. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1377. WHERE
  1378. ip.tool = 'announcement' AND
  1379. announcement.c_id = $course_id AND
  1380. ip.c_id = $course_id AND
  1381. (ip.visibility='0' OR ip.visibility='1')
  1382. $condition_session
  1383. $searchCondition
  1384. ORDER BY display_order DESC";
  1385. }
  1386. }
  1387. } else {
  1388. // STUDENT
  1389. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1390. if ($allowUserEditSetting && !api_is_anonymous()) {
  1391. if ($group_id == 0) {
  1392. // No group
  1393. $cond_user_id = " AND (
  1394. ip.lastedit_user_id = '".$user_id."' OR (
  1395. (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) OR
  1396. (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
  1397. )
  1398. ) ";
  1399. } else {
  1400. $cond_user_id = " AND (
  1401. ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.")
  1402. )";
  1403. $cond_user_id .= $extraGroupCondition;
  1404. }
  1405. } else {
  1406. if ($group_id == 0) {
  1407. $cond_user_id = " AND (
  1408. (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
  1409. ) ";
  1410. } else {
  1411. $cond_user_id = " AND (
  1412. (ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id."))
  1413. )";
  1414. $cond_user_id .= $extraGroupCondition;
  1415. }
  1416. }
  1417. $sql = "SELECT $select
  1418. FROM $tbl_announcement announcement INNER JOIN
  1419. $tbl_item_property ip
  1420. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1421. WHERE
  1422. announcement.c_id = $course_id AND
  1423. ip.c_id = $course_id AND
  1424. ip.tool='announcement'
  1425. $cond_user_id
  1426. $condition_session
  1427. $searchCondition
  1428. AND ip.visibility='1'
  1429. ORDER BY display_order DESC";
  1430. } else {
  1431. if ($user_id) {
  1432. if ($allowUserEditSetting && !api_is_anonymous()) {
  1433. $cond_user_id = " AND (
  1434. ip.lastedit_user_id = '".api_get_user_id()."' OR
  1435. ((ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id='0' OR ip.to_group_id IS NULL))
  1436. ) ";
  1437. } else {
  1438. $cond_user_id = " AND ((ip.to_user_id='$user_id' OR ip.to_user_id IS NULL) AND (ip.to_group_id='0' OR ip.to_group_id IS NULL) ) ";
  1439. }
  1440. $sql = "SELECT $select
  1441. FROM $tbl_announcement announcement
  1442. INNER JOIN $tbl_item_property ip
  1443. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1444. WHERE
  1445. announcement.c_id = $course_id AND
  1446. ip.c_id = $course_id AND
  1447. ip.tool='announcement'
  1448. $cond_user_id
  1449. $condition_session
  1450. $searchCondition
  1451. AND ip.visibility='1'
  1452. AND announcement.session_id IN(0, ".$session_id.")
  1453. ORDER BY display_order DESC";
  1454. } else {
  1455. if (($allowUserEditSetting && !api_is_anonymous())) {
  1456. $cond_user_id = " AND (
  1457. ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  1458. )";
  1459. } else {
  1460. $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL ";
  1461. }
  1462. $sql = "SELECT $select
  1463. FROM $tbl_announcement announcement
  1464. INNER JOIN $tbl_item_property ip
  1465. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1466. WHERE
  1467. announcement.c_id = $course_id AND
  1468. ip.c_id = $course_id AND
  1469. ip.tool='announcement'
  1470. $cond_user_id
  1471. $condition_session
  1472. $searchCondition AND
  1473. ip.visibility='1' AND
  1474. announcement.session_id IN ( 0,".api_get_session_id().")";
  1475. }
  1476. }
  1477. }
  1478. if (!is_null($start) && !is_null($limit)) {
  1479. $start = intval($start);
  1480. $limit = intval($limit);
  1481. $sql .= " LIMIT $start, $limit";
  1482. }
  1483. $result = Database::query($sql);
  1484. if ($getCount) {
  1485. $result = Database::fetch_array($result, 'ASSOC');
  1486. return $result['count'];
  1487. }
  1488. $iterator = 1;
  1489. $bottomAnnouncement = $announcement_number;
  1490. $displayed = [];
  1491. $results = [];
  1492. $actionUrl = api_get_path(WEB_CODE_PATH).'announcements/announcements.php?'.api_get_cidreq();
  1493. $emailIcon = Display::return_icon(
  1494. 'email.gif',
  1495. get_lang('AnnounceSentByEmail')
  1496. );
  1497. $attachmentIcon = Display::return_icon(
  1498. 'attachment.gif',
  1499. get_lang('Attachment')
  1500. );
  1501. $editIcon = Display::return_icon(
  1502. 'edit.png',
  1503. get_lang('Edit'),
  1504. '',
  1505. ICON_SIZE_SMALL
  1506. );
  1507. $deleteIcon = Display::return_icon(
  1508. 'delete.png',
  1509. get_lang('Delete'),
  1510. '',
  1511. ICON_SIZE_SMALL
  1512. );
  1513. while ($myrow = Database::fetch_array($result, 'ASSOC')) {
  1514. if (!in_array($myrow['id'], $displayed)) {
  1515. $sent_to_icon = '';
  1516. // the email icon
  1517. if ($myrow['email_sent'] == '1') {
  1518. $sent_to_icon = ' '.$emailIcon;
  1519. }
  1520. $groupReference = ($myrow['to_group_id'] > 0) ? ' <span class="label label-info">'.get_lang('Group').'</span> ' : '';
  1521. $title = $myrow['title'].$groupReference.$sent_to_icon;
  1522. $item_visibility = api_get_item_visibility(
  1523. $_course,
  1524. TOOL_ANNOUNCEMENT,
  1525. $myrow['id'],
  1526. $session_id
  1527. );
  1528. $myrow['visibility'] = $item_visibility;
  1529. // show attachment list
  1530. $attachment_list = self::get_attachment($myrow['id']);
  1531. $attachment_icon = '';
  1532. if (count($attachment_list) > 0) {
  1533. $attachment_icon = ' '.$attachmentIcon;
  1534. }
  1535. /* TITLE */
  1536. $user_info = api_get_user_info($myrow['insert_user_id']);
  1537. $username = sprintf(get_lang("LoginX"), $user_info['username']);
  1538. $username_span = Display::tag(
  1539. 'span',
  1540. $user_info['complete_name'],
  1541. array('title' => $username)
  1542. );
  1543. $title = Display::url(
  1544. $title.$attachment_icon,
  1545. $actionUrl.'&action=view&id='.$myrow['id']
  1546. );
  1547. // we can edit if : we are the teacher OR the element belongs to
  1548. // the session we are coaching OR the option to allow users to edit is on
  1549. if (api_is_allowed_to_edit(false, true) ||
  1550. (api_is_session_general_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id']))
  1551. || (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  1552. ) {
  1553. $modify_icons = "<a href=\"".$actionUrl."&action=modify&id=".$myrow['id']."\">".$editIcon."</a>";
  1554. if ($myrow['visibility'] == 1) {
  1555. $image_visibility = "visible";
  1556. $alt_visibility = get_lang('Hide');
  1557. } else {
  1558. $image_visibility = "invisible";
  1559. $alt_visibility = get_lang('Visible');
  1560. }
  1561. $modify_icons .= "<a href=\"".$actionUrl."&action=showhide&id=".$myrow['id']."&sec_token=".$stok."\">".
  1562. Display::return_icon($image_visibility.'.png', $alt_visibility, '', ICON_SIZE_SMALL)."</a>";
  1563. // DISPLAY MOVE UP COMMAND only if it is not the top announcement
  1564. if ($iterator != 1) {
  1565. $modify_icons .= "<a href=\"".$actionUrl."&action=move&up=".$myrow["id"]."&sec_token=".$stok."\">".
  1566. Display::return_icon('up.gif', get_lang('Up'))."</a>";
  1567. } else {
  1568. $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
  1569. }
  1570. if ($iterator < $bottomAnnouncement) {
  1571. $modify_icons .= "<a href=\"".$actionUrl."&action=move&down=".$myrow["id"]."&sec_token=".$stok."\">".
  1572. Display::return_icon('down.gif', get_lang('Down'))."</a>";
  1573. } else {
  1574. $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
  1575. }
  1576. if (api_is_allowed_to_edit(false, true)) {
  1577. $modify_icons .= "<a href=\"".$actionUrl."&action=delete&id=".$myrow['id']."&sec_token=".$stok."\" onclick=\"javascript:if(!confirm('".addslashes(api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES, api_get_system_encoding()))."')) return false;\">".
  1578. $deleteIcon."</a>";
  1579. }
  1580. $iterator++;
  1581. } else {
  1582. $modify_icons = Display::url(
  1583. Display::return_icon('default.png'),
  1584. $actionUrl.'&action=view&id='.$myrow['id']
  1585. );
  1586. }
  1587. $announcement = [
  1588. 'id' => $myrow['id'],
  1589. 'title' => $title,
  1590. 'username' => $username_span,
  1591. 'insert_date' => api_convert_and_format_date(
  1592. $myrow['insert_date'],
  1593. DATE_TIME_FORMAT_LONG
  1594. ),
  1595. 'lastedit_date' => api_convert_and_format_date(
  1596. $myrow['lastedit_date'],
  1597. DATE_TIME_FORMAT_LONG
  1598. ),
  1599. 'actions' => $modify_icons
  1600. ];
  1601. $results[] = $announcement;
  1602. }
  1603. $displayed[] = $myrow['id'];
  1604. }
  1605. return $results;
  1606. }
  1607. /**
  1608. * @return int
  1609. */
  1610. public static function getNumberAnnouncements()
  1611. {
  1612. // Maximum title messages to display
  1613. $maximum = '12';
  1614. // Database Table Definitions
  1615. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1616. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1617. $session_id = api_get_session_id();
  1618. $_course = api_get_course_info();
  1619. $course_id = $_course['real_id'];
  1620. $userId = api_get_user_id();
  1621. $condition_session = api_get_session_condition(
  1622. $session_id,
  1623. true,
  1624. true,
  1625. 'announcement.session_id'
  1626. );
  1627. if (api_is_allowed_to_edit(false, true)) {
  1628. // check teacher status
  1629. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  1630. if (api_get_group_id() == 0) {
  1631. $group_condition = '';
  1632. } else {
  1633. $group_condition = " AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0 OR ip.to_group_id IS NULL)";
  1634. }
  1635. $sql = "SELECT
  1636. announcement.*,
  1637. ip.visibility,
  1638. ip.to_group_id,
  1639. ip.insert_user_id
  1640. FROM $tbl_announcement announcement
  1641. INNER JOIN $tbl_item_property ip
  1642. ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref)
  1643. WHERE
  1644. announcement.c_id = $course_id AND
  1645. ip.c_id = $course_id AND
  1646. ip.tool = 'announcement' AND
  1647. ip.visibility <> '2'
  1648. $group_condition
  1649. $condition_session
  1650. GROUP BY ip.ref
  1651. ORDER BY display_order DESC
  1652. LIMIT 0, $maximum";
  1653. }
  1654. } else {
  1655. // students only get to see the visible announcements
  1656. if (empty($_GET['origin']) || $_GET['origin'] !== 'learnpath') {
  1657. $group_memberships = GroupManager::get_group_ids($_course['real_id'], $userId);
  1658. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1659. if (api_get_group_id() == 0) {
  1660. $cond_user_id = " AND (
  1661. ip.lastedit_user_id = '".$userId."' OR (
  1662. ip.to_user_id='".$userId."' OR
  1663. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  1664. ip.to_group_id IS NULL
  1665. )
  1666. )
  1667. ";
  1668. } else {
  1669. $cond_user_id = " AND (
  1670. ip.lastedit_user_id = '".$userId."'OR
  1671. ip.to_group_id IN (0, ".api_get_group_id().") OR
  1672. ip.to_group_id IS NULL
  1673. )";
  1674. }
  1675. } else {
  1676. if (api_get_group_id() == 0) {
  1677. $cond_user_id = " AND (
  1678. ip.to_user_id='".$userId."' OR
  1679. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  1680. ip.to_group_id IS NULL
  1681. ) ";
  1682. } else {
  1683. $cond_user_id = " AND (
  1684. ip.to_user_id='".$userId."' OR
  1685. ip.to_group_id IN (0, ".api_get_group_id().") OR
  1686. ip.to_group_id IS NULL
  1687. ) ";
  1688. }
  1689. }
  1690. // the user is member of several groups => display personal announcements AND
  1691. // his group announcements AND the general announcements
  1692. if (is_array($group_memberships) && count($group_memberships) > 0) {
  1693. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1694. FROM $tbl_announcement announcement
  1695. INNER JOIN $tbl_item_property ip
  1696. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1697. WHERE
  1698. announcement.c_id = $course_id AND
  1699. ip.c_id = $course_id AND
  1700. ip.tool='announcement' AND
  1701. ip.visibility='1'
  1702. $cond_user_id
  1703. $condition_session
  1704. GROUP BY ip.ref
  1705. ORDER BY display_order DESC
  1706. LIMIT 0, $maximum";
  1707. } else {
  1708. // the user is not member of any group
  1709. // this is an identified user => show the general announcements AND his personal announcements
  1710. if ($userId) {
  1711. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1712. $cond_user_id = " AND (
  1713. ip.lastedit_user_id = '".$userId."' OR
  1714. ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  1715. ) ";
  1716. } else {
  1717. $cond_user_id = " AND ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
  1718. }
  1719. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1720. FROM $tbl_announcement announcement
  1721. INNER JOIN $tbl_item_property ip
  1722. ON (announcement.c_id = ip.c_id AND announcement.id = ip.ref)
  1723. WHERE
  1724. announcement.c_id = $course_id AND
  1725. ip.c_id = $course_id AND
  1726. ip.tool='announcement' AND
  1727. ip.visibility='1'
  1728. $cond_user_id
  1729. $condition_session
  1730. GROUP BY ip.ref
  1731. ORDER BY display_order DESC
  1732. LIMIT 0, $maximum";
  1733. } else {
  1734. if (api_get_course_setting('allow_user_edit_announcement')) {
  1735. $cond_user_id = " AND (
  1736. ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  1737. ) ";
  1738. } else {
  1739. $cond_user_id = " AND ip.to_group_id='0' ";
  1740. }
  1741. // the user is not identiefied => show only the general announcements
  1742. $sql = "SELECT
  1743. announcement.*,
  1744. ip.visibility,
  1745. ip.to_group_id,
  1746. ip.insert_user_id
  1747. FROM $tbl_announcement announcement
  1748. INNER JOIN $tbl_item_property ip
  1749. ON (announcement.id = ip.ref AND announcement.c_id = ip.c_id)
  1750. WHERE
  1751. announcement.c_id = $course_id AND
  1752. ip.c_id = $course_id AND
  1753. ip.tool='announcement' AND
  1754. ip.visibility='1' AND
  1755. ip.to_group_id='0'
  1756. $condition_session
  1757. GROUP BY ip.ref
  1758. ORDER BY display_order DESC
  1759. LIMIT 0, $maximum";
  1760. }
  1761. }
  1762. }
  1763. }
  1764. $result = Database::query($sql);
  1765. return Database::num_rows($result);
  1766. }
  1767. }