AnnouncementManager.php 82 KB

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