AnnouncementManager.php 74 KB

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