AnnouncementManager.php 73 KB

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