AnnouncementManager.php 76 KB

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