AnnouncementManager.php 67 KB

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