AnnouncementManager.php 71 KB

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