AnnouncementManager.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865
  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_query = " 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_query = " 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_query = " 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_query);
  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. * @return bool|int
  413. */
  414. public static function add_group_announcement(
  415. $emailTitle,
  416. $newContent,
  417. $to,
  418. $to_users,
  419. $file = array(),
  420. $file_comment = '',
  421. $sendToUsersInSession = false
  422. ) {
  423. $_course = api_get_course_info();
  424. // Database definitions
  425. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  426. $order = self::get_last_announcement_order();
  427. $now = api_get_utc_datetime();
  428. $course_id = api_get_course_int_id();
  429. // store in the table announcement
  430. $params = [
  431. 'c_id' => $course_id,
  432. 'content' => $newContent,
  433. 'title' => $emailTitle,
  434. 'end_date' => $now,
  435. 'display_order' => $order,
  436. 'session_id' => api_get_session_id(),
  437. ];
  438. $last_id = Database::insert($tbl_announcement, $params);
  439. // Store the attach file
  440. if ($last_id) {
  441. $sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
  442. Database::query($sql);
  443. if (!empty($file)) {
  444. self::add_announcement_attachment_file(
  445. $last_id,
  446. $file_comment,
  447. $file
  448. );
  449. }
  450. // Store in item_property (first the groups, then the users
  451. if (!isset($to_users)) {
  452. // when no user is selected we send it to everyone
  453. $send_to = CourseManager::separateUsersGroups($to);
  454. // storing the selected groups
  455. if (is_array($send_to['groups'])) {
  456. foreach ($send_to['groups'] as $group) {
  457. api_item_property_update(
  458. $_course,
  459. TOOL_ANNOUNCEMENT,
  460. $last_id,
  461. "AnnouncementAdded",
  462. api_get_user_id(),
  463. $group
  464. );
  465. }
  466. }
  467. } else {
  468. // the message is sent to everyone, so we set the group to 0
  469. // storing the selected users
  470. if (is_array($to_users)) {
  471. foreach ($to_users as $user) {
  472. api_item_property_update(
  473. $_course,
  474. TOOL_ANNOUNCEMENT,
  475. $last_id,
  476. "AnnouncementAdded",
  477. api_get_user_id(),
  478. '',
  479. $user
  480. );
  481. }
  482. }
  483. }
  484. if ($sendToUsersInSession) {
  485. self::addAnnouncementToAllUsersInSessions($last_id);
  486. }
  487. }
  488. return $last_id;
  489. }
  490. /**
  491. * This function stores the announcement item in the announcement table
  492. * and updates the item_property table
  493. *
  494. * @param int id of the announcement
  495. * @param string email
  496. * @param string content
  497. * @param array users that will receive the announcement
  498. * @param mixed attachment
  499. * @param string file comment
  500. */
  501. public static function edit_announcement(
  502. $id,
  503. $emailTitle,
  504. $newContent,
  505. $to,
  506. $file = array(),
  507. $file_comment = '',
  508. $sendToUsersInSession = false
  509. ) {
  510. $_course = api_get_course_info();
  511. $course_id = api_get_course_int_id();
  512. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  513. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  514. $id = intval($id);
  515. $params = [
  516. 'title' => $emailTitle,
  517. 'content' => $newContent
  518. ];
  519. Database::update(
  520. $tbl_announcement,
  521. $params,
  522. ['c_id = ? AND id = ?' => [$course_id, $id]]
  523. );
  524. // save attachment file
  525. $row_attach = self::get_attachment($id);
  526. $id_attach = 0;
  527. if ($row_attach) {
  528. $id_attach = intval($row_attach['id']);
  529. }
  530. if (!empty($file)) {
  531. if (empty($id_attach)) {
  532. self::add_announcement_attachment_file($id, $file_comment, $file);
  533. } else {
  534. self::edit_announcement_attachment_file($id_attach, $file, $file_comment);
  535. }
  536. }
  537. // we remove everything from item_property for this
  538. $sql = "DELETE FROM $tbl_item_property
  539. WHERE c_id = $course_id AND ref='$id' AND tool='announcement'";
  540. Database::query($sql);
  541. if ($sendToUsersInSession) {
  542. self::addAnnouncementToAllUsersInSessions($id);
  543. }
  544. // store in item_property (first the groups, then the users
  545. if (!is_null($to)) {
  546. // !is_null($to): when no user is selected we send it to everyone
  547. $send_to = CourseManager::separateUsersGroups($to);
  548. // storing the selected groups
  549. if (is_array($send_to['groups'])) {
  550. foreach ($send_to['groups'] as $group) {
  551. api_item_property_update(
  552. $_course,
  553. TOOL_ANNOUNCEMENT,
  554. $id,
  555. "AnnouncementUpdated",
  556. api_get_user_id(),
  557. $group
  558. );
  559. }
  560. }
  561. // storing the selected users
  562. if (is_array($send_to['users'])) {
  563. foreach ($send_to['users'] as $user) {
  564. api_item_property_update(
  565. $_course,
  566. TOOL_ANNOUNCEMENT,
  567. $id,
  568. "AnnouncementUpdated",
  569. api_get_user_id(),
  570. 0,
  571. $user
  572. );
  573. }
  574. }
  575. } else {
  576. // the message is sent to everyone, so we set the group to 0
  577. api_item_property_update(
  578. $_course,
  579. TOOL_ANNOUNCEMENT,
  580. $id,
  581. "AnnouncementUpdated",
  582. api_get_user_id(),
  583. '0'
  584. );
  585. }
  586. }
  587. /**
  588. * @param int $announcementId
  589. */
  590. public static function addAnnouncementToAllUsersInSessions($announcementId)
  591. {
  592. $courseCode = api_get_course_id();
  593. $_course = api_get_course_info();
  594. $sessionList = SessionManager::get_session_by_course(api_get_course_int_id());
  595. if (!empty($sessionList)) {
  596. foreach ($sessionList as $sessionInfo) {
  597. $sessionId = $sessionInfo['id'];
  598. $userList = CourseManager::get_user_list_from_course_code(
  599. $courseCode,
  600. $sessionId
  601. );
  602. if (!empty($userList)) {
  603. foreach ($userList as $user) {
  604. api_item_property_update(
  605. $_course,
  606. TOOL_ANNOUNCEMENT,
  607. $announcementId,
  608. "AnnouncementUpdated",
  609. api_get_user_id(),
  610. 0,
  611. $user['user_id'],
  612. 0,
  613. 0,
  614. $sessionId
  615. );
  616. }
  617. }
  618. }
  619. }
  620. }
  621. /**
  622. * @param int $insert_id
  623. * @return bool
  624. */
  625. public static function update_mail_sent($insert_id)
  626. {
  627. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  628. if ($insert_id != strval(intval($insert_id))) {
  629. return false;
  630. }
  631. $insert_id = intval($insert_id);
  632. $course_id = api_get_course_int_id();
  633. // store the modifications in the table tbl_annoucement
  634. $sql = "UPDATE $tbl_announcement SET email_sent='1'
  635. WHERE c_id = $course_id AND id = $insert_id";
  636. Database::query($sql);
  637. }
  638. /**
  639. * Gets all announcements from a user by course
  640. * @param string course db
  641. * @param int user id
  642. * @return array html with the content and count of announcements or false otherwise
  643. */
  644. public static function get_all_annoucement_by_user_course($course_code, $user_id)
  645. {
  646. $course_info = api_get_course_info($course_code);
  647. $course_id = $course_info['real_id'];
  648. if (empty($user_id)) {
  649. return false;
  650. }
  651. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  652. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  653. if (!empty($user_id) && is_numeric($user_id)) {
  654. $user_id = intval($user_id);
  655. $sql = "SELECT DISTINCT announcement.title, announcement.content
  656. FROM $tbl_announcement announcement, $tbl_item_property toolitemproperties
  657. WHERE
  658. announcement.c_id = $course_id AND
  659. toolitemproperties.c_id = $course_id AND
  660. announcement.id = toolitemproperties.ref AND
  661. toolitemproperties.tool='announcement' AND
  662. (
  663. toolitemproperties.insert_user_id='$user_id' AND
  664. (toolitemproperties.to_group_id='0' OR toolitemproperties.to_group_id IS NULL)
  665. )
  666. AND toolitemproperties.visibility='1'
  667. AND announcement.session_id = 0
  668. ORDER BY display_order DESC";
  669. $rs = Database::query($sql);
  670. $num_rows = Database::num_rows($rs);
  671. $content = '';
  672. $i = 0;
  673. $result = array();
  674. if ($num_rows > 0) {
  675. while ($myrow = Database::fetch_array($rs)) {
  676. $content.= '<strong>' . $myrow['title'] . '</strong><br /><br />';
  677. $content.= $myrow['content'];
  678. $i++;
  679. }
  680. $result['content'] = $content;
  681. $result['count'] = $i;
  682. return $result;
  683. }
  684. return false;
  685. }
  686. return false;
  687. }
  688. /**
  689. * This function shows the form for sending a message to a specific group or user.
  690. * @param $to_already_selected
  691. */
  692. public static function show_to_form($to_already_selected)
  693. {
  694. $userList = self::get_course_users();
  695. $groupList = self::get_course_groups();
  696. if ($to_already_selected == '' || $to_already_selected == 'everyone') {
  697. $to_already_selected = array();
  698. }
  699. echo "<table id=\"recipient_list\" >";
  700. echo '<tr>';
  701. echo '<td>';
  702. echo '<label><input type="checkbox" id="send_to_all_users">'.get_lang('SendToAllUsers') . "</label>";
  703. echo "</td>";
  704. echo '</tr>';
  705. echo '<tr>';
  706. // the form containing all the groups and all the users of the course
  707. echo '<td>';
  708. echo "<strong>" . get_lang('Users') . "</strong><br />";
  709. self::construct_not_selected_select_form($groupList, $userList, $to_already_selected);
  710. echo "</td>";
  711. // the buttons for adding or removing groups/users
  712. echo '<td valign="middle">';
  713. 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])"><i class="fa fa-arrow-right"></i></button>';
  714. echo '<br /> <br />';
  715. 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])"><i class="fa fa-arrow-left"></i></button>';
  716. echo "</td>";
  717. echo "<td>";
  718. // the form containing the selected groups and users
  719. echo "<strong>" . get_lang('DestinationUsers') . "</strong><br />";
  720. self::construct_selected_select_form($groupList, $userList, $to_already_selected);
  721. echo "</td>";
  722. echo "</tr>";
  723. echo "</table>";
  724. }
  725. /**
  726. * this function shows the form for sending a message to a specific group or user.
  727. */
  728. public static function show_to_form_group($group_id)
  729. {
  730. echo "<table id=\"recipient_list\" >";
  731. echo "<tr>";
  732. echo "<td>";
  733. echo "<select id=\"not_selected_form\" name=\"not_selected_form[]\" size=5 style=\"width:200px\" multiple>";
  734. $group_users = GroupManager::getStudentsAndTutors($group_id);
  735. foreach ($group_users as $user) {
  736. echo '<option value="' . $user['user_id'] . '" title="' . sprintf(get_lang('LoginX'), $user['username']) . '" >' .
  737. api_get_person_name($user['firstname'], $user['lastname']) .
  738. '</option>';
  739. }
  740. echo '</select>';
  741. echo "</td>";
  742. // the buttons for adding or removing groups/users
  743. echo "<td valign=\"middle\">";
  744. 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])"><i class="fa fa-arrow-right"></i></button>';
  745. echo '<br /> <br />';
  746. 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])"><i class="fa fa-arrow-left"></i></button>';
  747. echo "</td>";
  748. echo "<td>";
  749. echo "<select id=\"selectedform\" name=\"selectedform[]\" size=5 style=\"width:200px\" multiple>";
  750. echo '</select>';
  751. echo "</td>";
  752. echo "</tr>";
  753. echo "</table>";
  754. }
  755. /**
  756. * Shows the form for sending a message to a specific group or user.
  757. * @param array $groupList
  758. * @param array $userList
  759. * @param array $to_already_selected
  760. */
  761. public static function construct_not_selected_select_form(
  762. $groupList = array(),
  763. $userList = array(),
  764. $to_already_selected = array()
  765. ) {
  766. echo '<select id="not_selected_form" name="not_selected_form[]" size="7" class="span4" multiple>';
  767. // adding the groups to the select form
  768. if (!empty($groupList)) {
  769. foreach ($groupList as $this_group) {
  770. if (is_array($to_already_selected)) {
  771. if (!in_array("GROUP:" . $this_group['id'], $to_already_selected)) {
  772. // $to_already_selected is the array containing the groups (and users) that are already selected
  773. $user_label = ($this_group['userNb'] > 0) ? get_lang('Users') : get_lang('LowerCaseUser') ;
  774. $user_disabled = ($this_group['userNb'] > 0) ? "" : "disabled=disabled" ;
  775. echo "<option $user_disabled value=\"GROUP:" . $this_group['id'] . "\">",
  776. "G: ", $this_group['name'], " - " . $this_group['userNb'] . " " . $user_label .
  777. "</option>";
  778. }
  779. }
  780. }
  781. // a divider
  782. echo "<option value=\"\">---------------------------------------------------------</option>";
  783. }
  784. // adding the individual users to the select form
  785. if (!empty($userList)) {
  786. foreach ($userList as $user) {
  787. if (is_array($to_already_selected)) {
  788. if (!in_array("USER:" . $user['user_id'], $to_already_selected)) {
  789. // $to_already_selected is the array containing the users (and groups) that are already selected
  790. echo "<option value=\"USER:" . $user['user_id'] . "\" title='" . sprintf(get_lang('LoginX'), $user['username']) . "'>",
  791. "", api_get_person_name($user['firstname'], $user['lastname']),
  792. "</option>";
  793. if (isset($user['drh_list']) && !empty($user['drh_list'])) {
  794. foreach ($user['drh_list'] as $drh) {
  795. echo "<option value=\"USER:" . $drh['user_id'] . "\" title='" . sprintf(get_lang('LoginX'), $drh['username']) . "'>&nbsp;&nbsp;&nbsp;&nbsp;",
  796. "", api_get_person_name($drh['firstname'], $drh['lastname']),
  797. "</option>";
  798. }
  799. }
  800. }
  801. }
  802. }
  803. }
  804. echo "</select>";
  805. }
  806. /**
  807. * this function shows the form for sending a message to a specific group or user.
  808. */
  809. /**
  810. * @param null $groupList
  811. * @param null $userList
  812. * @param $to_already_selected
  813. */
  814. public static function construct_selected_select_form($groupList = null, $userList = null, $to_already_selected = array())
  815. {
  816. // we load all the groups and all the users into a reference array that we use to search the name of the group / user
  817. $ref_array_groups = self::get_course_groups();
  818. $ref_array_users = self::get_course_users();
  819. // we construct the form of the already selected groups / users
  820. echo '<select id="selectedform" name="selectedform[]" size="7" multiple class="span4">';
  821. if (is_array($to_already_selected)) {
  822. foreach ($to_already_selected as $groupuser) {
  823. list($type, $id) = explode(":", $groupuser);
  824. if ($type == "GROUP") {
  825. echo "<option value=\"" . $groupuser . "\">G: " . $ref_array_groups[$id]['name'] . "</option>";
  826. } else {
  827. foreach ($ref_array_users as $key => $value) {
  828. if ($value['user_id'] == $id) {
  829. echo "<option value=\"" . $groupuser . "\" title='" . sprintf(get_lang('LoginX'), $value['username']) . "'>" .
  830. api_get_person_name($value['firstname'], $value['lastname']) . "</option>";
  831. if (isset($value['drh_list']) && !empty($value['drh_list'])) {
  832. foreach ($value['drh_list'] as $drh) {
  833. echo "<option value=\"USER:" . $drh['user_id'] . "\" title='" . sprintf(get_lang('LoginX'), $drh['username']) . "'>&nbsp;&nbsp;&nbsp;&nbsp;",
  834. "", api_get_person_name($drh['firstname'], $drh['lastname']),
  835. "</option>";
  836. }
  837. }
  838. break;
  839. }
  840. }
  841. }
  842. }
  843. } else {
  844. if ($to_already_selected == 'everyone') {
  845. // adding the groups to the select form
  846. if (is_array($ref_array_groups)) {
  847. foreach ($ref_array_groups as $this_group) {
  848. //api_display_normal_message("group " . $thisGroup[id] . $thisGroup[name]);
  849. 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
  850. echo "<option value=\"GROUP:" . $this_group['id'] . "\">",
  851. "G: ", $this_group['name'], " &ndash; " . $this_group['userNb'] . " " . get_lang('Users') .
  852. "</option>";
  853. }
  854. }
  855. }
  856. // adding the individual users to the select form
  857. foreach ($ref_array_users as $this_user) {
  858. 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
  859. echo "<option value=\"USER:", $this_user['user_id'], "\" title='" . sprintf(get_lang('LoginX'), $this_user['username']) . "'>",
  860. "", api_get_person_name($this_user['firstname'], $this_user['lastname']),
  861. "</option>";
  862. }
  863. }
  864. }
  865. }
  866. echo "</select>";
  867. }
  868. /**
  869. * Returns announcement info from its id
  870. *
  871. * @param int $course_id
  872. * @param int $annoucement_id
  873. * @return array
  874. */
  875. public static function get_by_id($course_id, $annoucement_id)
  876. {
  877. $annoucement_id = intval($annoucement_id);
  878. $course_id = $course_id ? intval($course_id) : api_get_course_int_id();
  879. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  880. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  881. $sql = "SELECT DISTINCT announcement.id, announcement.title, announcement.content
  882. FROM $tbl_announcement announcement
  883. INNER JOIN $tbl_item_property toolitemproperties
  884. ON
  885. announcement.id = toolitemproperties.ref AND
  886. announcement.c_id = $course_id AND
  887. toolitemproperties.c_id = $course_id
  888. WHERE
  889. toolitemproperties.tool='announcement' AND
  890. announcement.id = $annoucement_id";
  891. $result = Database::query($sql);
  892. if (Database::num_rows($result)) {
  893. return Database::fetch_array($result);
  894. }
  895. return array();
  896. }
  897. /**
  898. * this function gets all the users of the course,
  899. * including users from linked courses
  900. * @deprecate use CourseManager class
  901. */
  902. public static function get_course_users()
  903. {
  904. //this would return only the users from real courses:
  905. $session_id = api_get_session_id();
  906. if ($session_id != 0) {
  907. $userList = CourseManager::get_real_and_linked_user_list(
  908. api_get_course_id(),
  909. true,
  910. $session_id,
  911. true
  912. );
  913. } else {
  914. $userList = CourseManager::get_real_and_linked_user_list(
  915. api_get_course_id(),
  916. false,
  917. 0,
  918. true
  919. );
  920. }
  921. return $userList;
  922. }
  923. /**
  924. * this function gets all the groups of the course,
  925. * not including linked courses
  926. */
  927. public static function get_course_groups()
  928. {
  929. $session_id = api_get_session_id();
  930. if ($session_id != 0) {
  931. $new_group_list = CourseManager::get_group_list_of_course(
  932. api_get_course_id(),
  933. $session_id,
  934. 1
  935. );
  936. } else {
  937. $new_group_list = CourseManager::get_group_list_of_course(
  938. api_get_course_id(),
  939. 0,
  940. 1
  941. );
  942. }
  943. return $new_group_list;
  944. }
  945. /**
  946. * This tools loads all the users and all the groups who have received
  947. * a specific item (in this case an announcement item)
  948. */
  949. public static function load_edit_users($tool, $id)
  950. {
  951. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  952. $tool = Database::escape_string($tool);
  953. $id = intval($id);
  954. $course_id = api_get_course_int_id();
  955. $sql = "SELECT * FROM $tbl_item_property
  956. WHERE c_id = $course_id AND tool='$tool' AND ref = $id";
  957. $result = Database::query($sql);
  958. $to = array();
  959. while ($row = Database::fetch_array($result)) {
  960. $to_group = $row['to_group_id'];
  961. switch ($to_group) {
  962. // it was send to one specific user
  963. case null:
  964. $to[] = "USER:" . $row['to_user_id'];
  965. break;
  966. // it was sent to everyone
  967. case 0:
  968. return "everyone";
  969. break;
  970. default:
  971. $to[] = "GROUP:" . $row['to_group_id'];
  972. }
  973. }
  974. return $to;
  975. }
  976. /**
  977. * returns the javascript for setting a filter
  978. * this goes into the $htmlHeadXtra[] array
  979. */
  980. public static function user_group_filter_javascript()
  981. {
  982. return "<script language=\"JavaScript\" type=\"text/JavaScript\">
  983. <!--
  984. function jumpMenu(targ,selObj,restore)
  985. {
  986. eval(targ+\".location='\"+selObj.options[selObj.selectedIndex].value+\"'\");
  987. if (restore) selObj.selectedIndex=0;
  988. }
  989. //-->
  990. </script>";
  991. }
  992. /**
  993. * constructs the form to display all the groups and users the message has been sent to
  994. * input: $sent_to_array is a 2 dimensional array containing the groups and the users
  995. * the first level is a distinction between groups and users:
  996. * $sent_to_array['groups'] * and $sent_to_array['users']
  997. * $sent_to_array['groups'] (resp. $sent_to_array['users']) is also an array
  998. * containing all the id's of the groups (resp. users) who have received this message.
  999. * @author Patrick Cool <patrick.cool@>
  1000. */
  1001. public static function sent_to_form($sent_to_array)
  1002. {
  1003. // we find all the names of the groups
  1004. $group_names = self::get_course_groups();
  1005. // we count the number of users and the number of groups
  1006. if (isset($sent_to_array['users'])) {
  1007. $number_users = count($sent_to_array['users']);
  1008. } else {
  1009. $number_users = 0;
  1010. }
  1011. if (isset($sent_to_array['groups'])) {
  1012. $number_groups = count($sent_to_array['groups']);
  1013. } else {
  1014. $number_groups = 0;
  1015. }
  1016. $total_numbers = $number_users + $number_groups;
  1017. // starting the form if there is more than one user/group
  1018. $output = array();
  1019. if ($total_numbers > 1) {
  1020. //$output.="<option>".get_lang("SentTo")."</option>";
  1021. // outputting the name of the groups
  1022. if (is_array($sent_to_array['groups'])) {
  1023. foreach ($sent_to_array['groups'] as $group_id) {
  1024. $output[] = $group_names[$group_id]['name'];
  1025. }
  1026. }
  1027. if (isset($sent_to_array['users'])) {
  1028. if (is_array($sent_to_array['users'])) {
  1029. foreach ($sent_to_array['users'] as $user_id) {
  1030. $user_info = api_get_user_info($user_id);
  1031. $output[] = $user_info['complete_name_with_username'];
  1032. }
  1033. }
  1034. }
  1035. } else {
  1036. // there is only one user/group
  1037. if (isset($sent_to_array['users']) and is_array($sent_to_array['users'])) {
  1038. $user_info = api_get_user_info($sent_to_array['users'][0]);
  1039. $output[] = api_get_person_name($user_info['firstname'], $user_info['lastname']);
  1040. }
  1041. if (isset($sent_to_array['groups']) and
  1042. is_array($sent_to_array['groups']) and
  1043. isset($sent_to_array['groups'][0]) and
  1044. $sent_to_array['groups'][0] !== 0
  1045. ) {
  1046. $group_id = $sent_to_array['groups'][0];
  1047. $output[] = "&nbsp;" . $group_names[$group_id]['name'];
  1048. }
  1049. if (empty($sent_to_array['groups']) and empty($sent_to_array['users'])) {
  1050. $output[] = "&nbsp;" . get_lang('Everybody');
  1051. }
  1052. }
  1053. if (!empty($output)) {
  1054. $output = array_filter($output);
  1055. if (count($output) > 0) {
  1056. $output = implode(', ', $output);
  1057. }
  1058. return $output;
  1059. }
  1060. }
  1061. /**
  1062. * Returns all the users and all the groups a specific announcement item
  1063. * has been sent to
  1064. * @param string The tool (announcement, agenda, ...)
  1065. * @param int ID of the element of the corresponding type
  1066. * @return array Array of users and groups to whom the element has been sent
  1067. */
  1068. public static function sent_to($tool, $id)
  1069. {
  1070. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1071. $tool = Database::escape_string($tool);
  1072. $id = intval($id);
  1073. $sent_to_group = array();
  1074. $sent_to = array();
  1075. $course_id = api_get_course_int_id();
  1076. $sql = "SELECT to_group_id, to_user_id
  1077. FROM $tbl_item_property
  1078. WHERE c_id = $course_id AND tool = '$tool' AND ref=" . $id;
  1079. $result = Database::query($sql);
  1080. while ($row = Database::fetch_array($result)) {
  1081. // if to_group_id is null then it is sent to a specific user
  1082. // if to_group_id = 0 then it is sent to everybody
  1083. if ($row['to_group_id'] != 0) {
  1084. $sent_to_group[] = $row['to_group_id'];
  1085. }
  1086. // if to_user_id <> 0 then it is sent to a specific user
  1087. if ($row['to_user_id'] <> 0) {
  1088. $sent_to_user[] = $row['to_user_id'];
  1089. }
  1090. }
  1091. if (isset($sent_to_group)) {
  1092. $sent_to['groups'] = $sent_to_group;
  1093. }
  1094. if (isset($sent_to_user)) {
  1095. $sent_to['users'] = $sent_to_user;
  1096. }
  1097. return $sent_to;
  1098. }
  1099. /**
  1100. * Show a list with all the attachments according to the post's id
  1101. * @param int announcement id
  1102. * @return array with the post info
  1103. * @author Arthur Portugal
  1104. * @version November 2009, dokeos 1.8.6.2
  1105. */
  1106. public static function get_attachment($announcement_id)
  1107. {
  1108. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1109. $announcement_id = intval($announcement_id);
  1110. $course_id = api_get_course_int_id();
  1111. $row = array();
  1112. $sql = 'SELECT id, path, filename, comment FROM ' . $tbl_announcement_attachment . '
  1113. WHERE c_id = ' . $course_id . ' AND announcement_id = ' . $announcement_id . '';
  1114. $result = Database::query($sql);
  1115. if (Database::num_rows($result) != 0) {
  1116. $row = Database::fetch_array($result, 'ASSOC');
  1117. }
  1118. return $row;
  1119. }
  1120. /**
  1121. * This function add a attachment file into announcement
  1122. * @param int announcement id
  1123. * @param string file comment
  1124. * @param array uploaded file $_FILES
  1125. * @return int -1 if failed, 0 if unknown (should not happen), 1 if success
  1126. */
  1127. public static function add_announcement_attachment_file($announcement_id, $file_comment, $file)
  1128. {
  1129. $_course = api_get_course_info();
  1130. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1131. $return = 0;
  1132. $announcement_id = intval($announcement_id);
  1133. $course_id = api_get_course_int_id();
  1134. if (is_array($file) && $file['error'] == 0) {
  1135. // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1136. $courseDir = $_course['path'] . '/upload/announcements';
  1137. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1138. $updir = $sys_course_path . $courseDir;
  1139. // Try to add an extension to the file if it hasn't one
  1140. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1141. // user's file name
  1142. $file_name = $file['name'];
  1143. if (!filter_extension($new_file_name)) {
  1144. $return = -1;
  1145. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1146. } else {
  1147. $new_file_name = uniqid('');
  1148. $new_path = $updir . '/' . $new_file_name;
  1149. move_uploaded_file($file['tmp_name'], $new_path);
  1150. $params = [
  1151. 'c_id' => $course_id,
  1152. 'filename' => $file_name,
  1153. 'comment' => $file_comment,
  1154. 'path' => $new_file_name,
  1155. 'announcement_id' => $announcement_id,
  1156. 'size' => intval($file['size']),
  1157. ];
  1158. $insertId = Database::insert($tbl_announcement_attachment, $params);
  1159. if ($insertId) {
  1160. $sql = "UPDATE $tbl_announcement_attachment SET id = iid WHERE iid = $insertId";
  1161. Database::query($sql);
  1162. }
  1163. $return = 1;
  1164. }
  1165. }
  1166. return $return;
  1167. }
  1168. /**
  1169. * This function edit a attachment file into announcement
  1170. * @param int attach id
  1171. * @param array uploaded file $_FILES
  1172. * @param string file comment
  1173. * @return int
  1174. */
  1175. public static function edit_announcement_attachment_file($id_attach, $file, $file_comment)
  1176. {
  1177. $_course = api_get_course_info();
  1178. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1179. $return = 0;
  1180. $course_id = api_get_course_int_id();
  1181. if (is_array($file) && $file['error'] == 0) {
  1182. // TODO: This path is obsolete. The new document repository scheme should be kept in mind here.
  1183. $courseDir = $_course['path'] . '/upload/announcements';
  1184. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1185. $updir = $sys_course_path . $courseDir;
  1186. // Try to add an extension to the file if it hasn't one
  1187. $new_file_name = add_ext_on_mime(stripslashes($file['name']), $file['type']);
  1188. // user's file name
  1189. $file_name = $file ['name'];
  1190. if (!filter_extension($new_file_name)) {
  1191. $return - 1;
  1192. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  1193. } else {
  1194. $new_file_name = uniqid('');
  1195. $new_path = $updir . '/' . $new_file_name;
  1196. @move_uploaded_file($file['tmp_name'], $new_path);
  1197. $safe_file_comment = Database::escape_string($file_comment);
  1198. $safe_file_name = Database::escape_string($file_name);
  1199. $safe_new_file_name = Database::escape_string($new_file_name);
  1200. $id_attach = intval($id_attach);
  1201. $sql = "UPDATE $tbl_announcement_attachment SET filename = '$safe_file_name', comment = '$safe_file_comment', path = '$safe_new_file_name', size ='" . intval($file['size']) . "'
  1202. WHERE c_id = $course_id AND id = '$id_attach'";
  1203. $result = Database::query($sql);
  1204. if ($result === false) {
  1205. $return = -1;
  1206. Display :: display_error_message(get_lang('UplUnableToSaveFile'));
  1207. } else {
  1208. $return = 1;
  1209. }
  1210. }
  1211. }
  1212. return $return;
  1213. }
  1214. /**
  1215. * This function delete a attachment file by id
  1216. * @param integer $id attachment file Id
  1217. *
  1218. */
  1219. public static function delete_announcement_attachment_file($id)
  1220. {
  1221. $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
  1222. $id = intval($id);
  1223. $course_id = api_get_course_int_id();
  1224. $sql = "DELETE FROM $tbl_announcement_attachment
  1225. WHERE c_id = $course_id AND id = $id";
  1226. Database::query($sql);
  1227. }
  1228. /**
  1229. * @param int $id
  1230. * @param bool $sendToUsersInSession
  1231. * @param bool $sendToDrhUsers
  1232. */
  1233. public static function send_email($id, $sendToUsersInSession = false, $sendToDrhUsers = false)
  1234. {
  1235. $email = AnnouncementEmail::create(null, $id);
  1236. $email->send($sendToUsersInSession, $sendToDrhUsers);
  1237. }
  1238. /**
  1239. * @param $stok
  1240. * @param $announcement_number
  1241. */
  1242. public static function getAnnouncements($stok, $announcement_number)
  1243. {
  1244. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1245. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1246. $user_id = api_get_user_id();
  1247. $group_id = api_get_group_id();
  1248. $session_id = api_get_session_id();
  1249. $condition_session = api_get_session_condition($session_id, true, true, 'announcement.session_id');
  1250. $course_id = api_get_course_int_id();
  1251. $_course = api_get_course_info();
  1252. $group_memberships = GroupManager::get_group_ids($course_id, api_get_user_id());
  1253. $allowUserEditSetting = api_get_course_setting('allow_user_edit_announcement');
  1254. if (api_is_allowed_to_edit(false, true) ||
  1255. ($allowUserEditSetting && !api_is_anonymous())
  1256. ) {
  1257. // A.1. you are a course admin with a USER filter
  1258. // => see only the messages of this specific user + the messages of the group (s)he is member of.
  1259. //if (!empty($user_id)) {
  1260. if (0) {
  1261. if (is_array($group_memberships) && count($group_memberships) > 0 ) {
  1262. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1263. FROM $tbl_announcement announcement, $tbl_item_property ip
  1264. WHERE
  1265. announcement.c_id = $course_id AND
  1266. ip.c_id = $course_id AND
  1267. announcement.id = ip.ref AND
  1268. ip.tool = 'announcement' AND
  1269. (
  1270. ip.to_user_id = $user_id OR
  1271. ip.to_group_id IS NULL OR
  1272. ip.to_group_id IN (0, ".implode(", ", $group_memberships).")
  1273. ) AND
  1274. ip.visibility IN ('1', '0')
  1275. $condition_session
  1276. ORDER BY display_order DESC";
  1277. } else {
  1278. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1279. FROM $tbl_announcement announcement, $tbl_item_property ip
  1280. WHERE
  1281. announcement.c_id = $course_id AND
  1282. ip.c_id = $course_id AND
  1283. announcement.id = ip.ref AND
  1284. ip.tool ='announcement' AND
  1285. (ip.to_user_id = $user_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL) AND
  1286. ip.visibility IN ('1', '0')
  1287. $condition_session
  1288. ORDER BY display_order DESC";
  1289. }
  1290. } elseif ($group_id != 0) {
  1291. // A.2. you are a course admin with a GROUP filter
  1292. // => see only the messages of this specific group
  1293. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1294. FROM $tbl_announcement announcement, $tbl_item_property ip
  1295. WHERE
  1296. announcement.c_id = $course_id AND
  1297. ip.c_id = $course_id AND
  1298. announcement.id = ip.ref
  1299. AND ip.tool='announcement'
  1300. AND ip.visibility<>'2'
  1301. AND (ip.to_group_id = $group_id OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  1302. $condition_session
  1303. GROUP BY ip.ref
  1304. ORDER BY display_order DESC";
  1305. } else {
  1306. // A.3 you are a course admin without any group or user filter
  1307. // A.3.a you are a course admin without user or group filter but WITH studentview
  1308. // => see all the messages of all the users and groups without editing possibilities
  1309. if (isset($isStudentView) and $isStudentView=="true") {
  1310. $sql="SELECT
  1311. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1312. FROM $tbl_announcement announcement, $tbl_item_property ip
  1313. WHERE
  1314. announcement.c_id = $course_id AND
  1315. ip.c_id = $course_id AND
  1316. announcement.id = ip.ref
  1317. AND ip.tool='announcement'
  1318. AND ip.visibility='1'
  1319. $condition_session
  1320. GROUP BY ip.ref
  1321. ORDER BY display_order DESC";
  1322. } else {
  1323. // A.3.a you are a course admin without user or group filter and WTIHOUT studentview (= the normal course admin view)
  1324. // => see all the messages of all the users and groups with editing possibilities
  1325. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1326. FROM $tbl_announcement announcement, $tbl_item_property ip
  1327. WHERE
  1328. announcement.c_id = $course_id AND
  1329. ip.c_id = $course_id AND
  1330. announcement.id = ip.ref
  1331. AND ip.tool='announcement'
  1332. AND (ip.visibility='0' or ip.visibility='1')
  1333. $condition_session
  1334. GROUP BY ip.ref
  1335. ORDER BY display_order DESC";
  1336. }
  1337. }
  1338. } else {
  1339. // STUDENT
  1340. if (is_array($group_memberships) && count($group_memberships)>0) {
  1341. if ($allowUserEditSetting && !api_is_anonymous()) {
  1342. if (api_get_group_id() == 0) {
  1343. // No group
  1344. $cond_user_id = " AND (
  1345. ip.lastedit_user_id = '".$user_id."' OR (
  1346. ip.to_user_id='".$user_id."' OR
  1347. (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
  1348. )
  1349. ) ";
  1350. } else {
  1351. $cond_user_id = " AND (
  1352. ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".$group_id.")
  1353. )";
  1354. }
  1355. } else {
  1356. if (api_get_group_id() == 0) {
  1357. $cond_user_id = " AND (
  1358. ip.to_user_id=$user_id OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".implode(", ", $group_memberships)."))
  1359. ) ";
  1360. } else {
  1361. $cond_user_id = " AND (
  1362. ip.to_user_id=$user_id OR (ip.to_group_id IS NULL OR ip.to_group_id IN (0, ".api_get_group_id()."))
  1363. )";
  1364. }
  1365. }
  1366. $sql = "SELECT
  1367. announcement.*,
  1368. ip.visibility,
  1369. ip.to_group_id,
  1370. ip.insert_user_id,
  1371. ip.insert_date
  1372. FROM $tbl_announcement announcement,
  1373. $tbl_item_property ip
  1374. WHERE
  1375. announcement.c_id = $course_id AND
  1376. ip.c_id = $course_id AND
  1377. announcement.id = ip.ref
  1378. AND ip.tool='announcement'
  1379. $cond_user_id
  1380. $condition_session AND
  1381. ip.visibility='1'
  1382. ORDER BY display_order DESC";
  1383. } else {
  1384. if ($user_id) {
  1385. if ($allowUserEditSetting && !api_is_anonymous()) {
  1386. $cond_user_id = " AND (
  1387. ip.lastedit_user_id = '".api_get_user_id()."' OR
  1388. (ip.to_user_id='".$user_id."' AND (ip.to_group_id='0' OR ip.to_group_id IS NULL))
  1389. ) ";
  1390. } else {
  1391. $cond_user_id = " AND (ip.to_user_id='".$user_id."' AND (ip.to_group_id='0' OR ip.to_group_id IS NULL) ) ";
  1392. }
  1393. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1394. FROM $tbl_announcement announcement, $tbl_item_property ip
  1395. WHERE
  1396. announcement.c_id = $course_id AND
  1397. ip.c_id = $course_id AND
  1398. announcement.id = ip.ref AND
  1399. ip.tool='announcement'
  1400. $cond_user_id
  1401. $condition_session
  1402. AND ip.visibility='1'
  1403. AND announcement.session_id IN(0, ".api_get_session_id().")
  1404. ORDER BY display_order DESC";
  1405. } else {
  1406. if (($allowUserEditSetting && !api_is_anonymous())) {
  1407. $cond_user_id = " AND (
  1408. ip.lastedit_user_id = '".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  1409. )";
  1410. } else {
  1411. $cond_user_id = " AND ip.to_group_id='0' OR ip.to_group_id IS NULL ";
  1412. }
  1413. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id, ip.insert_date
  1414. FROM $tbl_announcement announcement, $tbl_item_property ip
  1415. WHERE
  1416. announcement.c_id = $course_id AND
  1417. ip.c_id = $course_id AND
  1418. announcement.id = ip.ref AND
  1419. ip.tool='announcement'
  1420. $cond_user_id
  1421. $condition_session AND
  1422. ip.visibility='1' AND
  1423. announcement.session_id IN ( 0,".api_get_session_id().")";
  1424. }
  1425. }
  1426. }
  1427. $result = Database::query($sql);
  1428. $num_rows = Database::num_rows($result);
  1429. $html = null;
  1430. if ($num_rows == 0) {
  1431. if ((api_is_allowed_to_edit(false, true) OR
  1432. (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) and
  1433. (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath')
  1434. ) {
  1435. $html .= '<div id="no-data-view">';
  1436. $html .= '<h3>' . get_lang('Announcements') . '</h3>';
  1437. $html .= Display::return_icon('valves.png', '', array(), 64);
  1438. $html .= '<div class="controls">';
  1439. $html .= Display::url(
  1440. get_lang('AddAnnouncement'),
  1441. api_get_self() . "?" . api_get_cidreq() . "&action=add",
  1442. array('class' => 'btn btn-primary')
  1443. );
  1444. $html .= '</div>';
  1445. $html .= '</div>';
  1446. } else {
  1447. $html = Display::return_message(get_lang('NoAnnouncements'), 'warning');
  1448. }
  1449. return $html;
  1450. }
  1451. $iterator = 1;
  1452. $bottomAnnouncement = $announcement_number;
  1453. $origin = null;
  1454. $html .= '<table width="100%" class="data_table announcements-list">';
  1455. $ths = Display::tag('th', get_lang('Title'));
  1456. $ths .= Display::tag('th', get_lang('By') );
  1457. $ths .= Display::tag('th', get_lang('LastUpdateDate') );
  1458. if (api_is_allowed_to_edit(false,true) OR (api_is_course_coach() &&
  1459. api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id']))
  1460. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1461. $ths .= Display::tag('th', get_lang('Modify'));
  1462. }
  1463. $html .= Display::tag('tr', $ths);
  1464. $displayed = array();
  1465. while ($myrow = Database::fetch_array($result, 'ASSOC')) {
  1466. if (!in_array($myrow['id'], $displayed)) {
  1467. $sent_to_icon = '';
  1468. // the email icon
  1469. if ($myrow['email_sent'] == '1') {
  1470. $sent_to_icon = ' '.Display::return_icon('email.gif', get_lang('AnnounceSentByEmail'));
  1471. }
  1472. $title = $myrow['title'].$sent_to_icon;
  1473. $item_visibility = api_get_item_visibility($_course, TOOL_ANNOUNCEMENT, $myrow['id'], $session_id);
  1474. $myrow['visibility'] = $item_visibility;
  1475. // the styles
  1476. if ($myrow['visibility'] == '0') {
  1477. $style='invisible';
  1478. } else {
  1479. $style = '';
  1480. }
  1481. $html .= '<tr class="announcements-list-line">';
  1482. // show attachment list
  1483. $attachment_list = AnnouncementManager::get_attachment($myrow['id']);
  1484. $attachment_icon = '';
  1485. if (count($attachment_list)>0) {
  1486. $attachment_icon = ' '.Display::return_icon('attachment.gif',get_lang('Attachment'));
  1487. }
  1488. /* TITLE */
  1489. $user_info = api_get_user_info($myrow['insert_user_id']);
  1490. $username = sprintf(get_lang("LoginX"), $user_info['username']);
  1491. $username_span = Display::tag('span', api_get_person_name($user_info['firstName'], $user_info['lastName']), array('title'=>$username));
  1492. $title = Display::url($title.$attachment_icon, api_get_self().'?'.api_get_cidreq().'&action=view&id='.$myrow['id']);
  1493. $html .= Display::tag('td', Security::remove_XSS($title), array('class' => 'announcements-list-line-title '.$style));
  1494. $html .= Display::tag('td', $username_span, array('class' => 'announcements-list-line-by-user'));
  1495. $html .= Display::tag('td', api_convert_and_format_date($myrow['insert_date'], DATE_TIME_FORMAT_LONG), array('class' => 'announcements-list-line-datetime'));
  1496. // we can edit if : we are the teacher OR the element belongs to
  1497. // the session we are coaching OR the option to allow users to edit is on
  1498. if (api_is_allowed_to_edit(false,true) OR
  1499. (api_is_course_coach() && api_is_element_in_the_session(TOOL_ANNOUNCEMENT, $myrow['id']))
  1500. OR (api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())
  1501. ) {
  1502. $modify_icons = "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=modify&id=".$myrow['id']."\">".
  1503. Display::return_icon('edit.png', get_lang('Edit'),'',ICON_SIZE_SMALL)."</a>";
  1504. if ($myrow['visibility']==1) {
  1505. $image_visibility="visible";
  1506. $alt_visibility=get_lang('Hide');
  1507. } else {
  1508. $image_visibility="invisible";
  1509. $alt_visibility=get_lang('Visible');
  1510. }
  1511. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&origin=".$origin."&action=showhide&id=".$myrow['id']."&sec_token=".$stok."\">".
  1512. Display::return_icon($image_visibility.'.png', $alt_visibility,'',ICON_SIZE_SMALL)."</a>";
  1513. // DISPLAY MOVE UP COMMAND only if it is not the top announcement
  1514. if ($iterator != 1) {
  1515. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=move&up=".$myrow["id"]."&sec_token=".$stok."\">".
  1516. Display::return_icon('up.gif', get_lang('Up'))."</a>";
  1517. } else {
  1518. $modify_icons .= Display::return_icon('up_na.gif', get_lang('Up'));
  1519. }
  1520. if ($iterator < $bottomAnnouncement) {
  1521. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&action=move&down=".$myrow["id"]."&sec_token=".$stok."\">".
  1522. Display::return_icon('down.gif', get_lang('Down'))."</a>";
  1523. } else {
  1524. $modify_icons .= Display::return_icon('down_na.gif', get_lang('Down'));
  1525. }
  1526. if (api_is_allowed_to_edit(false,true)) {
  1527. $modify_icons .= "<a href=\"".api_get_self()."?".api_get_cidreq()."&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;\">".
  1528. Display::return_icon('delete.png', get_lang('Delete'),'',ICON_SIZE_SMALL).
  1529. "</a>";
  1530. }
  1531. $iterator ++;
  1532. $html .= Display::tag('td', $modify_icons, array('class' => 'announcements-list-line-actions'));
  1533. }
  1534. $html .= "</tr>";
  1535. }
  1536. $displayed[] = $myrow['id'];
  1537. }
  1538. $html .= "</table>";
  1539. return $html;
  1540. }
  1541. /**
  1542. * @return int
  1543. */
  1544. public static function getNumberAnnouncements()
  1545. {
  1546. // Maximum title messages to display
  1547. $maximum = '12';
  1548. // Database Table Definitions
  1549. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  1550. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1551. $session_id = api_get_session_id();
  1552. $_course = api_get_course_info();
  1553. $course_id = $_course['real_id'];
  1554. $userId = api_get_user_id();
  1555. $condition_session = api_get_session_condition($session_id, true, true, 'announcement.session_id');
  1556. if (api_is_allowed_to_edit(false,true)) {
  1557. // check teacher status
  1558. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  1559. if (api_get_group_id() == 0) {
  1560. $group_condition = "";
  1561. } else {
  1562. $group_condition = " AND (ip.to_group_id='".api_get_group_id()."' OR ip.to_group_id = 0 OR ip.to_group_id IS NULL)";
  1563. }
  1564. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1565. FROM $tbl_announcement announcement, $tbl_item_property ip
  1566. WHERE
  1567. announcement.c_id = $course_id AND
  1568. ip.c_id = $course_id AND
  1569. announcement.id = ip.ref AND
  1570. ip.tool = 'announcement' AND
  1571. ip.visibility <> '2'
  1572. $group_condition
  1573. $condition_session
  1574. GROUP BY ip.ref
  1575. ORDER BY display_order DESC
  1576. LIMIT 0,$maximum";
  1577. }
  1578. } else {
  1579. // students only get to see the visible announcements
  1580. if (empty($_GET['origin']) or $_GET['origin'] !== 'learnpath') {
  1581. $group_memberships = GroupManager::get_group_ids($_course['real_id'], $userId);
  1582. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1583. if (api_get_group_id() == 0) {
  1584. $cond_user_id = " AND (
  1585. ip.lastedit_user_id = '".$userId."' OR (
  1586. ip.to_user_id='".$userId."' OR
  1587. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  1588. ip.to_group_id IS NULL
  1589. )
  1590. )
  1591. ";
  1592. } else {
  1593. $cond_user_id = " AND (
  1594. ip.lastedit_user_id = '".$userId."'OR
  1595. ip.to_group_id IN (0, ".api_get_group_id().") OR
  1596. ip.to_group_id IS NULL
  1597. )";
  1598. }
  1599. } else {
  1600. if (api_get_group_id() == 0) {
  1601. $cond_user_id = " AND (
  1602. ip.to_user_id='".$userId."' OR
  1603. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  1604. ip.to_group_id IS NULL
  1605. ) ";
  1606. } else {
  1607. $cond_user_id = " AND (
  1608. ip.to_user_id='".$userId."' OR
  1609. ip.to_group_id IN (0, ".api_get_group_id().") OR
  1610. ip.to_group_id IS NULL
  1611. ) ";
  1612. }
  1613. }
  1614. // the user is member of several groups => display personal announcements AND his group announcements AND the general announcements
  1615. if (is_array($group_memberships) && count($group_memberships)>0) {
  1616. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1617. FROM $tbl_announcement announcement, $tbl_item_property ip
  1618. WHERE
  1619. announcement.c_id = $course_id AND
  1620. ip.c_id = $course_id AND
  1621. announcement.id = ip.ref AND
  1622. ip.tool='announcement'
  1623. AND ip.visibility='1'
  1624. $cond_user_id
  1625. $condition_session
  1626. GROUP BY ip.ref
  1627. ORDER BY display_order DESC
  1628. LIMIT 0, $maximum";
  1629. } else {
  1630. // the user is not member of any group
  1631. // this is an identified user => show the general announcements AND his personal announcements
  1632. if ($userId) {
  1633. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  1634. $cond_user_id = " AND (
  1635. ip.lastedit_user_id = '".$userId."' OR
  1636. ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  1637. ) ";
  1638. } else {
  1639. $cond_user_id = " AND ( ip.to_user_id='".$userId."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
  1640. }
  1641. $sql = "SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1642. FROM $tbl_announcement announcement, $tbl_item_property ip
  1643. WHERE
  1644. announcement.c_id = $course_id AND
  1645. ip.c_id = $course_id AND
  1646. announcement.id = ip.ref
  1647. AND ip.tool='announcement'
  1648. AND ip.visibility='1'
  1649. $cond_user_id
  1650. $condition_session
  1651. GROUP BY ip.ref
  1652. ORDER BY display_order DESC
  1653. LIMIT 0, $maximum";
  1654. } else {
  1655. if (api_get_course_setting('allow_user_edit_announcement')) {
  1656. $cond_user_id = " AND (
  1657. ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  1658. ) ";
  1659. } else {
  1660. $cond_user_id = " AND ip.to_group_id='0' ";
  1661. }
  1662. // the user is not identiefied => show only the general announcements
  1663. $sql="SELECT announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  1664. FROM $tbl_announcement announcement, $tbl_item_property ip
  1665. WHERE
  1666. announcement.c_id = $course_id AND
  1667. ip.c_id = $course_id AND
  1668. announcement.id = ip.ref
  1669. AND ip.tool='announcement'
  1670. AND ip.visibility='1'
  1671. AND ip.to_group_id='0'
  1672. $condition_session
  1673. GROUP BY ip.ref
  1674. ORDER BY display_order DESC
  1675. LIMIT 0, $maximum";
  1676. }
  1677. }
  1678. }
  1679. }
  1680. $result = Database::query($sql);
  1681. return Database::num_rows($result);
  1682. }
  1683. }